bash 不使用 nohup + ps aux grep + kill 启动/停止后台 Python 进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34687883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting/stopping a background Python process wtihout nohup + ps aux grep + kill
提问by Basj
I usually use:
我通常使用:
nohup python -u myscript.py &> ./mylog.log & # or should I use nohup 2>&1 ? I never remember
to start a background Python process that I'd like to continue running even if I log out, and:
启动一个后台 Python 进程,即使我退出,我也想继续运行该进程,并且:
ps aux |grep python
# check for the relevant PID
kill <relevantPID>
It works but it's a annoying to do all these steps.
它有效,但执行所有这些步骤很烦人。
I've read some methods in which you need to save the PID in some file, but that's even more hassle.
我已经阅读了一些需要将 PID 保存在某个文件中的方法,但这更麻烦。
Is there a clean method to easily start / stop a Python script?like:
是否有一种干净的方法可以轻松启动/停止 Python 脚本?喜欢:
startpy myscript.py # will automatically continue running in
# background even if I log out
# two days later, even if I logged out / logged in again the meantime
stoppy myscript.py
Or could this long part nohup python -u myscript.py &> ./mylog.log &
be written in the shebang of the script, such that I could start the script easily with ./myscript.py
instead of writing the long nohup line?
或者这个长的部分nohup python -u myscript.py &> ./mylog.log &
可以写在脚本的shebang中,这样我就可以轻松地启动脚本./myscript.py
而不是写长的 nohup 行?
Note : I'm looking for a one or two line solution, I don't want to have to write a dedicated systemd service for this operation.
注意:我正在寻找一两行解决方案,我不想为此操作编写专用的 systemd 服务。
采纳答案by terence hill
As far as I know, there are just two (or maybe three or maybe four?) solutions to the problem of running background scripts on remote systems.
据我所知,在远程系统上运行后台脚本的问题只有两个(或者三个或四个?)解决方案。
1) nohup
1) nohup
nohup python -u myscript.py > ./mylog.log 2>&1 &
1 bis) disown
1 之二) 否认
Same as above, slightly different because it actually remove the program to the shell job lists, preventing the SIGHUP to be sent.
与上面相同,略有不同,因为它实际上将程序删除到 shell 作业列表中,从而阻止发送 SIGHUP。
2) screen (or tmux as suggested by neared)
2)屏幕(或neared建议的tmux)
Hereyou will find a starting point for screen.
您将在此处找到屏幕的起点。
See this postfor a great explanation of how background processes works. Another related post.
有关后台进程如何工作的详细解释,请参阅这篇文章。另一个相关帖子。
3) Bash
3) 重击
Another solution is to write two bash functions that do the job:
另一种解决方案是编写两个 bash 函数来完成这项工作:
mynohup () {
[[ "" = "" ]] && echo "usage: mynohup python_script" && return 0
nohup python -u "" > "${1%.*}.log" 2>&1 < /dev/null &
}
mykill() {
ps -ef | grep "" | grep -v grep | awk '{print }' | xargs kill
echo "process "" killed"
}
Just put the above functions in your ~/.bashrc
or ~/.bash_profile
and use them as normal bash commands.
只需将上述函数放在您的~/.bashrc
or 中~/.bash_profile
,并将它们用作普通的 bash 命令。
Now you can do exactly what you told:
现在你可以完全按照你说的去做:
mynohup myscript.py # will automatically continue running in
# background even if I log out
# two days later, even if I logged out / logged in again the meantime
mykill myscript.py
4) Daemon
4) 守护进程
This daemon moduleis very useful:
这个守护程序模块非常有用:
python myscript.py start
python myscript.py stop
回答by neallred
Do you mean log in and out remotely (e.g. via SSH)? If so, a simple solution is to install tmux (terminal multiplexer). It creates a server for terminals that run underneath it as clients. You open up tmux with tmux
, type in your command, type in CONTROL+B+D
to 'detach' from tmux, and then type exit at the main terminal to log out. When you log back in, tmux and the processes running in it will still be running.
您的意思是远程登录和注销(例如通过 SSH)?如果是这样,一个简单的解决方案是安装 tmux(终端多路复用器)。它为作为客户端在其下运行的终端创建了一个服务器。你打开 tmux tmux
,输入你的命令,输入CONTROL+B+D
to 'detach' from tmux,然后在主终端输入 exit 退出。当您重新登录时,tmux 和其中运行的进程仍将运行。