Wizard
Wizard

Reputation: 22083

Nohup to run python program

I am working on a remote server equipped with CentOS from the local computer,

I'd like it to run the following codes:

nohup python3 search_large_files.py &

However, it did not worked as I expected

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

How could I utilize nohup to run my python codes so I can power off and go sleeping while the server is working.

Upvotes: 0

Views: 4870

Answers (1)

nosklo
nosklo

Reputation: 222842

nohup removes itself from the name of the process running. You can't find it with ps ax| grep nohup for this reason.

Check this test.py file I made:

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

Running it:

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

Now with 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

As you can see it is there, with pid 12453 but without nohup in the name.

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

And it was working this whole time.

Upvotes: 7

Related Questions