Pause all userspace processes

 2022-09-07

If you need to debug some kind of monitoring system (or just have some fun), you might want to pause all userspace processes for a certain number of seconds (to measure delays, etc.).

You can easily do this using GDB like this:

gdb_sleep_all.sh
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail

# Select all process IDs that are _not_ children of PID 2, [kthreadd].
pids="$( ps -o pid --no-headers --ppid 2 -p 2 --deselect )"

for pid in $pids; do
	cmdline="$( cat "/proc/$pid/cmdline" | tr '\0' ' ' )" || continue
	echo ------------------------------------------------------------------
	echo "PID: $pid"
	echo "Command line: $cmdline"
	echo ------------------------------------------------------------------
	gdb -p "$pid" -x sleep.gdb -batch &
done

wait

sleep.gdb is a very simple GDB script; it basically sleeps for a determined amount of seconds:

sleep.gdb
shell sleep 10
quit

You can simply run

sudo ./gdb_sleep_all.sh

and all of your userspace processes should be frozen for 30 seconds.

On a couple of servers, this worked quite well; not so well on my laptop with Xfce installed. Obviously, this would require a bit of work to adapt for containers as well. Otherwise, pretty neat, huh?