home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!hri.com!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!dkuug!hillkomm.dk!tfj
- From: tfj@hillkomm.dk (Torben Fjerdingstad)
- Newsgroups: comp.unix.shell
- Subject: zapuser script questions
- Keywords: How do I remove last argument from a variable?
- Message-ID: <1992Dec16.083042.600@hillkomm.dk>
- Date: 16 Dec 92 08:30:42 GMT
- Organization: Hilleroed Kommune
- Lines: 73
-
-
- I want to make at script to kill all processes attached to at specific
- users terminal.
-
- I also want to be sure that the users running applications get a fair
- deal.
-
- Therefore i tried `ps -e|grep <tty> | sort -n -r' to get the child
- processes first in the kill queue.
-
- That did not work well. Some parents die before their childen.
- I often got messages from kill: xxxxx: no such process and user
- applications left stuff hanging around.
-
- THEN I got an idea: How about tsort? That damned thing I fought with,
- trying to build a curses library for Minix.
-
- My questions are below the script:
-
- #!/bin/sh
- # Assuming these field from a `ps -fe' (sys5r3.2):
- # ps: UID PID PPID C STIME TTY TIME COMMAND
- # awk: $1 $2 $3 $6
- if [ $# -ne 1 ]
- then
- echo "usage: $0 <tty>"
- exit 1
- fi
- TTY="$1"
-
- SORTED_PROCS=`ps -fe|\
- awk '$6 == tty { printf("%s %s\n",$2,$3)}' tty="$TTY" - | tsort`
- # (thanks to the c.u.q faq list)
-
- echo $SORTED_PROCS
- echo "I guess you see pid 1 (init) as the last one."
-
- # Now the hard skipping of last arg.
- NUMPROCS=`echo $SORTED_PROCS | wc -w`
-
- KILL_LIST=`for arg in $SORTED_PROCS
- do
- NUMPROCS=\`expr $NUMPROCS - 1\`
- if [ $NUMPROCS != "0" ]
- then
- echo $arg
- fi
- done`
-
- echo killing $KILL_LIST
- # enable after test:
- #for proc in $KILL_LIST
- #do
- # kill -1 $proc || kill -15 $proc || kill -9 $proc
- #done
- # END
-
- Questions:
- ==========
- The KILL_LIST loop is ugly. It's only purpose is to skip the last argument
- of $SORTED_PROCS. How do I do that simple? Can it be done without external
- commands?
-
- What does tsort actually do? Does it really work here?
-
- Any comments?
-
- Please email, and I will summarize.
-
-
- --
- Torben
- tfj@hillkomm.dk
-