5

I want to kill all processes on a system except for PID 1.

I am currently using pgrep -l . | awk "{if (\$1 != 1) print \$1}" | xargs -n 1 kill -s INT.

awk is used to exclude the process with PID 1

If I run the command on alpine linux (which uses sh), I get:

# pgrep -l . | awk "{if (\$1 != 1) print \$1}" | xargs -n 1 kill -s INT
kill: can't kill pid 265: No such process

I believe PID 256 is from awk.

Is there a clean way (using utilities that should be available across most linux systems) to kill all processes except for PID 1?

  • 1
    That simply makes no sense. Many processes running are essential. What are you really trying to achieve with this? – Julie Pelletier May 17 '16 at 5:13
  • I am using this to develop within a docker container. When code is updated, I want to kill all processes (such as tests that are currently running) and then rerun those tests. – F21 May 17 '16 at 5:14
  • Since you apparently don't want to reboot, you should keep track of the processes you create that need to be restarted. – Julie Pelletier May 17 '16 at 5:20
  • 1
    Why not restart the container instead? – Stephen Kitt May 17 '16 at 5:23
  • 1
    complexity is a problem that scripts can solve. – cas May 17 '16 at 10:08
6

On Linux at least,

kill -- -1

Will send the SIGTERM signal to every process it can except for the calling process (so the process running that kill command which could be the shell if kill is built-in there (it usually is on POSIX shells) or the process running a standalone kill command) and the process of pid 1.

Note that it does it as part of the kill() system call, so it's more reliable than using commands like pkill or killall (or the traditional killall command sometimes found as killall5 on Linux traditionally used for that) that first list the processes and then kill them as those would miss the processes that have been spawned in the mean time.

So it sounds like exactly what you want.

trap '' TERM # ignore SIGTERM ourselves though it wouldn't be needed 
             # in most shells
kill -- -1
sleep 1 # leave processes some time to exit cleanly on SIGTERM
kill -s KILL -- -1 # removes the remaining processes or those 
                   # that have started since without giving them
                   # a chance to clean-up.
exit

Should kill everything but the process of id 1.

You can experiment with it by running:

unshare --mount-proc -mpf

That starts a shell in a separate pid and mount namespace (with a new /proc (as used by ps/pkill/killall)) where that shell has pid 1.

Outside of Linux, kill -- -1 should work on every system at killing most processes, but the list of processes that are exempt from the killing can vary from system to system.

0

Can you? Yes. Should you? under nearly all circumstances, no. But:

init ignores SIGTERM on Linux, explicitly. So you could simply do killall -TERM '*'.

  • 1
    Running killall -TERM * doesn't kill anything for me: killall: bin: no process killed killall: src: no process killed. ps confirms that no process was killed. – F21 May 17 '16 at 6:48
  • 1
    F21: That's because shell expansion happens. Try doing killall -TERM '*' – theblazehen May 17 '16 at 7:29
  • @theblazehen, at least with the killall from psmisc on Linux, killall '*' returns with a *: no process found message as arguments are not meant to be patterns. With that version of killall however, you can use -r for the argument to be taken as a regexp, so it behaves more like pkill (the companion command of pgrep used by the OP). So killall -r '^' or pkill '^'. SIGTERM is the default signal, you don't need -TERM. – Stéphane Chazelas May 17 '16 at 10:22
  • There are many implementations of init on Linux, whether they ignore SIGTERM will vary with implementations. From a grep SigIgn /proc/1/status on my system using systemd, SIGPIPE is the only signal that it ignores. It doesn't matter anyway, as for processes of pid 1 on Linux, signals are ignored if they are not otherwise handled by the application. – Stéphane Chazelas May 17 '16 at 10:34

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.