Hello all,

I'd like to learn how to use fork properly. I'm monitoring a running process 
and when it stops, I use a script to start it. The script will most likely 
become a cron job. To test everything I created a test script to run as a 
process which is written as follows:

#!/usr/bin/perl

use warnings;
use strict;

while (1) {
    print "Hello, I'm running.\n";
    sleep 10;
}

The monitoring script is as follows:

#!/usr/bin/perl

use warnings;
use strict;

my $ps_check = `ps -ef | grep testScript.pl | grep -v grep`;

if ($ps_check) {
    print "testScript.pl is running.\n";
} else {
    print "testScript.pl is not running. I'll start it.\n";
    my $pid = fork();
    if (!defined($pid)) {
        die "Could not fork: $!\n";
    } elsif ($pid == 0) {
        exec("/home/scripts/utils/develop/ron/testScript.pl");
        exit;
    } else {
        waitpid($pid, 0);
    }
}

I was wondering if this script is written correctly and if there's a way to 
have the monitoring script release the shell and get the prompt back, or is it 
running in the child process shell and that's why it's getting output?


Ron Smith
geeksatla...@yahoo.com
(213)300-9448

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to