Script started with nohup get killed when leave terminal

Posted on

Script started with nohup get killed when leave terminal – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, ubuntu, python, nohup, .

I need to run some python scripts with infinite loop on ubuntu server (Amazon EC2). And I really like the idea to use nohup as it is simple and I can have output.out log file. I start script like

nohup script.py > output.out & 

The process get started. I check it with jobs -l all fine.
Then I close the terminal and log back check processes with jobs -l and I can’t see it any more. Can you advise me what am I doing wrong?

Solution :

If you change session (log out and come back), there is no chance you’ll see your script as job.

try ps instead.

ps -u myuser

replacing myuser with actual login..

If your job is visible when you use jobs -l you can remove it from your session with disown

Another way to keep script running is to use the screen tool.

Try redirecting all I/O streams

nohup script.py < /dev/null > output.out 2> error.out &

Long expalation:
Background running process (job) is always bound to particular terminal:

[11]user@lab7-11:~> nohup ./test.sh > out &
[1] 39978

[11]user@lab7-11:~> ps aux |grep 39978
user   39978  0.0  0.0 106096  1156 pts/11   S    06:15   0:00 bash ./test.sh

While it is bound to terminal, you are able to see it in jobs output:

[11]user@lab7-11:~> jobs -l
[1]+ 39978 Running                 nohup ./test.sh > out &

After you’ve closed that terminal, your job became daemon and is not more bound to terminal

[7]user@lab7-11:~> ps aux |grep 39978
user   39978  0.0  0.0 106096  1156 ?        S    06:15   0:00 bash ./test.sh

So, at this stage, you can check state of your process via ps as suggested by @Archemar but not via job

Short explanation:
Your script was not killed – it became daemon.

Leave a Reply

Your email address will not be published. Required fields are marked *