BASH - 检查是否存在PID [英] BASH - Check if PID Exists
问题描述
我想,直到一个进程被关闭来搪塞我的bash脚本执行(我有PID存储在一个变量)。我在想结果
而[pid正在执行];做结果
睡500结果
DONE
I want to stall the execution of my BASH script until a process is closed (I have the PID stored in a variable). I'm thinking
while [ PID IS RUNNING]; do
sleep 500
done
大多数我见过使用的/ dev / null的,这似乎需要root的例子。有没有办法做到这一点,无需root?
Most of the examples I have seen use /dev/null which seems to require root. Is there a way to do this without requiring root?
非常感谢你提前!
推荐答案
杀-s 0 $ PID
将返回成功,如果 $ PID
运行,否则失败,而无需实际发送信号的过程,这样你就可以使用,在直接在如果
语句。
kill -s 0 $pid
will return success if $pid
is running, failure otherwise, without actually sending a signal to the process, so you can use that in your if
statement directly.
等待$ PID
将等待这个过程,免去您的整个循环。
wait $pid
will wait on that process, replacing your whole loop.
这篇关于BASH - 检查是否存在PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!