I want to parse the output of the ps command and return true or false if a program is found. How would anyone here do this? I have access to the command line so it's open for me, no restrictions.
migrated from stackoverflow.com Jan 11 '10 at 2:59
Pipe the output to
The above is more suited for causally checking stuff by hand. If you want to use something in a script, use the return value from
returns 0 if some process exists with the specified name and 1 otherwise.
In the general case, you should look for your SPECIFIC process in the process list, rather than something which just happens to have the same name.
We've previously been bitten by bugs where a process with the same (or similar) name appears and breaks something (usually a monitor or start/stop script, so it's not usually vital).
Your process should write a pid file when it starts up (or before, by having a shell script write its pid in then exec your task), and then you can refer to the contents of that pid file in a subsequent shell script.
To check if a specific pid is running, kill it with "signal 0" - this doesn't actually send a signal just checks that it is there.
I wouldn't recommend the ps and pipe through grep approach in the other answer reason being that you might get false positives (especially for short command names which are abundant in UNIX) and have to resort to double greps etc. just to figure it out. Instead, consider using the
The best approach though would be to maintain the pid of the process you've spawned off in a file or in the memory of the controller process so that you know it upfront and can search for it.