A Quick Introduction to Unix/Job Control
Controlling Jobs in Unix
A process in Unix - that is something doing a particular job - can run either in foreground or background. A job in foreground will be showing currently in the shell and you cannot communicate with the shell until either the job is finished or you interrupt it. A job running in background starts and returns you to the prompt where you can enter further commands while the background process continues. A background job can write to the current terminal window.
Jobs start by default in the foreground.
You can either start a job in background or send it to background after it has started.
To start a job in background
To the command to start the job you append &. So, we can run the gnu c compiler to compile the program hello in the background like this:
% gcc hello.c -o hello &
To move a started job to background
If you start a job in the foreground, you can move it to background. First, you stop the job with control-z, then use the command bg to send the stopped job to background. So, we might start the web browser Lynx, stop it, send it to the background like this:
%lynx ^z %bg
The job stopped by control-z is passed to the background and continues running.
To Foreground a Job
You can call a job to foreground using the command fg. Used on its own it will recall job most recently started in background. If we have just sent lynx to the background as above then:
will move the job into foreground.
What Jobs are Running?
Either of two commands can be used two find out what jobs are running in background.
The command jobs is available in many shells and reports the jobs running, the job numbers, the process name (and if you want the process group id with the option -l). You can use it like this:
In the output the numbers in square brackets ('[' and ']') are the job numbers that are used by the process control commands such as fg and bg.
The command ps will print information about processes currently running. Actually, it is a little more complicated than that. The command prints information about processes controlled by the current terminal and current effective user id. The process identification number needed by some commands for controlling the process will be given in this case in the first column of output.
Using job numbers
Now that you can identify a job's number using jobs or ps, you can use them to control jobs. If you have several jobs running in background, you can select one to bring to foreground by using its job number. Instead of using bare fg you add the job number like this:
This would take the job with the number two (identified by jobs) and bring it to the foreground.
Killing a Job
You can terminate Unix jobs in different ways. A simple way is to bring the job to foreground and terminate it, with control-c for example.
The Unix command kill can be used to terminate a background process. You can follow kill either by the job number (prefixed with %) or the process identification number (PID). A common misconception is that the -9 (SIGKILL signal) is required to terminate a process. This is a bad practice because the SIGKILL signal does not allow the process to gracefully terminate and makes it immediately terminate. This can cause problems with memory management. The proper way to kill a process is with the -2 (SIGINT) signal which allows the process to perform cleanup:
%kill -2 %2
The %2 specifies the PID of the process. To kill using a job number, the percent sign is omitted:
%kill -2 1367
If the -2 signal does not work, the process may be blocked or may be executing improperly. In this case, use -1 (SIGHUP), -15 (SIGTERM), and then at last resort -9 (SIGKILL).
- Shells and subshells
- Directory Structure
- Files and Processes
- Listing Files and Directories
- Exercises 1
- Creating Directories
- Creating Files
- Changing Directories
- Special Directories
- Exercises 2
- Copying Files
- Moving Files
- Deleting Files
- Exercises 3
- Searching Text Files
- Permissions on Files and Directories
- Editing Text
- Exercises 4
- My First Shell Script
- Job Control