我正在尝试使用 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/