How to toggle a process’ execution

Would you like to pause a process and then resume it again?

Like when you are developing your application, that uses some kind of code watching / hot-reloading to restart when you edit a file, but you don’t want to restart it for some reason?

Maybe you are debugging it in the browser (and want to avoid a reload), or you are editing it’s migration files and you don’t want it to execute them just jet.

Or you might be doing something completely different, but still: something is the same in me and all of you who are still reading this post: we all have a deep psychological need, something we never dared to tell anyone: We would like to toggle the execution of a process.

Let me show you my very simple script for doing just that:


if [ $(cat /proc/$(pgrep -f "$1" | head -n1)/stat | cut -d ' ' -f 3) == "T" ]; then
    echo "Resuming $1";
    notify-send "Resuming $1"
    pkill -CONT  "$1"
    echo "Pausing $1";
    notify-send "Pausing $1"
    pkill -STOP  "$1"

How does it work?

Linux supports sending different ‘SIGNALS’ to processes, when you press CTRL+C that is sending a signal, when you do “killall $process” that is one, and when you do it with -9 that’s a different one.

For our current script, we are interested in these two signals:


When you send SIGSTOP to a process, the OS immediately stops it’s execution, but the process does not exit or quit in any way, only it’s execution is stopped. Later, you can send SIGCONT to the same process, and it will continue right on the beat on which it was stopped.

The first line just queries the process’ status to see if it is stopped (paused) or not, and based on that it decides what to do.

Dissecting the first line from the inside out, let’s see whats going on here:

  • $1
    • This is the first argument for the script.
  • pgrep -f “$1”
    • This returns the PID for a process’ name.
  • $(pgrep -f “$1” | head -n1)
    • Here we are making sure, we only care about the first result
  • cat /proc/$(pgrep -f “$1” | head -n1)/stat
  • cat /proc/$(pgrep -f “$1” | head -n1)/stat | cut -d ’ ’ -f 3
    • This just returns the third field, a single letter in which we are ultimately intrested:
      • T means STOPPED, then we’ll resume it
      • Or not T and we’ll pause it

The two pkill command is the responsible one for sending the required signal to the process.

And a final touch: notify-send is a convenience tool to show a notification on today’s Linux desktops. You might need to install libnotify-bin to use it.

Extra tip

You can add this script to be executed on a keystroke, for example, this is how you do it in XFCE:

After this, you will have a keystroke that pauses and resumes your process of choice. I’m using it in one of our applications that we are developing, tell me, where will you use it?