--- local/fixproc.orig Sat Apr 20 09:30:13 2002 +++ local/fixproc Sat Mar 6 01:59:59 2004 @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!%%PERL%% # # fixproc [-min n] [-max n] [-check | -kill | -restart | -exist | -fix] proc ... # @@ -129,7 +129,7 @@ # # Timothy Kong 3/1995 -$database_file = '/local/etc/fixproc.conf'; +$database_file = '%%PREFIX%%/etc/fixproc.conf'; $debug = 0; # specify debug level using -dN # currently defined: -d1 @@ -155,6 +155,14 @@ $shell_header = "#!/bin/sh\n"; $shell_end_marker = 'shell_end_marker'; +open(command, "/bin/ps -p $$ |") || die "$0: can't run ps command\n"; +if (split(' ', ) > 4) { + $ps_opts = 'ax'; +} else { + $ps_opts = '-e'; +} +close command; + &read_args(); &read_database(); # &dump_database(); # debug only @@ -203,7 +211,9 @@ $i++; } close (file); - system "chmod +x $file"; + ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, + $atime,$mtime,$ctime,$blksize,$blocks) = stat($file); + chmod $mode | 0111, $file; return file; } @@ -237,8 +247,8 @@ # return code is number divided by 256 $error_code = (system "$tmpfile") / 256; - system "rm $tmpfile"; - return ($fix_failed_error) if ($error_code != 0); + unlink $tmpfile; + return ($cannot_fix_error) if ($error_code != 0); # sleep needed here? return &do_exist ($proc); } @@ -268,7 +278,7 @@ # return code is number divided by 256 $error_code = (system "$tmpfile") / 256; - system "rm $tmpfile"; + unlink $tmpfile; return ($check_failed_error) if ($error_code != 0); # check passed, continue @@ -285,10 +295,12 @@ # do ps, check to see if min <= no. of processes <= max $! = $fixproc_error; - open (command, "/bin/ps -e | /bin/grep $proc | /bin/wc -l |") + open (command, "/bin/ps $ps_opts |") || die "$0: can't run ps-grep-wc command\n"; - $proc_count = ; - if (($proc_count < $min{$proc}) || ($proc_count > $max{$proc})) + @allprocs = ; + close command; + @procs = grep(/$proc/, @allprocs); + if (($#procs < $min{$proc}) || ($#procs > $max{$proc})) { return $check_failed_error; } @@ -305,41 +317,48 @@ # first try kill $! = $fixproc_error; - open (command, "/bin/ps -e | /bin/grep $proc |") + open (command, "/bin/ps $ps_opts |") || die "$0: can't run ps-grep-awk command\n"; while () { - # match the first field of ps -e + if /$proc/ { + # match the first field of ps $ps_opts $! = $fixproc_error; - /^\s*(\d+)\s/ || die "$0: can't match ps -e output\n"; - system "kill $1"; + /^\s*(\d+)\s/ || die "$0: can't match ps $ps_opts output\n"; + kill 15, $1; + } } + close command; # if process still exist, try kill -9 sleep 2; $! = $fixproc_error; - open (command, "/bin/ps -e | /bin/grep $proc |") + open (command, "/bin/ps $ps_opts |") || die "$0: can't run ps-grep-awk command\n"; $second_kill_needed = 0; while () { - # match the first field of ps -e + if /$proc/ { + # match the first field of ps $ps_opts $! = $fixproc_error; - /^\s*(\d+)\s/ || die "$0: can't match ps -e output\n"; - system "kill -9 $1"; + /^\s*(\d+)\s/ || die "$0: can't match ps $ps_opts output\n"; + kill 9, $1; $second_kill_needed = 1; + } } + close command; return ($no_error) if ($second_kill_needed == 0); # see if kill -9 worked sleep 2; $! = $fixproc_error; - open (command, "/bin/ps -e | /bin/grep $proc |") + open (command, "/bin/ps $ps_opts |") || die "$0: can't run ps-grep-awk command\n"; while () { # a process still exist, return error - return $cannot_kill_error; + return $cannot_kill_error if /$proc/; } + close command; return $no_error; # good, all dead }