LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to see if a process ir running, if not start it (https://www.linuxquestions.org/questions/programming-9/script-to-see-if-a-process-ir-running-if-not-start-it-339618/)

ncsuapex 07-03-2005 12:20 PM

Script to see if a process is running, if not start it
 
Ok,

I lifted this script off a guys blog and it doesnt seem to work 100%. I have not yet asked him for help, but if I can't get any help otherwise I guess I'll see what he says.


The script is supposed to check if a process is running, if it is, it's supposed to do nothing, if the process is not running it's supposed to start it. The second part of the script works 100%. The bad part is, it starts the process whether or not the process is running, so I have a cron job that runs this script every 10 minutes. So when I go back in an hour or so to check out many process's are running. There is a process started every 10 minutes. My question is what's wrong with this script that it starts the process even though there is one running?


script:


#!/usr/bin/perl

# if your prgram has the string "grep" in the name or in the path
# this program won't work.

open PROS, "ps -ef|grep process";


while ($line = <PROS>){
unless ($line =~ m/grep/){
#print "it is running\n";
exit;
}
}

#print "it isn't running\n";
exec 'nohup /path/to/my/script/./script &';

I am running Debian 2.6.8. The script was written in VI and saved with the permissions 755. perl is located in /usr/bin/perl.

Dark_Helmet 07-03-2005 12:36 PM

You have replaced "process" with the actual name of the process, as it appears in the ps command, right?
Code:

open PROS, "ps -ef|grep process";
For example, if you wanted to keep the CUPS printing daemon active, that line would need to be changed to:
Code:

open PROS, "ps -ef|grep cupsd";

ncsuapex 07-03-2005 12:44 PM

Correct, the word process in the ps -ef | grep process command is the name of the process I'm wanting to work with here. I just changed it posting purposes.



If I run the ps -ef | grep process command on its on, it comes back with the correct results. What I'm looking for is why does it start the process even though it's already running? It should A) Check to see if the process is running then B) start it if it's not running or C) do nothing if it is running.

Dark_Helmet 07-03-2005 12:53 PM

I wasn't looking carefully enough.

Try changing that same line to this:
Code:

open PROS, "ps -ef|grep process |";
You need the vertical bar at the end to signify the input is coming from a command output.

ncsuapex 07-03-2005 01:27 PM

That seemed to have worked. I need to test it some more. Odd thing is, it hade the pipe at the end like you suggested and didn't work so I took it out.


All times are GMT -5. The time now is 10:36 PM.