How to pause and resume programs on Unix and Unix-like systems

From ArticleWorld


Many users have little idea about the power of their Unix systems. Although most of them offer user-friendly methods of stopping running processes or monitoring their CPU usage, few offer you the ability to pause and resume the execution of processes.

At any given time, you can pause a running program. That is, literally pause it. It will freeze its execution and stop doing anything. You can then resume it at any given time. This is useful when an application uses some CPU power even when not doing anything special (like the MS Office suite on OS X). Here is how to pause and resume programs.

Steps

  • Open a console window. On most Unix systems, you either get one if you are not logged in to a graphical environment, or you need to open a terminal window. On OS X, the Gnome desktop or BeOS this is called Terminal. Other environments call it Console or something similar. Terminal, Console and any variation on this theme will do.
  • First, we will need to find out the PID of the program we want to pause. The PID is a unique identification number the operating system assigns to a process. Two processes cannot have the same PID, which is why the PID is much more reliable than the name of the program itself (you may have more running instances of the same program: all have the same name, but different PIDs). To do so, use:
ps ax

This will give you a rather long list, so you may want to try:

ps ax | grep [program name]

replacing [program name] with the name of the program. This is case sensitive, so you may need to try more capitalization versions, or search only for a part of the program's name. It will display a table quite similar to the following:

237  ??  R     21:51.95 /Applications/Opera.app/Contents/MacOS/Opera -psn_0_1703937
810  p1  R+     0:00.00 grep Opera

Notice how this found two processes. One is the process of grep which searches for something called "Opera". The other one is of interest to us. The leftmost number is the PID.

  • Pause the process, by typing:
kill -s STOP [pid]

again, replacing [pid] with the PID we found above. Following the example above, we would type:

kill -s STOP 237
  • Resume it using:
kill -s CONT [pid]

Notes

On some occasions, this may cause your application to crash, or, rather, perform strangely when resuming. This sometimes happens because of the way it was written. It is a good idea to save your work before pausing and resuming a program, just to make sure.