In addition to starting commands, the shell provides basic job control functions for processes. For shell sessions, such as interactive sessions on Stampede2, it can be useful to see and control processes that run for longer times. Job control allows the user to stop, suspend, and resume jobs from within the shell. This is useful if you have a program that runs longer than desired, does not complete due to a bug, or has other problems. From within a shell session, the ps command will show the current processes running in your shell session.

$ ps
  PID TTY          TIME CMD
 4621 pts/7    00:00:00 bash
32273 pts/7    00:00:00 ps

From within a running process, using Ctrl C will send an interrupt signal to the process, which will usually cause it to terminate (I/O and other factors can block the interrupt from taking effect immediately). If you have executed a long-running program that you want to complete, but you want to do other things in the same shell while it runs (rather than starting a new shell on the system), you can suspend the process by pressing Ctrl Z. It can be resumed in the background with bg. Similarly, a process can be invoked and immediately sent to the background by adding an & at the end of the command:

A running job can be brought to the foreground with fg:

While using job control in the shell you can also use the jobs command to display currently running jobs, similar to ps.

In the example above, we invoke longscript.sh & and immediately send it to the background. The jobs command shows the list of running jobs under shell control. Using fg, we can bring "longscript.sh" back to the foreground. See the manual page (man fg) for details about how to specify which job will be brought to the foreground if there is more than one background job.

You can also use the top command to view details about running processes. The program htop is a common, more interactive, alternative to top that is not installed by default on most Linux systems, but is worth exploring. And finally, use the kill command followed by a PID to stop a process. For more information on these job control commands, see their respective man pages.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement