Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # standard script to revive hanging processes
- # cleanup and die function
- sub cleanup($file, $reason) {
- ($file, $reason) = (shift, shift);
- print "unlink: $file\n";
- unlink $file; print $reason."\n"; exit;
- }
- # start-background-process helper
- sub background($cmdline) {
- ($cmdline) = (shift);
- print "background: $cmdline\n";
- defined($pid = fork) or die "$!"; return if $pid;
- setsid or die "$!";
- exec "$cmdline"; exit;
- }
- # main process loop, make it happen
- $cmd = shift or die "no command specified!";
- print "revive: starting ($cmd)\n";
- # store current process list
- $tmp = "/tmp/keepalive".time();
- `ps -waxopgid,args > $tmp`;
- # revive (kill and restart) specified command
- open(LIST, "<$tmp") or &cleanup($tmp, "$!");
- while(<LIST>) {
- next if /revive/;
- next unless /([0-9]+).*($cmd.*)$/;
- # running, revive it (kill and restart)
- ($pid, $cmd, $revived) = ($1, $2, ++$revived);
- print "restarting: $pid\n";
- kill -15, $pid; &background("$cmd");
- }
- &cleanup($tmp, "finished") if $revived;
- &background("$cmd") unless $revived;
- &cleanup($tmp, "finished");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement