16 – Job Control
In the previous lesson, we looked at some of the implications of Linux being a multi-user operating system. In this lesson, we will examine the multitasking nature of Linux, and how this is manipulated with the command line interface.
As with any multitasking operating system, Linux executes multiple, simultaneous processes. Well, they appear simultaneous, anyway. Actually, a single processor computer can only execute one process at a time but the Linux kernel manages to give each process its turn at the processor and each appears to be running at the same time.
- ps – list the processes running on the system
- kill – send a signal to one or more processes (usually to “kill” a process)
- jobs – an alternate way of listing your own processes
- bg – put a process in the background
- fg – put a process in the forground
A Practical ExampleWhile it may seem that this subject is rather obscure, it can be very practical for the average user who mostly works with the graphical user interface. You might not know this, but most (if not all) of the graphical programs can be launched from the command line. Here’s an example: there is a small program supplied with the X Window system called xload which displays a graph representing system load. You can excute this program by typing the following: [me@linuxbox me]$ xload Notice that the small xload window appears and begins to display the system load graph. Notice also that your prompt did not reappear after the program launched. The shell is waiting for the program to finish before control returns to you. If you close the xload window, the xload program terminates and the prompt returns.
Putting A Program In The BackgroundNow, in order to make life a little easier, we are going to launch the xload program again, but this time we will put it in the background so that the prompt will return. To do this, we execute xload like this: [me@linuxbox me]$ xload &  1223 [me@linuxbox me]$ In this case, the prompt returned because the process was put in the background. Now imagine that you forgot to use the “&” symbol to put the program into the background. There is still hope. You can type Ctrl-z and the process will be suspended. The process still exists, but is idle. To resume the process in the background, type the bg command (short for background). Here is an example: [me@linuxbox me]$ xload + Stopped xload [me@linuxbox me]$ bg + xload &
Listing Your ProcessesNow that we have a process in the background, it would be helpful to display a list of the processes we have launched. To do this, we can use either the jobs command or the more powerful ps command. [me@linuxbox me]$ jobs + Running xload & [me@linuxbox me]$ ps PID TTY TIME CMD 1211 pts/4 00:00:00 bash 1246 pts/4 00:00:00 xload 1247 pts/4 00:00:00 ps [me@linuxbox me]$
Killing A ProcessSuppose that you have a program that becomes unresponsive; how do you get rid of it? You use the kill command, of course. Let’s try this out on xload. First, you need to identify the process you want to kill. You can use either jobs or ps, to do this. If you use jobs you will get back a job number. With ps, you are given a process id (PID). We will do it both ways: [me@linuxbox me]$ xload &  1292 [me@linuxbox me]$ jobs + Running xload & [me@linuxbox me]$ kill %1 [me@linuxbox me]$ xload &  1293  Terminated xload [me@linuxbox me]$ ps PID TTY TIME CMD 1280 pts/5 00:00:00 bash 1293 pts/5 00:00:00 xload 1294 pts/5 00:00:00 ps [me@linuxbox me]$ kill 1293 + Terminated xload [me@linuxbox me]$
A Little More About killWhile the kill command is used to “kill” processes, its real purpose is to send signals to processes. Most of the time the signal is intended to tell the process to go away, but there is more to it than that. Programs (if they are properly written) listen for signals from the operating system and respond to them, most often to allow some graceful method of terminating. For example, a text editor might listen for any signal that indicates that the user is logging off, or that the computer is shutting down. When it receives this signal, it saves the work in progress before it exits. The killcommand can send a variety of signals to processes. Typing:
kill -lwill give you a list of the signals it supports. Most are rather obscure, but several are useful to know:
|1||SIGHUP||Hang up signal. Programs can listen for this signal and act upon it. This signal is sent to processes running in a terminal when you close the terminal.|
|2||SIGINT||Interrupt signal. This signal is given to processes to interrupt them. Programs can process this signal and act upon it. You can also issue this signal directly by typing Ctrl-c in the terminal window where the program is running.|
|15||SIGTERM||Termination signal. This signal is given to processes to terminate them. Again, programs can process this signal and act upon it. This is the default signal sent by the kill command if no signal is specified.|
|9||SIGKILL||Kill signal. This signal causes the immediate termination of the process by the Linux kernel. Programs cannot listen for this signal.|
- Use the ps command to get the process id (PID) of the process you want to terminate.
- Issue a kill command for that PID.
- If the process refuses to terminate (i.e., it is ignoring the signal), send increasingly harsh signals until it does terminate.