我通过ssh运行一个Python脚本filename.py
。一旦我登录到远程机器上,我会运行以下命令:
python filename.py &
然而,当我关闭终端时,Python 停止运行。为什么会这样?我以为在语句结尾加上 & 符号意味着程序仍在后台运行?
nohup
命令:nohup python filename.py &
nohup [command] &
可以在后台运行任务,并将您带回到shell。
$ nohup:忽略输入并将输出附加到nohup.out
- ShanZhengYangnohup
的输出会被写入一个名为 nohup.out
的文件中。如果你想要观察它的运行,可以使用 tail -f nohup.out
命令。 - Alex Woolford我对Python在shell中的使用还不是很熟悉,但在SSH相关环境中可以使用screen。
例如:
sudo apt-get install screen
screen -m
python prog.py &