簡體   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