version 7, including all changes.
.
Rev |
Author |
# |
Line |
1 |
PerryLorier |
1 |
In Unix you can run "jobs", suspend them, run them in the background, or bring them to the foreground. |
|
|
2 |
|
|
|
3 |
The commands for this are: |
|
|
4 |
|
4 |
BenStaz |
5 |
!jobs |
|
|
6 |
|
|
|
7 |
This command will list all of the active jobs. |
|
|
8 |
Some switches you may like to try include: |
|
|
9 |
|
|
|
10 |
*''jobs -r'' will display running jobs only. |
|
|
11 |
*''jobs -s'' will display stopped jobs only. |
|
|
12 |
|
|
|
13 |
!bg |
|
|
14 |
|
|
|
15 |
''bg <JOB_SPEC>'' moves the job into the background. |
|
|
16 |
|
6 |
BenStaz |
17 |
Note : if the job is currently in the foreground you will first have to suspend it using ''Ctrl-z'' ([SIGTSTP]) |
4 |
BenStaz |
18 |
|
|
|
19 |
Note : To start an application in the background append ''&'' to the end of your command. |
|
|
20 |
|
|
|
21 |
*ping www.gooogle.co.nz & |
|
|
22 |
|
|
|
23 |
!fg |
|
|
24 |
|
|
|
25 |
''fg <JOB_SPEC>'' moves the stopped or backgrounded job into the foreground. |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
!kill |
|
|
29 |
|
|
|
30 |
''kill %<JOB_SPEC>'' sends a signal to a job. |
|
|
31 |
|
|
|
32 |
Note : you can use use ''kill -l'' to list the possible signals you could send. |
3 |
BenStaz |
33 |
|
|
|
34 |
!bg,fg and kill are not limited to jobids. |
|
|
35 |
|
|
|
36 |
We can also specify the start or part of the job command. |
|
|
37 |
|
|
|
38 |
For example: If a job command consisted of ''ping www.google.co.nz'', then these commands are valid. |
|
|
39 |
|
|
|
40 |
*kill %ping |
|
|
41 |
*kill %?google |
|
|
42 |
*fg ping |
7 |
BenStaz |
43 |
*bg ?goo |
3 |
BenStaz |
44 |
|
|
|
45 |
These will work as long as you don't supply an ambiguous job_spec. |
4 |
BenStaz |
46 |
Notice that, only kill requires the ''%'' so that it knows we are providing it with a JOB_SPEC rather than a process id. |
7 |
BenStaz |
47 |
|
1 |
PerryLorier |
48 |
|
|
|
49 |
!!How this works |
2 |
PerryLorier |
50 |
Internally each command line (job) gets it's own ProcessGroup assigned to it by the shell when it creates the job using [setsid(2)]. While the command is running in the foreground it recieves any signals from the tty layer (such as [SIGTSTP], [SIGINT], [SIGQUIT], [SIGHUP] etc). |
1 |
PerryLorier |
51 |
|
|
|
52 |
If you press ^Z the ProcessGroup gets sent a [SIGTSTP]. If the process(es) ignore the [SIGTSTP], this is converted by the kernel into a [SIGSTOP]. When a process recieves a [SIGSTOP] it's parent (the shell) will get notified (via a [SIGCHLD]), and will mark that job as being "Stopped". |
|
|
53 |
|
2 |
PerryLorier |
54 |
When you type "fg" or "bg", then the process is started with a [SIGCONT], and in the case of fg, [tcsetpgrp(3)] is called to make that ProcessGroup the new foreground ProcessGroup. |
1 |
PerryLorier |
55 |
|
|
|
56 |
When the shell recieves a [SIGHUP] (presumably from the terminal going away), then it will send a [SIGHUP] to each job. If a job is also "Stopped" then the shell will also send it a [SIGCONT]. Depending on your shell, exiting the shell with ^D, "exit", "logout","exit 0" etc may or may not cause jobs to recieve the [SIGHUP]. |
|
|
57 |
|
|
|
58 |
The disown builtin may be used to remove a process group from the shells list of jobs, so when the shell exits it doesn't send that process a [SIGHUP]. |