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

Dear Monks of wisdom

I do have the following situation.

A perl script that is running as a monitor for an application on Linux, I named it applMon. It does checks thing like are all processes running. The job here is to do an automatic restart if one of these processes is missing. This is all working so far.

But sometimes the shutdown respectively the startup command, which is run by e.g. qx(shutdown) or qx(startup) from the monitoring script, hangs. For that reason I wrote another script, let’s call it restartCompletenessCheck.

Now I’d like to start the restartCompletenessCheck script before I do run the shutdown on the corresponding application. In the restartCompletenessCheck script I do sleep for 60 seconds to give the application shutdown some time to finish via sleep. Now how can I achieve to run the restartCompletenessCheck script independently from the monitoring script dynamically? I was thinking about implementing fork in the applMon script. But fork runs another copy of the originating script which would then cause problems like start of the application twice and so on.

Do any of wise and astute monks have a concept on how to solve this issue? Thank you for your precious time to spend on such small problems.

p_boarder

Pseudo Code :

script applMon
check();
if check eq negative
Run restartCompletenessCheck();
qx(shutdown);
sleep 60;

checkThatEverythingIsStopped();

if (everThingStopped)
startApplication();
else
kill all application processes still running
checkThatEverythingIsStopped();
startApplication();
else
doWriteLogMessage(everyThinIsOk);



script restartCompletenessCheck
sleep 60 sec;
if (processes from shutdown are still running)
kill(them);
done

  • Comment on Run Perl script from another perl script independently

Replies are listed 'Best First'.
Re: Run Perl script from another perl script independently
by jethro (Monsignor) on Feb 23, 2011 at 11:32 UTC

    Usually fork is combined with exec to replace the originating script with some other script (restartCompletenessCheck in your case) in the forked process. Also the return value of fork signals where you are. So that you can do something else in the copy than in the original. You can make sure with a simple if construct that your copy doesn't start the application again

    If you use fork/exec for the shutdown and start of your application, you could use applMon directly to monitor the success instead of a separat script restartComplemenessCheck

Re: Run Perl script from another perl script independently
by roboticus (Chancellor) on Feb 23, 2011 at 11:54 UTC

    p_boarder:

    I generally try to leverage one of the built in process management tools, and write a reporting application as needed. However, that's not what you asked, so here's what I'd do--I'd use a state machine for each process. So once the process goes to 'running' it won't be modified until its starting timeout ends, etc. Something like:

    my %Jobs = ( Florb => { startup_time=>120, shutdown_time=>45, state=>'stopped', + }, Zooki => { startup_time=>60, shutdown_time=>10, state=>'stopped', + }, ); while (1) { my $desired_state = get_desired_state(); for my $J (keys %Jobs) { my $job = $Jobs{$J}; if ($$job{state} eq 'stopped') { if ($desired_state eq 'running') { $$job{state} = 'starting'; $$job{time} = time; } } elsif ($$job{state} eq 'running') { if ($desired_state eq 'stopped') { $$job{state} = 'stopping'; $$job{time} = time; } } elsif ($$job{state} eq 'starting') { if ($$job{time} + $$job{startup_time} < time()) { $$job{state} = 'running'; } else { if ($$job{time} + $$job{shutdown_time} < time()) { $$job{state} = 'stopped'; } } # doze a little sleep 10; } }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Run Perl script from another perl script independently
by p_boarder (Novice) on Mar 23, 2011 at 17:57 UTC
    Dear Perl-Monks of wisdom

    After a month of not replying to my thread I'd like to report about the solution I finally implemented.

    system("nohup completCheck >/dev/null 2>&1 &");

    This is not the most sophisticated approach, but in spirit of age everything has to be done in short time.

    I have tried to also the fork suggestion, but after end of project. This also worked fine.

    Thank you all for your support.

    Your willingness to learn

    p_boarder