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

I am working on a script to automate some process checking and basically want the script to check processes and then if one is not running, start it.

There are several instances of Apache for example that are running for various groups in our company. So I envision a for/each loop. This loop would check each process in turn and then if the ps command returns FAILURE, then it would launch that start command associated with that process.

The question here is: Would the best approach be a couple of arrays with the cmds in them? The way to look at each process on this box is the same save the item we are grepping for.

Help to do this in most efficient way is appreciated.

Replies are listed 'Best First'.
Re: Efficient Automation
by QM (Parson) on Apr 27, 2015 at 15:42 UTC
    Your idea is OK. Let's see some code?

    I would create a cron entry for each process, to start it if it's not running. I vaguely recall some "run lock" switch, so if a process is running, it will not start another.

    Perhaps someone else can refresh my memory on this?

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: Efficient Automation
by karlgoethebier (Abbot) on Apr 27, 2015 at 18:32 UTC
    "...a script to automate some process checking ... to check processes...start it."

    This is the classic task for Nagios.

    See Eventhandlers for details.

    The bash example provided at the same place can be easily ported to Perl.

    For a fork of Nagios with a more modern GUI and a lot of other useful features please see Re: perl module help.

    But please note: The setup of the "old" Nagios is much easier.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Efficient Automation
by MidLifeXis (Monsignor) on Apr 27, 2015 at 15:45 UTC

    Efficiency in what domain? Execution time is probably not a large concern (imo), because the time that it would take to start the Apache process will likely outweigh any amount of perl code that would be doing the checking.

    I would be asking myself about the most efficient way to manage the maintenance of these lists. Does the set of Apache processes change? Do you need to allow other people to manage this list? Does this list of Apache processes need to be able to be moved around to different back ends?

    Your solution for storage of this list may end up being a simple my @host_list, a configuration file with the necessary information that can be re-read as necessary, all the way up to a DB (small as it may be) where each back-end is able to start and stop Apache servers as the configuration changes.

    --MidLifeXis

Re: Efficient Automation
by Perlbotics (Archbishop) on Apr 27, 2015 at 19:59 UTC

    If you happen to use Solaris, have a look at SMF (Service Management Facility). You can control the service (start/stop/etc.) and define a restart policy.

    CPAN has to offer Supervisor::Process, Ubiq, Proc::Launcher::Supervisor, etc.