bash logoBash (a.k.a. Bourne Again SHell) is one of the best tools you’ll ever come across on Linux, Unix or OSX. You can do virtually anything with it! It is said that bash even cures cancer! Now, let me make this very, very clear… I’m not an expert. I haven’t written much bash scripts for the past 5 years, so it would be fair to say I’m a n00b. At the very least, I’m not a l33t bash afiçionado.

I had a specific need. I needed a way to find out if a process was running and to obtain its process id (pid). Well, in all honesty, I didn’t REALLY needed the pid, but thought it could maybe be handy for some, so I included it anyway. In my case, all I wanted to know was whether a process was running and if not, to start it. I digress.

First what I needed was to find the process I’m looking for:

#!/bin/bash

pid=`ps -eo pid,args | grep myprocess | \
grep -v grep | cut -c1-6`
#do what I need with the pid

What we did here:

First we get the process with the ps command. the -e option tells it to find processes in the entire environment. Without -e it will return only the processes owned by the user who runs the command. The -o option lets you specify which fields you want. That is exactly what the pid,args are. It is the fields we want, and the only fields we want in this instance.

Next we pipe the ps command through the grep command. grep extracts all the lines the ps command return that contains the string you specify, in this case “myprocess”.

Since the grep command will also return the grep process, we need to pipe it to another grep command to remove the grep proccess itself. The -v option tells grep to do an invert match, i.o.w. exclude the line containing the word “grep”. Lets look at an example:

If we only use:

$ ps -eo pid,args | grep myprocess
1234    /path/to/myprocess.sh
34761  grep myprocess

It returns two lines. We’re not interested in the second line and need to exclude it so the command:

$ ps -eo pid,args | grep myprocess | grep -v grep
1234   /path/to/myprocess.sh

grep -v grep excludes the line that contains “grep”. Nifty. Just what we need.

The next step is to get the pid (process id) piping the results to the cut command. The -c1-6 simply says “cut the characters from the first character up to and including the 6th character”.

... cut -c1-6

There you have it! You can now do what you need to with that process id. Crush it, Kill it, Destroy it, whatever tickles your fancy!

I’d like to know from the real experts, what would be an easier way to do it? More important, what would be a better way to do it? I have to be honest, I didn’t really googled much for an answer since I was busy doing the Afrigator language files and was looking for anything to procrastinate on and this seemed like great fun! My solution might not be very good. For example, it does not allow for more than one process. It assumes that there would always only be one process running. It should be easy enough to allow for more than one process, but I’ll leave that up to you! You would need to split the lines… argh, just figure it out!

PS: Justin & Lester, next time you give me grief, I will replace the two of you with bash scripts!

Update:

Here is a simpler command by Donald Jackson via Twitter utilizing awk:

$ ps aux | grep processname | grep -v grep | awk '{print $2}'

Thanx @donaldza

Welcome back! You should subscribe to my RSS feed here.
You should follow me on Twitter here
You should follow me on Gatorpeeps here.