BASH - Check if PID Exists

送分小仙女□ 提交于 2019-12-02 20:29:58

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.

wait $pid will wait on that process, replacing your whole loop.

It seems like you want

wait $pid

which will return when $pid finishes.

Otherwise you can use

ps -p $pid

to check if the process is still alive (this is more effective than kill -0 $pid because it will work even if you don't own the pid).

You might look for the presence of /proc/YOUR_PID directory.

I always use the following tail -f /dev/null --pid $PID. It doesn't require explicit loop and isn't limited to your shell's children pids only.

ps --pid $pid &>/dev/null

returns 0 if it exists, 1 otherwise

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!