Search code examples
pythongitubuntunohup

ubuntu nohup doesn't work


I'm using AWS(ubuntu instance). I want to use nohup to run in the background.

I tried this code in my putty.

sudo nohup python3 manage.py runserver --settings=health.settings 0.0.0.0:80

then,

nohup: ignoring input and appending output to 'nohup.out'

this code appears and website runs well. But I press 'Ctrl + C', then nohup ends. What's wrong?


Solution

  • nohup prevents HUP signals -- hangups, which are sent when the controlling terminal exits -- from being conveyed to the process. It doesn't ignore SIGINT, which are what ctrl+c sends.

    Start your process in the background (with & as the following command separator):

    sudo nohup python3 manage.py runserver --settings=health.settings 0.0.0.0:80 &
    

    Or redirect stdin/stdout/stderr yourself and use the bash builtin disown, which does just as good a job of preventing HUPs from propagating as nohup does:

    sudo python3 manage.py runserver ... </dev/null >runserver.log 2>&1 &
    disown -h
    

    ...or, much better, use a real process supervision system (Upstart, DJB daemontools, runit, launchd, systemd, or one of the many others -- best practice is to use whichever one your operating system vendor ships out-of-the-box absent a compelling contrary reason) to run your process in the background and restart it when it dies.