Managing Shell Jobs
When moving jobs between the foreground and background, it could be useful to a overview of all current jobs. To get such a overview, use the jobs command. As can see in Table 9.2, this command gives an overview of all jobs currently running as a background job, inclu Ding the job number assigned to the job when starting it in the background. These job numbers can be used as a argument to the FG and BG commands to perform job management tasks. In Exercise 9.1, you learn how to perform common job management tasks from the shell.
A good example of the practice of jobs management:
Exercise 9.1 Managing jobs in this Exercise and you apply the commands that's just learned about to manage jobs that has B Een started from the current shell.
1. Open a root shell and type the following commands:
Sleep 3600 &
DD If=/dev/zero Of=/dev/null &
Sleep 7200
2. Because you started the last command with no & after the command, you have to wait 2 hours before get Control to the shell back. Type Ctrl + Z to stop it.
3. Type jobs. You'll see the three jobs so you just started. The first of them has the Running state, and the last job currently was in the Stopped state.
3600 &[12464if=/dev/zero of=/dev/null & [22465
[[email protected] ~]# sleep 7200
^z
[3]+ Stopped Sleep 7200
[[email protected] ~]# jobs
[1] Running Sleep 3600 &
[2]-Running dd If=/dev/zero of=/dev/null &
[3]+ Stopped Sleep 7200
4.
[[email protected] ~]# bg 3 [ Span style= "color: #800080;" >3 ]+ sleep 7200 &[[email Protected] ~]# jobs[ 1 ] Running Sleep 3600 &2 ]-Running dd if =/dev/zero of=/dev/null &[ 3 ]+ Running Sleep 7200 &[[email protected] ~]#
5. Type FG 1 to move job 1 to the foreground.
6. Type Ctrl + C to cancel the job number 1 and use the jobs to confirm that it's now gone.
7. Use the same approach to cancel jobs 2 and 3 also.
[[email protected] ~]# FG1Sleep3600^C[[email protected]~]# jobs[2]-Running DDif=/dev/zero of=/dev/NULL&[3]+ Running Sleep7200&[[Email protected]~]# FG2DDif=/dev/zero of=/dev/NULL^c590233206+0Recordsinch590233205+0Records out302199400960Bytes (302GB) Copied,372.251S812MB/S[[email protected]~]# jobs[3]+ Running Sleep7200&[[Email protected]~]# FG3Sleep7200^C[[email protected]~]# Jobs[[email protected]~]#
8. Open A second terminal on your server.
9. from that second terminal, type DD If=/dev/zero of=/dev/null &.
Type Exit to close the second terminal.
from the other terminal, start top. You'll see that the DD job is still running. From top, use K to kill the DD job.
Managing Linux Shell Jobs