Difference between revisions of "Shell/Jobs"

From Coder Merlin
m (Editorial review and minor corrections)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
== Jobs ==
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.
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 ===
=== Identifying Jobs ===
The shell provides several different ways to identify a job:
The shell provides several ways to identify a job:
* <code>%''n''</code> - The specific job number is preceded by a percentage sign
* <code>%''n''</code> - The job number is preceded by a percentage sign
* <code>%+</code> - A shortcut to specify the current job
* <code>%+</code> - A shortcut to specify the current job
* <code>%-</code> - A shortcut to specify the previous job
* <code>%-</code> - A shortcut to specify the previous job


=== Listing Jobs ===
=== Listing Jobs ===
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:
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:
{{ConsoleLine|john-williams@codermerlin:~$|sleep 3}}
{{ConsoleLine|john-williams@codermerlin:~$|sleep 3}}


Line 18: Line 17:
{{Observe|: Section 8|
{{Observe|: Section 8|
# What do you think the argument to sleep specifies?
# What do you think the argument to sleep specifies?
# Are you able to prove your hypothesis? How?
# Are you able to prove your hypothesis? How?
}}
}}


Line 34: Line 33:
# What do you think the number displayed in brackets specifies?
# What do you think the number displayed in brackets specifies?
}}
}}
 
{{MerlinNoteFromEditor|I'm not sure what brackets it's referring to in step 3 above. Please clarify.}}


Let's run several jobs in the background and then view a list of our jobs:
Let's run several jobs in the background and then view a list of our jobs:
Line 52: Line 51:


=== Terminating a Job ===
=== Terminating a Job ===
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, {{SpecialKey|CONTROL|C}}.  
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, {{SpecialKey|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.   
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.   


{{ConsoleLine|jane-williams@codermerlin:~$|cat}}
{{ConsoleLine|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 {{SpecialKey|CONTROL|C}}.
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 {{SpecialKey|CONTROL|C}}.


=== Foreground Jobs ===
=== Foreground Jobs ===
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:
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:


{{ConsoleLine|jane-williams@codermerlin:~/Merlin$|cd}}
{{ConsoleLine|jane-williams@codermerlin:~/Merlin$|cd}}
{{ConsoleLine|jane-williams@codermerlin:~$|find}}
{{ConsoleLine|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:
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:


{{ConsoleLine|jane-williams@codermerlin:~/$|find /}}
{{ConsoleLine|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 {{SpecialKey|CONTROL|C}} to terminate the process immediately. Alternatively, we can use the key sequence {{SpecialKey|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.
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 {{SpecialKey|CONTROL|C}} to terminate the process immediately. Alternatively, we can use the key sequence {{SpecialKey|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 ===
=== Background Jobs ===
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.
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.
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.




{{MerlinMultipageExperienceNavBar}}
{{MerlinMultipageExperienceNavBar}}

Latest revision as of 13:54, 22 February 2023

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

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.

Identifying Jobs[edit]

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

Listing Jobs[edit]

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


ObserveObserveIcon.png
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 &


ObserveObserveIcon.png
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?

 This article can be improved by:  I'm not sure what brackets it's referring to in step 3 above. Please clarify.

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.

ObserveObserveIcon.png
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 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.

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 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:

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