Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / tests / jobs.tests
1 # test out %+, jobs -p, and $! agreement in a subshell first
2 ${THIS_SH} ./jobs1.sub
3
4 # test out fg/bg failure in a subshell
5 ${THIS_SH} ./jobs2.sub
6
7 # test out behavior of waiting for background pids -- bug in versions
8 # before 2.03
9 ${THIS_SH} ./jobs3.sub
10
11 # test out behavior of using job control notation when job control is not
12 # active
13 ${THIS_SH} ./jobs4.sub
14
15 # test out wait -n framework
16 ${THIS_SH} ./jobs5.sub
17
18 jobs
19 echo $?
20
21 # a no-such-job error, since we can use job control notation without job control
22 wait %1
23
24 # make sure we can't fg a job started when job control was not active
25 sleep 30 &
26 pid=$!
27 fg %1
28 # make sure the killed processes don't cause a message
29 exec 5>&2
30 exec 2>/dev/null
31 kill -n 9 $pid
32 wait    # make sure we reap the processes while stderr is still redirected
33 exec 2>&5
34
35 echo wait-for-pid
36 sleep 10 &
37 wait $!
38
39 echo wait-errors
40 wait 1-1
41 wait -- -4
42
43 echo wait-for-background-pids
44 sleep 5 &
45 sleep 8 &
46 wait
47
48 echo async list wait-for-background-pids
49 sleep 5 & sleep 8 &
50 wait
51
52 echo async list wait for child
53 sleep 5 & echo forked
54 wait
55
56 echo wait-when-no-children
57 wait
58
59 set -m
60
61 echo wait-for-job
62 sleep 5 &
63 wait %2         # this should be a no-such-job error
64 echo $?
65 wait %1
66
67 echo async list wait-for-job
68 sleep 5 & echo forked
69 wait %1
70
71 echo fg-bg 1
72 sleep 5 &
73 %1
74
75 echo fg-bg 2
76 sleep 5 &
77 fg %%
78
79 echo fg-bg 3
80 sleep 5 &
81 fg %s
82
83 echo fg-bg 4
84 sleep 5 &
85 fg %?ee
86
87 # these next two are error cases
88 echo fg-bg 5
89 sleep 15 &
90 fg %2           # this should be a no-such-job error
91 bg %1           # this should be a `bg background job?' error
92 wait
93
94 # these may someday mean to start the jobs, but not print the line
95 # describing the status, but for now they are errors
96 echo fg-bg 6
97 sleep 5 &
98 fg -s %1
99 bg -s %1
100 wait
101
102 # someday this may mean to disown all stopped jobs, but for now it is
103 # an error
104 disown -s
105
106 # this is an error -- the job with the pid that is the value of $! is
107 # retained only until a `wait' is performed
108 disown %1
109
110 # this, however, is an error
111 disown %2
112
113 echo wait-for-non-child
114 wait 1
115 echo $?
116
117 exit 1 | exit 2 | exit 3
118 echo $? -- ${PIPESTATUS[@]} -- ${PIPESTATUS[0]} - ${PIPESTATUS[1]} - ${PIPESTATUS[2]}
119
120 sleep 300 &
121 sleep300pid=$!
122 sleep 350 &
123 sleep 400 &
124
125 jobs
126
127 echo running jobs:
128 jobs -r
129
130 # should be an error
131 kill -n 1 %4
132 # should be an error
133 jobs %4
134 echo current job:
135 jobs %+
136 echo previous job:
137 jobs %-
138
139 kill -STOP %2
140 sleep 5 # give time for the shell to get the stop notification
141 echo after kill -STOP
142 echo running jobs:
143 jobs -r
144 echo stopped jobs:
145 jobs -s
146
147 disown %1
148
149 echo after disown
150 jobs
151 echo running jobs:
152 jobs -r
153 echo stopped jobs:
154 jobs -s
155
156 kill -s CONT %2
157 echo after kill -s CONT
158 echo running jobs:
159 jobs -r
160 echo stopped jobs:
161 jobs -s
162
163 kill -STOP %3
164 sleep 5 # give time for the shell to get the stop notification
165 echo after kill -STOP, backgrounding %3:
166 bg %3
167
168 disown -h %2
169
170 # make sure the killed processes don't cause a message
171 exec 5>&2
172 exec 2>/dev/null
173
174 echo killing...
175 kill -n 9 $sleep300pid
176 kill -n 9 %2 %3
177 wait    # make sure we reap the processes while stderr is still redirected
178 echo done
179
180 exec 2>&5
181
182 sleep 10 &
183 kill -STOP %1
184 sleep 5 # give time for the shell to get the stop notification
185 echo after KILL -STOP, foregrounding %1
186 fg %1
187
188 echo done