Script to see if a process ir running, if not start it
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.