Any Improvements to this process killing script? 
Author Message
 Any Improvements to this process killing script?

Hello,

I wrote this clunky shell script with awk in it...Anyone have any
suggestions to pretty it up?  Wasn't sure how to combine the awk
commands.

Thanks, Michael Sims.

#!/bin/sh
cat /dev/null > pstokill
cat /dev/null > clean.rpt

echo Killing all Zombie processes...
echo Killing all Zombie processes...>clean.rpt
ps -Alf | awk '$2~/Z/ {print "kill -9 " $4 }'>> pstokill
ps -Alf | awk '$2~/Z/ {print "User: "$3 " ProcID: " $4 " Application "
$9}'>> clean.rpt

echo Killing all F45run not assigned to pts
echo Killing all F45run not assigned to pts>>clean.rpt
ps -Alf | grep f45run | grep \? |awk '{print "kill -9 "$4}'>>pstokill
ps -Alf | grep f45run | grep \? |awk '{print "User: "$3 " ProcID: "
$4 " Application " $14" " $15" " $16}'>>clean.rpt

echo Killing all Runmenus not assigned to pts
echo Killing all Runmenus not assigned to pts>>clean.rpt
ps -Alf | grep runmenu50 | grep \? |awk '{print "kill -9 "$4}'>>pstokill
ps -Alf | grep runmenu50 | grep \? |awk '{print "User: "$3 " ProcID: "
$4 " Application " $15}'>>clean.rpt

chmod 755 pstokill
./pstokill
#!fin!

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 31 Mar 2002 03:00:00 GMT  
 Any Improvements to this process killing script?

Quote:

> Hello,

> I wrote this clunky shell script with awk in it...Anyone have any
> suggestions to pretty it up?  Wasn't sure how to combine the awk
> commands.

> Thanks, Michael Sims.

> #!/bin/sh
> cat /dev/null > pstokill
> cat /dev/null > clean.rpt

> echo Killing all Zombie processes...
> echo Killing all Zombie processes...>clean.rpt
> ps -Alf | awk '$2~/Z/ {print "kill -9 " $4 }'>> pstokill
> ps -Alf | awk '$2~/Z/ {print "User: "$3 " ProcID: " $4 " Application "
> $9}'>> clean.rpt

> echo Killing all F45run not assigned to pts
> echo Killing all F45run not assigned to pts>>clean.rpt
> ps -Alf | grep f45run | grep \? |awk '{print "kill -9 "$4}'>>pstokill
> ps -Alf | grep f45run | grep \? |awk '{print "User: "$3 " ProcID: "
> $4 " Application " $14" " $15" " $16}'>>clean.rpt

> echo Killing all Runmenus not assigned to pts
> echo Killing all Runmenus not assigned to pts>>clean.rpt
> ps -Alf | grep runmenu50 | grep \? |awk '{print "kill -9 "$4}'>>pstokill
> ps -Alf | grep runmenu50 | grep \? |awk '{print "User: "$3 " ProcID: "
> $4 " Application " $15}'>>clean.rpt

> chmod 755 pstokill
> ./pstokill
> #!fin!

I personally like a single task rather tham multiple so I would use an
approach similar to the following:

#!/bin/sh
cat /dev/null > clean.rpt

echo Killing all invalid processes...
echo Killing all invalid processes...>clean.rpt
ps -Alf | \
   awk 'BEGIN   {o="clean.rpt"}
        $2~/Z/  {print "kill -9 " $4
                 print "Z User: "$3 " ProcID: " $4 " Application " > o
                 next}
        /F45run.*?/{print "kill -9 "$4
                    print "F User: "$3 " ProcID: " $4 \
                          " Application " $14" " $15" " $16 > o
                    next}
        /runmenu50.*?/{print "kill -9 "$4
                       print "R User: "$3 " ProcID: "> $4 \
                             " Application " $15 > o
                       next} ' \
   sh

There are several notes:

    First, because I don't know what system you are running, the
guards      for the last two section might have to be ?.*F45run and
?.*runmeuu50

    Second, the processes are killed (and the report) in a
diffrerent        order than your script

    Third, the next statements are probably unnecessary but they
make        intent clear

--
Bob Stearns
University of Georgia

(706)542-5110



Sun, 31 Mar 2002 03:00:00 GMT  
 Any Improvements to this process killing script?

% I wrote this clunky shell script with awk in it...Anyone have any
% suggestions to pretty it up?  Wasn't sure how to combine the awk
% commands.

