执行将进程作为守护进程运行的 shell 脚本时,Python 挂起

标签 python python-2.7 daemon

我正在尝试使用 os.system(即将被 subprocess 取代)调用 shell 脚本(它以守护进程的形式运行进程)

os.system('/path/to/shell_script.sh')

shell 脚本如下所示:

nohup /path/to/program &

如果我在我的本地环境中执行这个 shell 脚本,我必须在返回到控制台之前按回车键,因为 shell 脚本正在作为守护进程运行一个进程。如果我在 python 中执行上述命令,我还必须在返回到控制台之前按回车键。

但是,如果我在 python 程序中执行此操作,它就会永远挂起。

如何让python程序在调用一个守护进程运行的shell脚本后恢复执行?

最佳答案

来自 here -

Within a script, running a command in the background with an ampersand (&) may cause the script to hang until ENTER is hit. This seems to occur with commands that write to stdout.

你应该尝试将你的输出重定向到某个文件(如果你不需要它,则为 null),如果你真的不需要输出,也许是 /dev/null

 nohup /path/to/program > /dev/null &

关于执行将进程作为守护进程运行的 shell 脚本时,Python 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31709072/

相关文章:

html - 如何绕过一些缓存或类似的?

python - 如何使用 andrew_curves 绘制 Pandas 数据框?

qt - 了解 QtService 中的后端

python - 为什么分配给类属性的函数被视为实例方法?

javascript - 将 Zeppelin 与外部 javascript 工具(如plotly)结合使用

python - 如何从python中的父文件夹导入依赖的cython模块

python - 如何使用 Python 将 XML 中的所有信息放入字典中

python - python中的json.dump()和json.dumps()有什么区别?

c - fcntl F_GETLK 总是返回真

c++ - 启动停止守护进程不发送 SIGTERM