
大家好, 我是吉阿, 今天给大家介绍Linux系统中等待命令的使用。
Wait是一个shell命令,它能保持等待状态, 直到指定进程运行完成,而且返回进程的退出状态值。 而且Wait命令还可以用于等待特定的进程ID和作业ID并返回其终止状态。
那么在什么情况下需要使用Wait命令呢?
在执行较大的自动化脚本时,某些命令或者模块的运行依赖于其它命令或者模块的运行结果, 这时我们需要使这些依赖别的模块结果的模块一直等待, 直到被依赖的模块完成并返回, 然后执行后续的模块或命令,在这种情况下,我们可以使用Wait命令保持等待状态直到上一个模块结束。
如何使用Wait命令
Wait命令可以用于监视先前的过程,根据被等待的命令的运行结果,它将返回Exit状态。例如,如果我们要等待进程ID是13245,我们可以使用“ wait 13245”, 当此进程(PID 13245)完成, wait命令将返回此进程(PID 13245)退出时的返回值。
PID-标识要等待终止的命令的进程ID。
wait PID
JID-标识要等待的后台进程的作业ID,仅适用于当前Shell执行环境中的wait调用。
wait JID
wait命令的退出返回值取决于指定的最后一个PID / JID。当任何进程异常终止时,退出状态将大于128。
当wait命令后面没有接参数,并且当前shell知道的所有进程ID都已终止时,Wait命令将以0值退出。
如果wait命令检测到任何错误,那么它将返回1到126之间的任何值。如果最后一个进程ID是未知的,则wait命令将以值127退出。
Wait命令使用范例
让我们通过一些例子脚本来学习了解wait命令的工作方式。
示例1:带有wait命令的脚本
我们这里有两个脚本,分别为“ foo.sh”和“ bar.sh”脚本。
“ Foo.sh”脚本的功能是打印介于1到5之间的数字,“ bar.sh”脚本将调用foo.sh并在后台运行它,然后获取foo.sh的PID并等待其完成,一旦完成,它将 启动“ bar.sh”里的循环并完成。
脚本: foo.sh
#!/bin/bash
for i in 1 2 3 4 5
do
echo “foo.sh – Looping … number $i”
done
脚本:bar.sh
#!/bin/bash
echo “Started bar.sh”
echo “Started foo.sh”
./foo.sh &
pid=$!
wait $pid
echo “Completed foo.sh”
for I in 1 2 3 4 5
do
echo “bar.sh – Looping … number $i”
done
运行结果:
Started bar.sh
Started foo.sh
foo.sh – Looping .. number 1
foo.sh – Looping .. number 2
foo.sh – Looping .. number 3
foo.sh – Looping .. number 4
foo.sh – Looping .. number 5
Completed foo.sh
bar.sh – Looping .. number 1
bar.sh – Looping .. number 2
bar.sh – Looping .. number 3
bar.sh – Looping .. number 4
bar.sh – Looping .. number 5
Completed bar.sh
$
示例2 –没有等待命令的脚本
仍旧有两个类似脚本,“ foo.sh”和“ bar.sh”脚本。 'foo.sh'脚本输出介于1到5之间的数字,而bar.sh脚本将调用foo.sh并在后台运行它,但它不会等待foo.sh完成并执行这两个脚本。
脚本– foo.sh
#!/bin/bash
for i in 1 2 3 4 5
do
echo “foo.sh – Looping … number $i”
done
脚本– bar.sh
#!/bin/bash
echo “Started bar.sh”
echo “Started foo.sh”
./foo.sh &
echo “Completed foo.sh”
for I in 1 2 3 4 5
do
echo “bar.sh – Looping … number $i”
done
运行结果:
Started bar.sh
Started foo.sh
Completed foo.sh
bar.sh – Looping .. number 1
bar.sh – Looping .. number 2
bar.sh – Looping .. number 3
bar.sh – Looping .. number 4
bar.sh – Looping .. number 5
Completed bar.sh
$
foo.sh – Looping .. number 1
foo.sh – Looping .. number 2
foo.sh – Looping .. number 3
foo.sh – Looping .. number 4
foo.sh – Looping .. number 5
$
示例3:具有等待命令和返回状态的脚本
和上面类似, “ bar.sh”脚本将调用foo.sh并在后台运行它,获取foo.sh的PID并等待其完成,一旦完成,它将启动bar.sh循环并完成,最后,它返回 foo.sh脚本的退出代码。
脚本: foo.sh (Exit status = 0)
脚本- foo.sh
#!/bin/bash
for i in 1 2 3 4 5
do
echo “foo.sh – Looping … number $i”
done
exit 0
脚本– bar.sh
#!/bin/bash
./foo.sh &
BPID=$!
wait $BPID
stat=$?
if [ $stat –eq 0 ]
then
echo “Exit status - $stat”
else
echo “Exit status - $stat”
fi
运行结果:
foo.sh – Looping .. number 1
foo.sh – Looping .. number 2
foo.sh – Looping .. number 3
foo.sh – Looping .. number 4
foo.sh – Looping .. number 5
Exit status - 0
$
Script – foo.sh (Exit status = NON Zero)
Script - foo.sh
#!/bin/bash
for i in 1 2 3 4 5
do
iiecho “foo.sh – Looping … number $i”
done
exit 127
Script – bar.sh
#!/bin/bash
./foo.sh &
BPID=$!
wait $BPID
stat=$?
if [ $stat –eq 0 ]
then
echo “Exit status - $stat”
else
echo “Exit status - $stat”
fi
Result
./foo.sh: line 4: iiecho: command not found
./foo.sh: line 4: iiecho: command not found
./foo.sh: line 4: iiecho: command not found
./foo.sh: line 4: iiecho: command not found
./foo.sh: line 4: iiecho: command not found
Exit status – 127
$
结论
wait和sleep都是操作系统中基于时间的系统调用。
让我们检查wait和sleep命令之间的区别。
wait:当用户想要暂停当前进程并释放该进程持有的所有资源并等待其他进程执行时。
sleep:当用户想要暂停当前进程一段时间时,使用此系统调用。 它将保持对资源的锁定,直到睡眠时间结束,然后再次开始执行该过程。