I would do this in one big awk script, because I'm like that.
In any case, there's no need for more than one execution of ps.
If you don't mind losing the categorisation, you could do this:
 ps -Alf | awk '
   function print_kill()
   {
      print "kill -9 " $4 > "pstokill"
   }

   function print_log()
   {
      print "User: "$3 " ProcID: " $4 " Application " $9 > "clean.rpt"
   }

   $2 ~ /^Z/ { print_kill(); print_log() }
   /f45run/ && /\?/ { print_kill(); print_log() }
   # and so on, although it seems like my print_log function is not
   # adequate
   '

If you insist on grouping all the kills together, you can stick them
all in a few arrays and print them at the end of the run. I'll do
that at the bottom.

% #!/bin/sh
% cat /dev/null > pstokill
% cat /dev/null > clean.rpt

I would drop these since the files will be overwritten by the first
print statement, anyway.

% echo Killing all Zombie processes...
% echo Killing all Zombie processes...>clean.rpt
% ps -Alf | awk '$2~/Z/ {print "kill -9 " $4 }'>> pstokill
% ps -Alf | awk '$2~/Z/ {print "User: "$3 " ProcID: " $4 " Application "
% $9}'>> clean.rpt

This is a bit pointless. Zombie processes are already gone, they're just
waiting for their parents to bury them. Kill the parent processes if you
like. I would drop this. The rest of the script is fine, except that
it calls ps 4 times, grep 8 times, and awk 4 times. The pstokill script
starts another shell and calls kill 1 time for everything you want to kill.
Also, it uses the -KILL switch to kill, which is not really desirable. You're
better off using default signal (-QUIT), which should allow the process to
exit cleanly.

I would create an awk script like this:
 #!/usr/bin/awk -f
 BEGIN {
   cmd = "ps -Alf"
   while ((cmd | getline) > 0) {
     # put the lines that match grep f45run | grep \? in an array keyed
     # on the process id
     if (/f45run/ && /\?/)
        f45run[$4] = "User: " $3 " ProcID: " $4 " Application " $14 " " $15 " " $16
     # put the lines that match grep runmenu50 | grep \? in another array keyed
     # on the process id
     else if (/runmenu50/ && /\?/)
        runmenu50[$4] = "User: " $3 " ProcID: " $4 " Application " $15 # $14?
   }
   close(cmd)

   # now, list all the rogue f45run processes
   killcmd = "kill"
   {*filter*}killcmd = "kill -9"

   print "All f45run not assigned to pts" > "clean.rpt"

   for (pid in f45run) {
     print f45run[pid] > "clean.rpt"
     killcmd = killcmd " " pid
     {*filter*}killcmd = {*filter*}killcmd " " pid
   }

   # if there were any, crush them like miserable bugs
   if (killcmd != "kill") {
     print "Killing all f45run not assigned to pts"
     # try to kill them nicely, but if they won't behave ...
     system(killcmd "; sleep 1;" {*filter*}killcmd ">>/dev/null")
   }

   # and do the same for runmenu50
   killcmd = "kill"
   {*filter*}killcmd = "kill -9"

   print "All runmenu50 not assigned to pts" > "clean.rpt"

   for (pid in runmenu50) {
     print runmenu50[pid] > "clean.rpt"
     killcmd = killcmd " " pid
     {*filter*}killcmd = {*filter*}killcmd " " pid
   }

   if (killcmd != "kill") {
     print "Killing all runmenu50 not assigned to pts"
     system(killcmd "; sleep 1;" {*filter*}killcmd ">>/dev/null")
   }
 }

This calls awk 1 time, the shell 3 times, and ps 1 time. It also goes
to sleep for a couple of seconds because I'm basically a pragmatic wimp.
--

Patrick TJ McPhee
East York  Canada



Mon, 01 Apr 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Thread#kill doesn't kill processes inside a thread

2. Killing processes without using 'kill'

3. Child process on crashing kills it parent process

4. CMM and Process Improvement Workshops - interactive and fun!

5. How can kill all child processes without killing parent process ?

6. Tcl Test Script Data – Database Advice (Test Automation Improvements)

7. killing processes

8. Kill a Process before Open Files

9. Find & Kill A Process

10. killing a Rexx process in OS/2

11. Help with killing OS2 Process

12. kill a process or application within rexx

 

 
Powered by phpBB® Forum Software