我在本地计算机上使用配备了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代码,这样我就可以在服务器工作时关闭电源并进入睡眠状态。
发布于 2018-08-28 01:32:57
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
....它一直都在工作。
https://stackoverflow.com/questions/52044111
复制相似问题