Python 在执行将进程作为守护进程运行的 shell 脚本时挂起
Python hangs when executing a shell script that runs a process as a 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 &
您为什么不尝试使用单独的线程?
将您的流程总结成
def run(my_arg):
my_process(my_arg)
thread = Thread(target = run, args = (my_arg, ))
thread.start()
检查加入和锁定以更好地控制线程执行。
https://docs.python.org/2/library/threading.html
我正在尝试使用 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 &
您为什么不尝试使用单独的线程? 将您的流程总结成
def run(my_arg):
my_process(my_arg)
thread = Thread(target = run, args = (my_arg, ))
thread.start()
检查加入和锁定以更好地控制线程执行。 https://docs.python.org/2/library/threading.html