Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

killing a job via script

Status
Not open for further replies.

jongbsio

Programmer
Aug 16, 2002
5
US
I need to extract the PID from the file created by the command

ps -A >> myfile
PID TTY TIME CMD
29465 - 0:00 dbcd
29466 - 0:00 dbcfs
29467 - 0:00 dbcfsrun
29468 - 0:00 dbcfsrun
29469 - 0:00 dbcfsrun

grep 'dbcd' myfile > myfile2
29465 - 0:00 dbcd

How do I extract out the PID from this file, I will want to do a
kill 29465

Thanks.
 
Try a:

cat myfile2 | cut -f1 -d' ' > killpid
kill `cat killpid`

Note that the ` are backticks (above the tab key), not regular apostrophes.

HTH.
 
How about this:

PID=`ps -e | grep $1 | awk '{ print $1 }' > /tmp/pid.txt`
cat /tmp/pid.txt | while read LINE
do kill -9 $LINE
done

Let's say you call the script killit.sh. The command you would use to kill the dbcd process would be:
killit.sh dbcd
 
My turn...

ps -A | awk ' /dbcd/ {print $1} ' | xargs -n 1 kill

Using grep to pipe to awk is not elegant since it creates an extra process and awk has excellent regex matching ability. There's probably an even simpler way to write this, maybe:

kill `ps -A |awk '/dbcd/ {print $1}'`

since that gets rid of my beloved xargs and we are down to three commands. But if you have a big machine, who cares, right? IBM Certified -- AIX 4.3 Obfuscation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top