Asterisk, and other worldly endeavours.

A blog by Leif Madsen

Return just PID of script with ‘ps’ and ‘awk’

Today I ran into an issue where I am running a python script that I needed to get the process ID (PID) of, but that the process was being output with a space between ‘python’ and the actual script name (in this case,

I’m sure it’s totally overkill and there is a much easier way I didn’t find to do this, but after some scouring of The Google, I found something that works! (The purpose of this was to kill off a rogue script process each night so I could restart it.)

Here is what the output looks like with just ps aux | grep python

# ps aux | grep python
root      1120  0.0  0.2  50176  4380 ?        Sl   Aug04  24:52 /usr/bin/python /usr/bin/fail2ban-server -b -s /var/run/fail2ban/fail2ban.sock
root     18182  2.2  1.5  35328 32148 pts/0    S    08:21   0:11 python
root     18219  0.0  0.0   3328   804 pts/0    S+   08:29   0:00 grep python

A little bit more data than I wanted, plus of course ‘grep python’ is always going to be returned if I just use grep straight up. Putting many pieces together from a few websites, this is what I came up with to just return the PID of the script:

ps -eo pid,command | grep "" | grep -v grep | awk '{print $1}'

What I’m doing, is controlling what is returned, so in this case have ps just return the pid and command fields. Run that through grep to just get the script I wanted, pipe that back through grep to remove the line including grep python and then pipe that through awk to just return the first field (which would be the pid of the process I wanted).

All in all, a nice hack 🙂

Written by Leif Madsen

2011/09/15 at 7:34 am

Posted in Being Productive, Programming

Tagged with , , , , ,

4 Responses

Subscribe to comments with RSS.

  1. Try ps -eo pid,command | grep [n]ameofscript. [n] will match the char but not the search term. I think ps has a shorter way of returning just the pid, too. Actually I’m pretty sure there’s an entirely-python way of getting its own pid. This method is prone to error if the script is running multiple times.

    Andrew Kohlsmith

    2011/09/15 at 2:19 pm

  2. You can use pgrep -lf jirairc.


    2011/09/26 at 1:18 am

  3. Great stuff, regarding the actual post title.

    ps -eo pid,command | grep [process] | grep -v grep | awk ‘{print $1}’

    Red skin

    2012/08/14 at 7:09 am

  4. I was just searching for this command. Thanks!
    Now I can kill processes in batch!


    2012/08/17 at 12:04 pm

Comments are closed.

%d bloggers like this: