]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/jobs.tests
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / jobs.tests
CommitLineData
8868edaf
CR
1# This program is free software: you can redistribute it and/or modify
2# it under the terms of the GNU General Public License as published by
3# the Free Software Foundation, either version 3 of the License, or
4# (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10#
11# You should have received a copy of the GNU General Public License
12# along with this program. If not, see <http://www.gnu.org/licenses/>.
13#
cce855bc
JA
14# test out %+, jobs -p, and $! agreement in a subshell first
15${THIS_SH} ./jobs1.sub
16
17# test out fg/bg failure in a subshell
18${THIS_SH} ./jobs2.sub
19
bb70624e
JA
20# test out behavior of waiting for background pids -- bug in versions
21# before 2.03
22${THIS_SH} ./jobs3.sub
23
b80f6443
JA
24# test out behavior of using job control notation when job control is not
25# active
26${THIS_SH} ./jobs4.sub
27
ac50fbac
CR
28# test out wait -n framework
29${THIS_SH} ./jobs5.sub
30
d233b485
CR
31# test out wait -f framework
32${THIS_SH} ./jobs6.sub
33
8868edaf
CR
34${THIS_SH} ./jobs7.sub
35
d166f048
JA
36jobs
37echo $?
38
b80f6443 39# a no-such-job error, since we can use job control notation without job control
cce855bc
JA
40wait %1
41
42# make sure we can't fg a job started when job control was not active
43sleep 30 &
44pid=$!
45fg %1
46# make sure the killed processes don't cause a message
47exec 5>&2
48exec 2>/dev/null
49kill -n 9 $pid
50wait # make sure we reap the processes while stderr is still redirected
51exec 2>&5
52
d166f048 53echo wait-for-pid
8868edaf 54sleep 4 &
d166f048
JA
55wait $!
56
57echo wait-errors
58wait 1-1
59wait -- -4
60
61echo wait-for-background-pids
8868edaf
CR
62sleep 2 &
63sleep 4 &
d166f048
JA
64wait
65
66echo async list wait-for-background-pids
8868edaf 67sleep 2 & sleep 4 &
d166f048
JA
68wait
69
70echo async list wait for child
8868edaf 71sleep 2 & echo forked
d166f048
JA
72wait
73
74echo wait-when-no-children
75wait
76
8868edaf
CR
77echo posix jobs output
78${THIS_SH} -o posix -c 'sleep 1 & P=$! ; sleep 2; jobs; wait'
79
d166f048
JA
80set -m
81
82echo wait-for-job
8868edaf 83sleep 3 &
d166f048
JA
84wait %2 # this should be a no-such-job error
85echo $?
86wait %1
87
88echo async list wait-for-job
8868edaf 89sleep 2 & echo forked
d166f048
JA
90wait %1
91
92echo fg-bg 1
8868edaf 93sleep 2 &
d166f048
JA
94%1
95
96echo fg-bg 2
8868edaf 97sleep 2 &
d166f048
JA
98fg %%
99
100echo fg-bg 3
8868edaf 101sleep 2 &
d166f048
JA
102fg %s
103
104echo fg-bg 4
8868edaf 105sleep 2 &
d166f048
JA
106fg %?ee
107
108# these next two are error cases
109echo fg-bg 5
8868edaf 110sleep 2 &
d166f048
JA
111fg %2 # this should be a no-such-job error
112bg %1 # this should be a `bg background job?' error
113wait
114
115# these may someday mean to start the jobs, but not print the line
116# describing the status, but for now they are errors
117echo fg-bg 6
8868edaf 118sleep 2 &
d166f048
JA
119fg -s %1
120bg -s %1
121wait
122
cce855bc 123# someday this may mean to disown all stopped jobs, but for now it is
d166f048 124# an error
cce855bc 125disown -s
d166f048 126
cce855bc
JA
127# this is an error -- the job with the pid that is the value of $! is
128# retained only until a `wait' is performed
d166f048
JA
129disown %1
130
cce855bc
JA
131# this, however, is an error
132disown %2
133
d166f048
JA
134echo wait-for-non-child
135wait 1
136echo $?
137
138exit 1 | exit 2 | exit 3
139echo $? -- ${PIPESTATUS[@]} -- ${PIPESTATUS[0]} - ${PIPESTATUS[1]} - ${PIPESTATUS[2]}
140
141sleep 300 &
0001803f 142sleep300pid=$!
d166f048
JA
143sleep 350 &
144sleep 400 &
145
146jobs
147
148echo running jobs:
149jobs -r
150
151# should be an error
152kill -n 1 %4
cce855bc
JA
153# should be an error
154jobs %4
155echo current job:
156jobs %+
157echo previous job:
158jobs %-
d166f048
JA
159
160kill -STOP %2
8868edaf 161sleep 3 # give time for the shell to get the stop notification
d166f048
JA
162echo after kill -STOP
163echo running jobs:
164jobs -r
165echo stopped jobs:
166jobs -s
167
168disown %1
169
170echo after disown
171jobs
172echo running jobs:
173jobs -r
174echo stopped jobs:
175jobs -s
176
177kill -s CONT %2
178echo after kill -s CONT
179echo running jobs:
180jobs -r
181echo stopped jobs:
182jobs -s
183
184kill -STOP %3
8868edaf 185sleep 3 # give time for the shell to get the stop notification
d166f048
JA
186echo after kill -STOP, backgrounding %3:
187bg %3
188
189disown -h %2
190
191# make sure the killed processes don't cause a message
192exec 5>&2
193exec 2>/dev/null
194
195echo killing...
0001803f 196kill -n 9 $sleep300pid
d166f048
JA
197kill -n 9 %2 %3
198wait # make sure we reap the processes while stderr is still redirected
199echo done
200
201exec 2>&5
202
8868edaf 203sleep 4 &
d166f048 204kill -STOP %1
8868edaf 205sleep 2 # give time for the shell to get the stop notification
d166f048
JA
206echo after KILL -STOP, foregrounding %1
207fg %1
208
209echo done