How can I determine if a process is running or not?
And then make a bash script that executes some stuff based on that condition...
if process 'abc' is running, do this
if it is not running , do this.
How can I achieve 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.
The square brackets are useful to avoid the output of the
|show 6 more comments|
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.
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
First thing that came to my mind for your problem: