检查 Bash 中是否存在 PID [英] Check if PID exists in Bash

查看:76
本文介绍了检查 Bash 中是否存在 PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想暂停 BASH 脚本的执行,直到进程关闭(我将 PID 存储在变量中).我在想

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?

在此先非常感谢您!

推荐答案

kill -s 0 $pid 如果 $pid 正在运行,将返回成功,否则失败,没有实际上向进程发送信号,因此您可以直接在 if 语句中使用它.

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 将等待该进程,替换整个循环.

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

这篇关于检查 Bash 中是否存在 PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