Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

PERL Monks! Lend me your wisdom - Program Monitor

by jdlev (Scribe)
on Mar 13, 2009 at 14:52 UTC ( [id://750443]=perlquestion: print w/replies, xml ) Need Help??

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

I need to create a program that monitors whether another program is running or not. If it is not, I need it to fire up that program and get it running. How would I do that?

I love it when a program comes together - jdhannibal
  • Comment on PERL Monks! Lend me your wisdom - Program Monitor

Replies are listed 'Best First'.
Re: PERL Monks! Lend me your wisdom - Program Monitor
by bellaire (Hermit) on Mar 13, 2009 at 14:59 UTC
    You should try the Super Search for "process monitor", there are many nodes which discuss this issue for various platforms.

    This link will preload that search.
Re: PERL Monks! Lend me your wisdom - Program Monitor
by JavaFan (Canon) on Mar 13, 2009 at 16:11 UTC
    If you are running a Unix flavour, then there's already a process which can do that. It's called init, and it will always be there. Just add something like the following to your /etc/inittab:
    PM:345:respawn:/path/to/your/program
    and tell init:
    telinit q
    See also man 5 inittab and man 8 init for specific details of your platform.
Re: PERL Monks! Lend me your wisdom - Program Monitor
by Illuminatus (Curate) on Mar 13, 2009 at 15:47 UTC
    Not really a perl question, per se, but OK. If
    1. You are lazy (and if you are a programmer, you probably are)
    2. You are running on *nix
    3. The restart does not have to be immediate
    4. You don't need to have logic to diagnose chronic/constant failures
    Then add a cron job that runs every couple of minutes and starts it if it is not there. Not pretty, but 15 minute's work, tops.
Re: PERL Monks! Lend me your wisdom - Program Monitor
by ig (Vicar) on Mar 13, 2009 at 19:42 UTC

    There are many existing applications, including some written in Perl, that monitor processes. They range from very simple to very sophisticated (and complex). Some of them have options to restart or take other actions if processes are gone or not responding. Some of them keep logs and produce helpful reports. I suggest you investigate some of the existing applications before writing your own. You can google for "process monitor" or "network management system" to get some leads on what is available.

Re: PERL Monks! Lend me your wisdom - Program Monitor
by targetsmart (Curate) on Mar 13, 2009 at 15:00 UTC
    . Re: Memory Leak: Uses 3GB+

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: PERL Monks! Lend me your wisdom - Program Monitor
by nagalenoj (Friar) on Mar 14, 2009 at 05:07 UTC

    There are many ways that we can achieve this. If you are using unix system, I hope the following 2 ways will you

    use strict; use warnings; my $CMD = '/home/blah/my_process'; my $SLEEP_TIME = 1; # check to see if it is running every 5 seconds while(1) { my @pid_array = `ps -ef | grep /home/blah/my_process`; my $pid_count = scalar(@pid_array); if ($pid_count == 1) { # It isn't running, that 1 is for grep. more than one tells you the pr +ocess is alive. system "$CMD"; # run it } elsif ($pid_count > 2) { # There is more than one instance running. # Do appropriate stuff print "many instances are running"; } sleep $SLEEP_TIME;

    The above code won't work all the time, but, you can adopt some changes and use the above to find a process is running or not

    This is another way. Try the below code

    use strict; use warnings; if (kill (0, pid)) { print "process alive"; } else { print "process died"; }

    To know about kill function, try google it.

      FYI...it's windows system, running on windows server 2003...thanks

      I love it when a program comes together - jdhannibal

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found