检查进程是否正在运行,是否启动的最优雅的方法?


8

我一直在用各种方法来搞砸bash,而我发现pgrep program > /dev/null || program这是最短/最优雅的方法。有没有比这更好的方法了?

Answers:


9

使用pgrep,您可能会匹配其他程序而不只是您的程序。如果要确保匹配正确的程序,请pidof改用。

您也可以使用start-stop-daemonPID文件。start-stop-daemon将检查程序是否正在运行,并在需要时启动它。这是大多数初始化脚本中使用的。


通过start-stop-daemon的man文件,它看起来不错。感谢您的见解!
aperson

1
您可能还对该/lib/lsb/init-functions库感兴趣,该库大多数初始化脚本都会解析该库以使用包装函数,例如start_daemonpidofproc
ℝaphink

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.