[SOLVED] Check if a program is running, if it isn't start it?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Some notes that may be of use to the OP going forward:
"ps -e | grep program_name" should work in your specific case here, but you cannot trust "ps -ef | grep program_name". Why would you want to add the -f option to ps? Because in the generic case, you might not be grepping for the program_name only, but you may be grepping for something more specific.
Using "ps -ef | grep program_name" can fail, because it will not only match "program_name", but it will also match "grep program_name". So the grep may see itself grep'ing, and thus your script would think that program_name is running when actually it is not. It's a timing issue whether grep will catch itself or not, but it certainly can, and usually does, so you need to take that possibility into account.
Better to use something like "pkill -0 -f program_name" to check if it's running or not.
Or, "ps -ef program_name | grep -v grep" would produce a similar result, by filtering out any instance of grep catching itself.
Say for example, you wanted to check if somebody was using vi to edit a specific file. You could not say "ps -e | grep vi" because that would find every instance of vi and you are only interested in the specific instance of "vi specific_filename". So you would have to use something more specific like ps -ef | egrep "vi .*specific_filename" | grep -v egrep (or the pkill -f equivalent). Even that regular expression in the egrep command may not be specific enough for this contrived example, but I'm not doing a tutorial on regular expressions, I'm only trying to point out that grep may catch itself grep'ing.
The command you are executing is not valid. It will never work. Try it without redirecting the output to /dev/null, and you'll see. It is always going to return an exit status of 1, so your test for equal to 0 will never match. And when it returns a 1, it does not mean what your script wants it to mean.
This is NOT what you want to do!
ps -e "deluge"
What works is to use -C (command name) instead of -e. The -C option takes an argument, the -e option does not.
ps -C "deluge"
Issue it from a command line like this to see the exit code for a command name that exists and one that doesn't.
ps -C "deluge" >& /dev/null ; echo $?
However, what you want to test for is an exit status greater than 0, or you can test for exit status equal 1. Firerat told you this already. If you get an exit status of 0, it means that ps found the command you passed as an argument on the -C switch. If you get an exit status of 1, it means that the command was not found. It is when it is NOT found that you want to start it.
You should really go back and read what Firerat and haertig told you. Firerat explained the exit status test problem you had, and haertig pointed out the inconsistent results you can get with the ps command. Use one of the alternative techniques that one or the other of them mentioned.
Due to a large amount of input from you guys, I have managed to get the script working correctly. Thank you!
I am just waiting for the half hour marker to pass to see if the cronjob is working, but to be quite frank, that will lmost certainly be for a different thread. Cronjobs have many guides to it, so a little reading and tinkering should see me right.