On Sun, Mar 8, 2009 at 21:41, Owen <rc...@pcug.org.au> wrote: >> >> I'd like to monitor a running script and if the script stops running, >> restart it and continue to monitor it. I need a little help from the >> community on the ins-and-outs of the details. But, basically I need >> someone to look at the following code/pseudo-code and give >> suggestions. >> >> 1) Check a script running. >> 2) If it's running, sleep for 30 seconds then check again. >> 3) If it's not running, restart it and continue to check after that. >> >> This is on a Red Hat box, so the first thing would be something like: >> >> While (1) { >> my $process = `ps -ef | grep <process name> | grep -v grep`; >> >> if ($process) { >> sleep 30} else { >> exec (./<process name>) or print STDERR "couldn't exec >> <process name>: $!"; >> } >> } > > > WARNING: Totally untested, but you will get the idea > > > #!/usr/bin/perl -w > > use strict; > > my $program = "<process_name>"; > my $status = `/bin/ps cat | /bin/grep $program`; > > if ( length($status) > 0 ) { > > sleep 30; > > } > else { exec "the_process" } # start program snip
You need a fork before the exec or you will lose the monitor program the first time it restarts the monitored processed: #!/usr/bin/perl use warnings; use strict; my $program = "process_name"; my $continue = 1; $SIG{TERM} = sub { $continue = 0 }; while ($continue) { my $status = `/bin/ps | /bin/grep $program | /bin/grep -v grep`; if ($status) { sleep 30; } else { my $pid = fork; die "could not fork" unless defined $pid; if ($pid) { #give process a chance to start sleep 5; next; } exec $program; #child process replaces self with program } } -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/