From Coder Merlin


A program is an executable file which contains a series of instructions which can be executed by the computer. A process is a program which 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 jobs. This functionality enables you to maximize your use of time rather than wait for the prompt to return as the shell waits for the execution of a job to complete.

Identifying Jobs[edit]

The shell provides several different ways to identify a job:

  • %n - The specific job number is preceded by a percentage sign
  • %+ - A shortcut to specify the current job
  • %- - A shortcut to specify the previous job

Listing Jobs[edit]

Jobs may be listed with 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

Observe, Ponder, and Journal : Section 8
  1. What do you think the argument to sleep specifies?
  2. 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 &

Observe, Ponder, and Journal : Section 9
  1. How is the behavior different from what you expected?
  2. What do you think it means to run a job in the background?
  3. 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:

john-williams@codermerlin:~$ jobs

Repeatedly execute the jobs command.

Observe, Ponder, and Journal : Section 10
  1. What do you observe as you execute the jobs command?
  2. How do you explain your observations?

Terminating a Job[edit]

While a job is executing, we may decide that we want to terminate it. Perhaps it is taking too long or we began executing it with the wrong arguments. We can often terminate the job with a special key sequence, CONTROL-C.

Let's try an example. We'll execute the cat command without any arguments, which will cause it to wait for something to be entered on the console.

jane-williams@codermerlin:~$ cat

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.

Foreground Jobs[edit]

Generally, when we execute a program we run it in the foreground. This means that our shell will execute 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 within our home directory. Let's try it:

jane-williams@codermerlin:~/Merlin$ cd
jane-williams@codermerlin:~$ find

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 of 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.

Background Jobs[edit]

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 will continue 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.

CoderMerlin™ proudly recommends:

Your purchase via these links helps keep  Coder Merlin™ 's orbuculum ablaze. As an Amazon Associate we earn from qualifying purchases. Thank you for your support!

Designed with pride in Silicon Valley, CA, USA