Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # standard script to revive hanging processes
  2.  
  3. # cleanup and die function
  4.  
  5. sub cleanup($file, $reason) {
  6.  
  7. ($file, $reason) = (shift, shift);
  8.  
  9. print "unlink: $file\n";
  10.  
  11. unlink $file; print $reason."\n"; exit;
  12. }
  13.  
  14. # start-background-process helper
  15.  
  16. sub background($cmdline) {
  17.  
  18. ($cmdline) = (shift);
  19.  
  20. print "background: $cmdline\n";
  21.  
  22. defined($pid = fork) or die "$!"; return if $pid;
  23.  
  24. setsid or die "$!";
  25.  
  26. exec "$cmdline"; exit;
  27. }
  28.  
  29. # main process loop, make it happen
  30.  
  31. $cmd = shift or die "no command specified!";
  32.  
  33. print "revive: starting ($cmd)\n";
  34.  
  35. # store current process list
  36.  
  37. $tmp = "/tmp/keepalive".time();
  38.  
  39. `ps -waxopgid,args > $tmp`;
  40.  
  41. # revive (kill and restart) specified command
  42.  
  43. open(LIST, "<$tmp") or &cleanup($tmp, "$!");
  44.  
  45. while(<LIST>) {
  46.  
  47. next if /revive/;
  48.  
  49. next unless /([0-9]+).*($cmd.*)$/;
  50.  
  51. # running, revive it (kill and restart)
  52.  
  53. ($pid, $cmd, $revived) = ($1, $2, ++$revived);
  54.  
  55. print "restarting: $pid\n";
  56.  
  57. kill -15, $pid; &background("$cmd");
  58. }
  59.  
  60. &cleanup($tmp, "finished") if $revived;
  61.  
  62. &background("$cmd") unless $revived;
  63.  
  64. &cleanup($tmp, "finished");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement