繁体   English   中英

Nohup运行python程序

[英]Nohup to run python program

我正在使用本地计算机上装有CentOS的远程服务器上工作,

我希望它运行以下代码:

nohup python3 search_large_files.py &

但是,它没有按我预期的那样工作

[root@iz2ze9wve43n2nyuvmsfx5z ~]# ps ax| grep nohup
29360 pts/1    R+     0:00 grep --color=auto nohup

我如何利用nohup运行我的python代码,以便在服务器工作时可以关闭电源并进入睡眠状态。

nohup将自己从正在运行的进程的名称中删除。 使用ps ax| grep nohup找不到它ps ax| grep nohup ps ax| grep nohup就是这个原因。

检查我制作的这个test.py文件:

import sys
import time
while True:
    time.sleep(0.5)
    sys.stdout.write('ok\n')
    sys.stdout.flush()

运行它:

nosklo@stackoverflow:~$ python3 test.py 
ok
ok
^CTraceback (most recent call last):
  File "test.py", line 4, in <module>
    time.sleep(0.5)
KeyboardInterrupt

现在使用nohup

nosklo@stackoverflow:~$ nohup python3 test.py > foo.txt &
nohup: ignoring input and redirecting stderr to stdout
[1] 12453
nosklo@stackoverflow:~$ ps ax | grep -i nohup
nosklo  12548  0.0  0.0  15976   944 pts/17   S+   14:14   0:00 grep --color=auto -i nohup
nosklo@stackoverflow:~$ ps ax | grep -i python
nosklo  12453  0.0  0.0  31400  5660 pts/17   S    14:13   0:00 python3 test.py
nosklo  12528  0.0  0.0  15976   940 pts/17   S+   14:14   0:00 grep --color=auto -i python

如您所见,它的名称为pid 12453但没有nohup

nosklo@stackoverflow:~$ kill %1
[1]+  Terminated               nohup python3 test.py > foo.txt
nosklo@stackoverflow:~$ tail foo.txt
ok
ok
ok
....

它一直都在工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM