Skip to content

Lesson 24: Controlling processes

A process is an executing instance of an application (or task), uniquely identified by a given integer, the process identifier (or PID for short). In a multi-user Linux system, processes will be owned by different users, mainly root.

Starting Processes

Every time you execute a command, a process and PID is generated. To execute a process in the background of the current terminal, add character & to the end of the command.

As an example, lets use the sleep command to imitate a running process (sleep suspends program execution for a specified time). Note: your PID may be different to the example below:

[learner planets]$ sleep 1000s &
[1] 5522
[learner planets]$

The number between square brackets is the terminal's local job number, which increments every time a job gets sent to the background. To reset this number, close and reopen the terminal. The PID is the next number (5522 in the example above).

Display Running Processes

To view a list of running processes, run one of the following commands:

  • ps - a snapshot of currently running processes
  • top - a live table of processes, cpu and memory utilisation

Let's view our sleep process with both commands, noticing the PID 5522:

[learner planets]$ ps
  PID TTY          TIME CMD
 5488 ?        00:00:00 bash
 5512 ?        00:00:00 bash
 5522 ?        00:00:00 sleep
 5528 ?        00:00:00 ps

[learner planets]$ top -u $USER
...
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 5488 learner   20   0  116380   2884   1572 S   0.0  0.0   0:00.02 bash
 5512 learner   20   0  115444   1932   1544 S   0.0  0.0   0:00.05 bash
 5522 learner   20   0  107952    352    276 S   0.0  0.0   0:00.00 sleep
 5534 learner   20   0  159908   2304   1548 R   0.0  0.0   0:00.01 top

By default, top shows a list of all processes by all users. Passing the -u switch filters by user. In this case, we used $USER to reference the currently logged in user. Press Ctrl-c to exit out of top.

Terminal Process Control

Processes can be sent to either the foreground or background anytime during execution by using commands fg %JOBID and bg %JOBID, respectively. To find out the JOBID, run the jobs commands.

Let's use our currently running sleep command to demonstrate terminal process control. Firstly, find the JOBID of the process:

[learner planets]$ jobs
[1]+  Running                 sleep 1000s &

Now let's bring the process to the foreground:

[learner planets]$ fg %1
sleep 1000s

Notice how the cursor is blinking but you don't see a prompt? That is because a command is running in the foreground.

Now let's send the process to the background which first requires the process to be suspended with Ctrl-z:

^Z
[1]+  Stopped                 sleep 1000s
[learner planets]$ bg %1
[1]+ sleep 1000s &
[learner planets]$

Killing Processes

A process will remain alive (although not necessary running) until an exit command is executed or it is deleted by either the user or the system.

We can delete our currently running sleep process by bringing it to the foreground and passing the SIGINT kill signal with Ctrl-c, running the kill command or killall command.

Let's use the kill command to kill the process:

[learner planets]$ kill %1
[1]+  Terminated              sleep 1000s

We could have also used the system PID (for example: 5522) to kill the process.

For more information about kill signals, see the signals man page: man 7 signal.