手把手教程:Ubuntu系统 nohup 后台运行,以为 .py 文件运行为例
如何在 xshell界面,使用 nohup运行 python Train.py。因为 xshell 界面断开连接之后,python等成勋的运行就断了。nohup可忽略挂起信号(如 SSH 断开),保持进程运行。
(1)后台运行 .py 格式的python 程序
nohup python Train.py > train.log 2>&1 &
nohup:忽略挂起信号(如 SSH 断开),保持进程运行。
python Train.py:你要运行的 Python 脚本。
> train.log:将标准输出保存到 train.log 文件中。
2>&1:将标准错误也重定向到同一个日志文件。
&:将任务放到后台运行。
查看脚本是否在运行:
ps aux | grep Train.py
或者更简单:
pgrep -af Train.py
实时查看日志输出:
tail -f train.log
如要退出 tail,按 Ctrl + C。
停止运行(如果需要):
-
查看进程号(PID):
pgrep -af Train.py -
终止进程:
kill 进程号
(2)后台运行 Jupyter Notebook (本地调用服务器资源)
# 后台运行
nohup jupyter notebook --no-browser --port=8888 --ip=0.0.0.0 > jupyter.log 2>&1 &
这里返回的编号,其实就是后面需要 kill的进程编号。

# 查看输出的日志
cat jupyter.log
实时查看日志输出:
tail -f jupyter.log
如要退出 tail,按 Ctrl + C。
停止运行(如果需要):
-
查看进程号(PID):
pgrep -af jupyter返回结果:
2088 /home/ubuntu/anaconda3/bin/python /home/ubuntu/anaconda3/bin/jupyter-notebook --no-browser --port=8888 --ip=0.0.0.0 -
终止进程:
kill 进程号

4983

被折叠的 条评论
为什么被折叠?



