Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I was running some processes under a screen session on a remote server. When I tried to kill all those processes by:

pkill -U tim

all my processes are killed including those I don't want to kill (i.e. the screen and ssh connection).

Is there a way to kill all my processes except the screen and ssh connection?

share|improve this question

4 Answers 4

Kinda hackerish:

ps -U tim | egrep -v "ssh|screen" | cut -b11-15 | xargs -t kill

this will kill everything but any ssh or screen processes. Here are the commands explained:

  • ps -U tim -- will obviously, list every process from the user tim
  • egrep -v "ssh|screen" -- will remove lines with ssh or screen processes
  • cut -b11-15 -- will cut the data in columns 11-15 (typically that's where the PID is located
  • xargs -t kill -- will pass all the process ID's to the kill command

You can also use awk, if you're more used to that.

ps -U tim | egrep -v "ssh|screen" | awk '{print $2}' | xargs -t kill
share|improve this answer

Nothing built in that I'm aware of. You could create a script like this:

#!/bin/bash
ps ux | sed -e '/bash/d' -e '/screen/d' | awk '{print $2}' | while read process
do 
  kill $process
done

If there were any other processes you wanted to avoid killing you would just need to add more

-e '/processname/d'

entries to the sed portion. There's probably a cleaner way to handle it, but this will work.

share|improve this answer

If you're killing all your procs a lot, you might want to investigate why you need to do that... but hey, this is all about doing things, not about not doing things.

One easy solution would be to use two userIDs... one for screen and your SSH connection, and the other one for all the processes you'll at some point want to kill off.

This is beyond "hackerish" and into just plain "hack" but it has an added advantage in that any OTHER programs you run as the "connect" user won't get killed when you kill off the other procs. This could include "tails" of error logs and things like that which you might WANT to have left around.

Hope this helps!

share|improve this answer
    
you mean having two userIDs for the remote server? Just curious, can I create another one for myself without being able to switch to root? –  Tim Sep 30 '09 at 20:19

Try:

ps aux | grep ^$LOGNAME | egrep -v 'ps aux|-bash|sshd' | awk '{ print $2 }' | xargs kill -9; ps aux | grep $LOGNAME
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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