There are certain user environments in which we have to login during certain performance testing and and kill all the process running in that environment.

The environment names are like rswrk01, … up to rswrk98. and this is how we login.

sudo su - rswrk96

It doesn't prompt for any password and straightaway logs in.

Is it possible to automate a script for killing all the running processes in all the environments, instead of logging in manually and killing the processes?

I am not the root user by the way.

$ uname -a
HP-UX rcihp145 B.11.23 U 9000/800 3683851961 unlimited-user license

So rswrk01 and co are users. An occasionally-useful of kill is that if you pass -1 as the PID, the signal is sent to all processes that the signal sender has permission to send signals to. In other words, for a non-root user, kill -$sig -1 sends the signal to all processes running as that user. This includes the killer, but the delivery is atomic, so all concerned processes do get a signal.

Now all you have to do is put this in a loop, and you're done. Since formatting numbers with leading zeroes is a bit of a pain, a not very nice-looking but convenient trick is to put a leading 1 (i.e. count from 101 to 198 rather than 1 to 98) and strip that off.

while [ $i -le 98 ]; do
  sudo su - rswrk${i#1} kill -KILL -1

Or in ksh:

for ((i=101; i<=198; i++)); do
  sudo su - rswrk${i#1} kill -KILL -1

Or in bash or zsh:

for i in {01..98}; do
  sudo su - rswrk$i kill -KILL -1
  • +1 very good answer indeed, I like it and I hope it works when i test it – munish Jan 18 '12 at 13:10

sudo -u rswrk96 -i killall ... should be the same as doing su - rswrk96 and then killall ....

What's left is generating the sequence 00-96. This should work:

for i in $(seq -w 00 96); do sudo -u rswrk$i -i killall ...; done

Now that I think of it, you could probably just do

for i in $(seq -w 00 96); do sudo killall -u rswrk$i; done

Don't need to login.

  • your answer seems reasonable, but there is no pkill in hp unix I gave which pkill and nothing returned – munish Jan 17 '12 at 14:41
  • by the way, what is that -i used before pkill does – munish Jan 17 '12 at 14:44
  • any other ideas people – munish Jan 17 '12 at 14:45
  • 1. OK, I replaced it with killall. See man killall to read what options you can put there (an idea: killall -u rswrk$i would kill all processes of that user). 2. The -i argument to sudo is the same as the - argument to su: it runs the user's login scripts, which create its environment. – angus Jan 17 '12 at 14:49
  • $ sudo -u rswrk96 usage: sudo -V | -h | -L | -l | -v | -k | -K | [-H] [-S] [-b] [-p prompt] [-u username/#uid] -s | <command> this is what i get when i give sudo -u instead of sudo su - donno why – munish Jan 17 '12 at 15:01

kill -9 -1 will simply revoke all the processes running on behalf of the command executing user, even the shells. You can try this with sudo to kill other user's processes. sudo -u user1 kill -9 -1

Or just simply use -u flag to pkill command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.