LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-03-2005, 12:20 PM   #1
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 43
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.

Last edited by ncsuapex; 07-03-2005 at 12:22 PM.
 
Old 07-03-2005, 12:36 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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";
 
Old 07-03-2005, 12:44 PM   #3
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 43
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.
 
Old 07-03-2005, 12:53 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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.
 
Old 07-03-2005, 01:27 PM   #5
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 43
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
script to check if process is dead or running rspurlock *BSD 6 04-12-2004 11:32 PM
Process start delay when network running andrewb758 Linux - Software 1 02-16-2004 11:01 AM
[script] check for a running process mikshaw Linux - Software 2 01-13-2004 08:33 PM
script at xwin start to kill old process(es) butthead Programming 3 02-01-2002 08:39 AM


All times are GMT -5. The time now is 08:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration