How can I determine if a process is running or not and then have a bash script execute some stuff based on that condition...
if process 'abc' is running, do this
if it is not running , do this.
A bash script to do something like that would look something like this:
This script is just checking to see if the program "gedit" is running.
This is what I use:
In plain English: if 'pgrep' returns 0, the process is running, otherwise it is not.
Bash Scripting :: String Comparisons
Ubuntu Manuals pgrep
Any solution that uses something like
Because you are not checking if a specific process is running, you are checking if there are any processes running that happens to match
So how do I reliably test for a certain running process?
That depends on what you need the test for.
I want to ensure that service abc is running, and if not, start it
This is what init and upstart are for. They'll start the service and ensure its pid gets stored in a pidfile. Try to start the service again (via init or upstart) and it will check the pidfile, and either start it if it's not there, or abort if it's already running. This is still not 100% reliable, but it's as close as you get.
See How can I check to see if my game server is still running... for other solutions.
abc is my script. I need to make sure only one instance of my script is running.
In this case, use a lockfile or a lockdir. E.g.
See Bash FAQ 45 for other ways of locking.
I usually have a 'pidof -x $(basename $0)' on my scripts to check if it's already running.
Riffing on @rommel-cid's idea, you can use
First thing that came to my mind for your problem: