![]() |
||||||||||||||||
Job ControlIn 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. There are several commands that can be used to control processes. They are:
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 & [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 [me@linuxbox me]$ bg 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 [me@linuxbox me]$ 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 & [me@linuxbox me]$ jobs [me@linuxbox me]$ kill %1 [me@linuxbox me]$ xload & [me@linuxbox me]$ ps [me@linuxbox me]$ kill 1293 [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 kill command can send a variety of signals to processes. Typing: kill -l will give you a list of the signals it supports.
Most are rather obscure, but several are useful to
know:
Now let's suppose that you have a program that is hopelessly hung and you want to get rid of it. Here's what you do:
[me@linuxbox me]$ ps x | grep bad_program [me@linuxbox me]$ kill -SIGTERM 2931 [me@linuxbox me]$ kill -SIGKILL 2931 In the example above I used the ps command with the x option to list all of my processes (even those not launched from the current terminal). In addition, I piped the output of the ps command into grep to list only list the program I was interested in. Next, I used kill to issue a SIGTERM signal to the troublesome program. In actual practice, it is more common to do it in the following way since the default signal sent by kill is SIGTERM and kill can also use the signal number instead of the signal name: [me@linuxbox me]$ kill 2931 Then, if the process does not terminate, force it with the SIGKILL signal: [me@linuxbox me]$ kill -9 2931 That's It!This concludes the "Learning the shell" series of lessons. In the next series, "Writing shell scripts," we will look at how to automate tasks with the shell. |
||||||||||||||||
© 2000-2019, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved. Linux® is a registered trademark of Linus Torvalds. |