我正在本地计算机上使用装有 CentOS 的远程服务器,
我希望它运行以下代码:
nohup python3 search_large_files.py &
Run Code Online (Sandbox Code Playgroud)
然而,它并没有像我预期的那样工作
[root@iz2ze9wve43n2nyuvmsfx5z ~]# ps ax| grep nohup
29360 pts/1 R+ 0:00 grep --color=auto nohup
Run Code Online (Sandbox Code Playgroud)
我如何利用 nohup 来运行我的 python 代码,以便我可以在服务器工作时关闭电源并进入睡眠状态。
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()
Run Code Online (Sandbox Code Playgroud)
运行它:
nosklo@stackoverflow:~$ python3 test.py
ok
ok
^CTraceback (most recent call last):
File "test.py", line 4, in <module>
time.sleep(0.5)
KeyboardInterrupt
Run Code Online (Sandbox Code Playgroud)
现在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
Run Code Online (Sandbox Code Playgroud)
如您所见,它在那里,带有 pid12453
但没有nohup
名称。
nosklo@stackoverflow:~$ kill %1
[1]+ Terminated nohup python3 test.py > foo.txt
nosklo@stackoverflow:~$ tail foo.txt
ok
ok
ok
....
Run Code Online (Sandbox Code Playgroud)
它一直在工作。
归档时间: |
|
查看次数: |
1424 次 |
最近记录: |