10

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?

6 Answers 6

15

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
2

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.

1
  • Yes. You can add -e '/login/d' and remove -e '/bash/d' . Then you even can remount root FS as RO. I mean case, when you are in non-X11 root terminal. P.S.: If you are in X11, then logout first. Commented May 2, 2019 at 13:10
1

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!

1
  • 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
    Commented Sep 30, 2009 at 20:19
1

Try:

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

I used the approach of @RoyRico - can't comment on that post because of lacking reputation - and adjusted it to my system. Because of some different configuration that one did not work off-the-shelf.

ps -U myUserName | egrep -v "ssh|screen|grep|bash|systemd|(sd-pam)|ps" | awk '{print $1}' | tail -n +2 | xargs -t kill

Well, I excluded more processes which I did not want to have killed. Second, the PIDs came in the first column so the former 'cut' command was totally wrong-positioned (as a hackerish solution something completely normal and acceptable ;) ). Third, while grep'ing I had a "PID" as a head line which I excluded by using tail.

0

To add on to existing answers, the following worked for me.

  • The PID appears in the first column on my machine, hence b1-6
  • Like another poster I included the names of the processes used in the command which might otherwise get picked up in the kill command.
  • Include PID in the ignore list to remove the title
  • Including --no-run-if-empty to xargs option suppresses writing the help to the screen if xargs ends up being blank, ie. no processes found.
  • If you put this in a bash script, include bash and the name of the bash script in the list of ignored commands too!

    ps -U myUserName | egrep -v "ssh|screen|PID|ps|grep|cut|xargs|kill" | cut -b1-6 | xargs -t --no-run-if-empty kill

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .