Search code examples
node.jsbashcentosnohup

bash, nohup sh file launches node and node hangs up on logoff?


I have a bash script test.sh

node ~/script1.js && \
node ~/script2.js && \
node ~/script3.js

I launch the bash script

nohup ~/test.sh &

Then I get disconnected due to losing connection to the server. I would expect the scripts to continue as i used nohup however the node script is getting a hangup signal.

What is the right way to do this?


Solution

  • If you know you're going to disconnect often your best bet is to use screen or tmux or similar, that way you can both disconnect and reconnect later if desired. To do that if you're at an interactive shell you could just, say, run screen, then run your command (without needing the nohup or putting it in the background) then type ctrla then d to detach from that screen session. You can then exit and the screen session and command will continue.

    With screen you could then reconnect to the session by running screen -x if there's only the one screen session, or you can give an argument to -x to specify which one to reattach to (you can give it a name when you start screen with the -S flag to make it easier to remember which to reconnect to). You can see a list of currently running screen sessions with screen -ls.

    tmux supports doing all that as well, but I'm more familiar with screen myself.

    If you want to do it with nohup as your current attempt does, you'll also want to disown the command, which will remove that job from your current shell. If that's the only one you could disown -a to disown all your jobs.