Register
It is currently Sat Nov 01, 2014 4:25 am

Script to kill a process


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Fri May 12, 2006 12:38 pm   

Joined: Fri May 12, 2006 12:28 pm
Posts: 2
Hello I have a script to kill a PID that i put in a file but I don't know the kill command to kill the process ID in my file. It looks like :

#!/bin/tcsh
ps -ef | grep j2ee5[1] | awk '{print $2}' > grep
kill -9 grep
cd /opt/oracle/product/9iAS904/j2ee51/home
./startorion.sh


Top
 Profile  
 PostPosted: Fri May 12, 2006 9:39 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
JpBoy wrote:
Hello I have a script to kill a PID that i put in a file but I don't know the kill command to kill the process ID in my file. It looks like :

#!/bin/tcsh
ps -ef | grep j2ee5[1] | awk '{print $2}' > grep
kill -9 grep
cd /opt/oracle/product/9iAS904/j2ee51/home
./startorion.sh


you might want to think about something more like this:
Code:
#!/bin/tcsh
ps -ef | grep j2ee5[1]  | awk '{print $2}' > new_file
kill -9 `cat new_file`
cd /opt/oracle/product/9iAS904/j2ee51/home
./startorion.sh

hope that helps :)


Top
 Profile  
 PostPosted: Sat May 13, 2006 6:03 am   

Joined: Fri May 12, 2006 12:28 pm
Posts: 2
It didn't work when I did :

#!/bin/tcsh
ps -ef | grep j2ee5[1] | awk '{print $2}' > grep
kill -9 'cat grep'
cd /opt/oracle/product/9iAS904/j2ee51/home
./startorion.sh

grep: No match.
kill: Arguments should be jobs or process id's.


Top
 Profile  
 PostPosted: Wed May 17, 2006 5:27 am   

Joined: Mon Mar 27, 2006 11:11 am
Posts: 5
i know this is few days old thread: but i would use `pgrep'. using ps for anything other than presenting the user the output is usually not-so-bright idea. also using kill -9 is usually a bad ideas too, use simple kill and add the -9 only if that fails.


Top
 Profile  
 PostPosted: Wed May 17, 2006 9:26 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
JpBoy wrote:
It didn't work when I did :

#!/bin/tcsh
ps -ef | grep j2ee5[1] | awk '{print $2}' > grep
kill -9 'cat grep'
cd /opt/oracle/product/9iAS904/j2ee51/home
./startorion.sh

grep: No match.
kill: Arguments should be jobs or process id's.

jbsnake wrote:
you might want to think about something more like this:
Code:
#!/bin/tcsh
ps -ef | grep j2ee5[1]  | awk '{print $2}' > new_file
kill -9 `cat new_file`
cd /opt/oracle/product/9iAS904/j2ee51/home
./startorion.sh



you noticed how i changed the name of the file after > to something other than grep?

bash thinks you are trying to redirect something to grep... not to a file...
try using *exactly* what i put...


Top
 Profile  
 PostPosted: Fri Jun 30, 2006 3:08 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Heya JpBoy,

I see a couple of potential problems here:

Code:
#!/bin/tcsh
ps -ef | grep j2ee5[1] | awk '{print $2}' > grep
kill -9 grep
cd /opt/oracle/product/9iAS904/j2ee51/home
./startorion.sh


First thing is your grep command:

# grep j2ee5[1]

If you're looking for something with square brackets, the shell (bash, I'm not positive about tcsh) will interpret them as a test expression. You'll need to escape them with backslashes, like so:

# grep j2ee5\[1\]


The next potential problem is, what if you come up with more than one result? Others have already pointed out that you need to get the data from the file with something like cat, but try this:

# cat new_file |xargs kill -9


Another thing that you might see is your "grep" command showing up in your output, and getting killed. For example:

[jeo@dojo:~] $ ps -ef|grep monkeys
jeo 16353 16299 99 16:04 pts/1 00:00:10 yes monkeys
jeo 16358 16299 0 16:04 pts/1 00:00:00 grep monkeys

We want to exclude that line with a "grep -v"

[jeo@dojo:~] $ ps -ef|grep monkeys|grep -v grep
jeo 16353 16299 99 16:04 pts/1 00:00:04 yes monkeys


Last one, I promise :) This is more of a "I hate temporary files" thing. You don't really need to create that temporary new_file. Try this instead:

# ps -ef | grep j2ee5\[1\] | grep -v grep | awk '{print $2}' | xargs kill -9


(as a side note, you may also want to look into 'pidof')
[jeo@dojo:~] $ /sbin/pidof yes
16353


I hope this helps!
-Jeo


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP