home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / shell / 5085 < prev    next >
Encoding:
Internet Message Format  |  1992-12-16  |  2.0 KB

  1. Path: sparky!uunet!spool.mu.edu!hri.com!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!dkuug!hillkomm.dk!tfj
  2. From: tfj@hillkomm.dk (Torben Fjerdingstad)
  3. Newsgroups: comp.unix.shell
  4. Subject: zapuser script questions
  5. Keywords: How do I remove last argument from a variable?
  6. Message-ID: <1992Dec16.083042.600@hillkomm.dk>
  7. Date: 16 Dec 92 08:30:42 GMT
  8. Organization: Hilleroed Kommune
  9. Lines: 73
  10.  
  11.  
  12. I want to make at script to kill all processes attached to at specific
  13. users terminal.
  14.  
  15. I also want to be sure that the users running applications get a fair
  16. deal.
  17.  
  18. Therefore i tried `ps -e|grep <tty> | sort -n -r' to get the child
  19. processes first in the kill queue.
  20.  
  21. That did not work well.  Some parents die before their childen.
  22. I often got messages from kill: xxxxx: no such process and user
  23. applications left stuff hanging around.
  24.  
  25. THEN I got an idea: How about tsort? That damned thing I fought with,
  26. trying to build a curses library for Minix. 
  27.  
  28. My questions are below the script:
  29.  
  30. #!/bin/sh
  31. # Assuming these field from a `ps -fe' (sys5r3.2):
  32. # ps:  UID   PID  PPID  C   STIME TTY TIME COMMAND
  33. # awk:  $1    $2    $3             $6
  34. if [ $# -ne 1 ]
  35. then
  36.     echo "usage: $0 <tty>"
  37.     exit 1
  38. fi
  39. TTY="$1"
  40.  
  41. SORTED_PROCS=`ps -fe|\
  42. awk '$6 == tty { printf("%s %s\n",$2,$3)}' tty="$TTY" - | tsort`
  43. # (thanks to the c.u.q faq list)
  44.  
  45. echo $SORTED_PROCS
  46. echo "I guess you see pid 1 (init) as the last one."
  47.  
  48. # Now the hard skipping of last arg.
  49. NUMPROCS=`echo $SORTED_PROCS | wc -w`
  50.  
  51. KILL_LIST=`for arg in $SORTED_PROCS
  52.    do
  53.       NUMPROCS=\`expr $NUMPROCS - 1\`
  54.       if [ $NUMPROCS != "0" ]
  55.          then
  56.          echo $arg
  57.       fi
  58.    done`
  59.  
  60. echo killing $KILL_LIST
  61. # enable after test:
  62. #for proc in $KILL_LIST
  63. #do
  64. #   kill -1 $proc || kill -15 $proc || kill -9 $proc
  65. #done
  66. # END
  67.  
  68. Questions:
  69. ==========
  70. The KILL_LIST loop is ugly. It's only purpose is to skip the last argument
  71. of $SORTED_PROCS. How do I do that simple? Can it be done without external
  72. commands?
  73.  
  74. What does tsort actually do? Does it really work here?
  75.  
  76. Any comments?
  77.  
  78. Please email, and I will summarize.
  79.  
  80.  
  81. -- 
  82. Torben
  83. tfj@hillkomm.dk
  84.