0

I would like to let run my script on background on my server. I red about nohup. I have my script.py. I run it like this:

python3 script.py&

It runs on background even when I close terminal. The script write numbers to file /tmp/aaa.txt every second, started from 0. I disconnect from server and reconnect to it and sow that there is numbers over 3000 (over 50 minutes). So my question is, do I really need to use nohup? If yes/no why?

# hostnamectl                                                                                                                                                                                                          
   Static hostname: my-server                                                                                                                                                                                                          
         Icon name: computer-vm                                                                                                                                                                                                              
           Chassis: vm                                                                                                                                                                                                                       
        Machine ID: XXXX                                                                                                                                                                                         
           Boot ID: XXXX                                                                                                                                                                                         
    Virtualization: vmware                                                                                                                                                                                                                   
  Operating System: Ubuntu 16.04.3 LTS                                                                                                                                                                                                       
            Kernel: Linux 4.4.0-116-generic                                                                                                                                                                                                  
      Architecture: x86-64 

My script script.py:

import time

i = 0

while True:
  f = open('aaa.txt', 'a+')
  f.write('Doing somethign: {0}\n'.format(i))
  f.close()
  i = i + 1
  time.sleep(1)
  • 1
    You don't need to use nohup if the script ignores the HUP signal. Does it? – Kusalananda Jun 25 '18 at 20:51
  • I do not configure anything, how can I check it? I added output of my script.py. – dorinand Jun 25 '18 at 20:54
3

The action is controlled by the bash option huponexit . Check to see what it is set to, if set to "on" it will send a sighup to all child processes on exit. If not, it should keep running.

$ shopt | grep huponexit
huponexit       off
| improve this answer | |
  • Hi, I can see the same output as you have, thank you for your explanation. – dorinand Jun 25 '18 at 21:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.