A program is an executable file that contains a series of instructions that the computer can execute. A process is a program that is being executed on the computer. The shell manages processes using the concept of a job. You can obtain a list of jobs and suspend and resume them. This lets you to maximize your use of time rather than wait for the prompt to return as the shell waits for a job execution to complete.
The shell provides several ways to identify a job:
%n- The job number is preceded by a percentage sign
%+- A shortcut to specify the current job
%-- A shortcut to specify the previous job
Jobs can be listed by using the jobs command. Let's try an example by executing a few long-running jobs. The sleep command simply waits before returning. Let's try it:
john-williams@codermerlin:~$ sleep 3
Let's try it again:
john-williams@codermerlin:~$ sleep 5
- What do you think the argument to sleep specifies?
- Are you able to prove your hypothesis? How?
These same jobs can be executed in the background (more on this below) simply by specifying an ampersand (&) after the command.
Let's try it:
john-williams@codermerlin:~$ sleep 60 &
- How is the behavior different from what you expected?
- What do you think it means to run a job in the background?
- What do you think the number displayed in brackets specifies?
Let's run several jobs in the background and then view a list of our jobs:
john-williams@codermerlin:~$ sleep 40 &
john-williams@codermerlin:~$ sleep 30 &
john-williams@codermerlin:~$ sleep 20 &
Now, let's use the jobs command:
Repeatedly execute the jobs command.
- What do you observe as you execute the jobs command?
- How do you explain your observations?
Terminating a Job
While a job is executing, we might decide that we want to terminate it. Perhaps it is taking too long or we began executing it with the wrong arguments. We can terminate a job with a special key sequence, CONTROL-C.
Let's try an example. We'll execute the cat command without any arguments, which causes it to wait for something to be entered on the console.
The process is now waiting. If we realize that we made a mistake (perhaps we wanted to print the contents of a file and forgot to specify the file's name), we can terminate the job. Try it now by pressing CONTROL-C.
Generally, when we execute a program, we run it in the foreground. This means that our shell executes the process and we won't be able to execute the next command until the currently running foreground process exits. This is ideal for short, interactive programs. Whenever we execute a program, running that program in the foreground is the default. For example, the find command can search for all files and directories in our home directory. Let's try it:
We'll likely see a long list of files and directories quickly scroll by. But what if we want to generate a list of all the files and directories on the entire server? This can take a while. Let's try it, just for fun:
jane-williams@codermerlin:~/$ find /
Eventually we'll encounter a permissions error and the process will halt. But if you don't have the patience to wait, remember that you can use the key sequence CONTROL-C to terminate the process immediately. Alternatively, we can use the key sequence CONTROL-Z to suspend the process. When we suspend a process, we can resume it in the foreground with fg followed by the job number.
When we have a complex process that will take a long time to execute, we don't want to be blocked from executing other commands while we wait for the long-running process to finish. In these cases, we can execute the long-running process in the background. Running the program in the background means that the process continues to execute, but we'll be able to enter new commands immediately. We saw an example of this above where we executed multiple sleep jobs simultaneously. We were able to do so because the shell was ready to accept new input before the previous job completed.
We can begin a job in the background by using an ampersand (&) after typing the command. When we suspend a process, we can resume it in the background with bg followed by the job number.