Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

running program?

by rsiedl (Friar)
on May 25, 2006 at 01:55 UTC ( [id://551484]=perlquestion: print w/replies, xml ) Need Help??

rsiedl has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,

I'm running a program on a fedora box which keeps dying. the program is written in C++ and open source.
What i would like to do is write a "wrapper" which monitors if the program is running or not and restarts it if it has died (until i learn C++ and fix the original prob).
I'm guessing something like:
#!/usr/bin/perl use strict; while (1) { my $program = `ps aux | grep PROGRAM`; #some code to determine if the result shows a running program and se +t $program to 1 if ($prgram) { sleep 60; } else { `PROGRAM`; } }
does that look like a good way to start or can someone suggest something better?

cheers,
reagen

updated to reflect jhourcle's notes about while and else. didnt put much thought into the code before posting and it shows :)

Replies are listed 'Best First'.
Re: running program?
by Zaxo (Archbishop) on May 25, 2006 at 02:34 UTC

    Here's a simple launcher daemon,

    #!/usr/bin/perl use Proc::Daemon; Proc::Daemon::Init; my @args = qw/whatever/; while (1) { defined (my $cpid = fork) or sleep(10), next; $cpid and wait, next; exec '/path/to/PROGRAM', @args; exit -1; }
    That forks off a child instance of PROGRAM. The parent sleeps until awakened (that's what wait does) by SIGCHLD, which says PROGRAM has exited. Then the parent does it all over again. The exit call after exec is a safety in case exec fails. Without it, the daemon becomes a fork bomb when it can't find or execute PROGRAM for some reason.

    You may want to do something a little different based on how PROGRAM ends itself. Just assign the list return of wait to an array and decode according to the docs.

    Like any daemon, this is an independent cuss -- you have to signal it with kill or system /bin/kill to make it stop. You can log out of the system and this will keep on chugging along.

    After Compline,
    Zaxo

Re: running program?
by Tanktalus (Canon) on May 25, 2006 at 03:55 UTC

    I'm kinda curious why all the work?

    perl -e 'system(@ARGS) while 1' your program with args here
    The system function is merely a glorified wrapper around fork and exec anyway, complete with a wait. If you want it to daemonise and ignore signals, just add a few characters:
    nohup perl -e 'system(@ARGS) while 1' your program with args here &
    Ok, that's not quite the same as daemonising, but it's often close enough for most purposes.

    Now, if the program you're trying to monitor goes and daemonises itself, well, then you have a completely different problem which I don't think anyone has addressed. In that scenario, you probably want to use pgrep (which solves the "grep finding itself" problem for you) in a similar structure to what you have. However, don't use backticks when system will do. Just a bad idea.

Re: running program?
by ioannis (Abbot) on May 25, 2006 at 03:21 UTC
    A quick solution is to run your program under init(1), who will rerun it when the progam fails, or stop it completely if the program keeps respawning too quickly. See inittab(5) and init(1) for details.
Re: running program?
by jhourcle (Prior) on May 25, 2006 at 02:04 UTC

    Have you tried doing the ps | grep ? You'll find that it doesn't do what you expect, and will always be true:

    slightly:~/desktop oneiros$ ps -aux |grep PROGRAM oneiros 840 0.0 0.0 27352 424 p1 S+ 9:57PM 0:00.00 +grep PROGRAM

    What you want is called a 'watchdog' program (sometimes called 'wxdog', depending on the application)

    Oh -- and while doesn't take an else ... you need an if inside the while ... and you'll want to sleep so you don't suck down all of the CPU.

    But the better solution is to find why the problem is dying.

      There's a trivial way to get around ps|grep finding itself, though:
      $ ps aux | grep [P]ROGRAM $
      Agreed on everything else.

        Note that you may need to quote the brackets to keep them from being interpreted or eaten by your shell.

        $ ps xuaw | grep '[p]rogram' $
Re: running program?
by sgifford (Prior) on May 25, 2006 at 05:29 UTC
Re: running program?
by izut (Chaplain) on May 25, 2006 at 13:24 UTC

    Why don't you use mon? It's written in Perl and extensible. Don't reinvent the wheel.

    Igor 'izut' Sutton
    your code, your rules.

Re: running program?
by cdarke (Prior) on May 25, 2006 at 07:19 UTC
    As everyone else says, plus. Perl has a grep:
    @proc = grep /PROGRAM/, `ps aux`
    ps can be replaced by various Proc:: modules (if you have time).
    Tried running your C++ program under strace?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://551484]
Approved by Moriarty
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-19 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found