...the problem I have with this approach (and with the use
of who(1)) is that their output is not predictable. Some people
still use the BSD version of ps(1), some people use the SysV version,
and some use the GNU version. I presume that on Solaris, you're
using the SysV version unless you've updated your path.
Given that assumption, it is possible to use Perl easily enough
to parse the output and give you the identities of the processes
you hope to kill. And that would look a lot like the classic
shell script that does this task.
I keep thinking (as I write this) that there ought to be a nice
one-liner that does the trick. Perhaps I'll work it out.
---v
Well, I took a crack at it, but the only improvements I was
able to offer is an invocation of ps(1) that renders the output a
little more predicatably.
Here's the code:
#!/usr/bin/perl
$myself = "student";
chomp ($mytty = `tty`); # gets: /dev/pts/5
$mytty =~ s/.dev.//; # gets: pts/5
=preview
We'll run the command: ps -u $myself -o 'pid,tty'
It will produce output like this:
PID TT
419 pts/3
401 pts/3
398 ?
399 pts/3
437 ?
479 pts/5
513 pts/5
1175 ?
=cut
@lines = split /\n/, `ps -u $myself -o 'pid,tty'`;
shift @lines; # gets rid of header line
foreach $entry (@lines) {
($pid, $term) = $entry =~ /^\D*(\d+)\s+(\S+)/;
next if ($term =~ /^[?]/ || $term eq $mytty);
print "kill 9, $pid\n"; # for testing
# kill 9, $pid; # production version
}
With light testing on this system (sol8,perl v5.005), it
gave the results I was hoping for. I don't know if there are
some anomalous listings that might have to be accounted for.
But you will note that it finds processes with a TTY other than
the current one, and kills them all. It would be better to kill
only the process group leader and that would cause the others
to be killed by init.
YMMV |