Monday, November 11, 2013

Discovering the PID of a running shell script

I recently needed a way to script the shut down of several bash scripts without killing all of the bash processes. I did not want to search through a series of ps axf | grep commands to kill each script one-at-a-time. I found this helpful tip on stackoverflow in relation to a similar question about Java:

# ps --no-headers -C scriptName.sh -o pid


This returns a list of PIDs, which can then be easily incorporated into a script with something along these lines:

for X in $(ps --no-headers -C scriptName.sh -o pid)
do
   kill -15 $X
done

No comments:

Post a Comment