last day (15 days later) » 

5:22 PM
3
A: How to run node.js app forever when console is closed?

T.J. CrowderMake sure you start it disconnected from the console. On *nix, that would usually be as simple as putting an ampersand (&) at the end of the command line. On Windows, the start command has similar functionality. Update: In the comments below, @nponeccop and @kennis claim that the & thing doesn't...

 
@nponeccop: No, it doesn't get disconnected when you disconnect from SSH, at least not on any nix system I've ever used (several, mostly but not exclusively Linux variants). I do this all the time. SSH into my server, start a long-running process disconnected as above, exit my SSH session, and go back later to check out the result. Recommend you *go try it.
@kennis: "Node.js definitely stops when you disconnect from SSH" See above, clearly not. Again, did you try it?
@nponeccop: See update, I'm afraid you're just mistaken about &.
@kennis: Strange. Don't know what to tell you, WFM. But there's no reason not to use nohup if that's what works for you, although I think we're all agreed that there are better ways of ensuring your node process is running than either nohup or &. :-)
 
All & does is spawn a process as a subshell. It says nothing about how the application responds to the SIGHUP that is sent to the process when you disconnect. Most processes gracefully exit when they get SIGHUP. It's the process's job to decide what to do with a signal. Enter nohup, which the man page describes perfectly: "run a command immune to hangups". & doesn't say anything about whether a process will or won't run in the background after logout. That's the job of the shell/process. That's why one script may exit and another not, they handle the SIGHUP differently. The & doesn't matter.
 
@coderjoe: The point is that & works because the subshell doesn't terminate when you exit your ssh shell, so SIGHUP isn't sent to the process, so how it handles SIGHUP doesn't matter.
 
@T.J.Crowder The bash manual, and simple bash scripts which log SIGHUP disagree with you. I think what you mean to say is SSH does not send a SIGHUP to bash on all forms of exiting. But if it does send it, bash will send it to the process causing it to terminate (if the process handles the SIGHUP). From the manual: "The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends the SIGHUP to all jobs, running or stopped."
 
@coderjoe: All I can say is that across multiple versions of multiple *nix distros, logging in via an ssh shell, starting a long-running process using an & at the end of the command, and disconnecting the ssh shell has worked for me consistently for more than 15 years. Early on it was Red Hat, then Fedora, and since then in no particular order CentOS, Debian, Knoppix, Maemo (yes, really, on a Nokia 770), and Ubuntu (a bunch of revs). If you have a replicatable case where it doesn't work, I'd be interested to see it, as I don't think exiting the ssh shell is what's ending the process.
@coderjoe: For the avoidance of doubt, I don't mean that last bit about a replicable test case in any kind of trolling way. If & isn't working for you, I'd love to know what you're doing so I know what's different, 'cause it could be really useful. :-) The previous people claiming it didn't work never came back with anything to support what they were saying, so I couldn't learn it from them.
 
5:22 PM
@T.J.Crowder a quick way to test this. Create a bash script that traps SIGHUP and otherwise runs forever. Start it with &. Send SIGHUP to the pseudo terminal that SSH spawns. Your subprocess running script will receive the SIGHUP. The thing with SSH is that ssh doesn't send SIGHUP on graceful disconnects. However if you interrupt your session, lose connection, close it in a non-graceful manner, or operate without a pseudo-tty, then SSH will send the SIGHUP and your subprocess will receive it. This is the reason why using & is a bad idea, you don't always control if SSH sends SIGHUP on exit.
@T.J.Crowder FWIW as a sanity check I also just tested this using a vagrant virtual machine not but 5 minutes ago and it operates as I and the bash manual describes. If bash gets SIGHUP, it sends SIGHUP to all child processes. SSH doesn't send SIGHUP on all types of connection closes.
@T.J.Crowder gist.github.com/coderjoe/10660379 There's a test script you can use. Run it with & and exit bash using one of 3 methods. Standard exit (script won't stop). Terminated connection (script stops). Force exit (script stops). Again this goes back to my original comment, whether the application stops is up to how it responds is really up to how it handles SIGHUP because whether it gets SIGHUP is dependent on many variables. How the connection closed, the SSH implementation, and more. The only way to guarantee consistant results is with nohup or some method detatch the process.
 
@coderjoe: Ah, we may know what the difference is now, but I still haven't managed to make the script stop. I always ssh into a shell from a shell. I usually exit by typing exit, which presumably is a "clean" exit and doesn't send the signal. But I've also tried killing the ssh (sudo kill -9 <pid for ssh> from a separate local shell), and the script started by that kept running as well. I'd've thought kill -9 would be, um, unclean. How are you terminating the ssh connection? All the ways I've tried, the script keeps going, but I expect there are other ways.
 
@T.J.Crowder The test script only traps SIGHUP use HUP. kill -l (letter L) to list the proper signal numbers.
@T.J.Crowder it's also worth noting that node responds to SIGHUP by shutting down. I've updated the gist with an example using node: gist.github.com/coderjoe/10660379 it quits on me when SIGHUP is received.
 
@coderjoe: Yes, but I'm not interested in synthetically creating SIGHUP. I'm interested in real-world situations where what I've described above won't work correctly. So far, all my normal use cases work just fine.
 
@T.J.Crowder as I said before, in your environment if you exit cleanly it will keep running. This will not work in all environments, with all SSH servers, and with all methods of closing a connection. The post to which this message responds specifically says "closing the console". "Works for me" doesn't adequately answer the question and I'm explaining why it might not work for others. Your solution is SSH server and client implementation and setting dependent.
More specifically your answer says to start it "disconnected from the console". That is not what & does, and your answer does not explain why you experience what you experience. When you are provided a counter example you update with "works for me". I'm trying to explain why it might not work for others.
 
@coderjoe: Don't get defensive, I'm not trying to argue, I'm trying to understand real world scenarios where it won't work for others (or indeed, me!). So what real-world scenario have you used to make this fail? Not theoretical, not sending exactly the right signal -- a real world situation. I tried killing things without warning, for instance, as that seems like a real sort of situation. Once I understand where it may fail, and why it works for me, I'll either update or delete, whichever seems most appropriate. (Most likely update; I suspect there's good info here if distilled.)
 
5:22 PM
@T.J.Crowder sorry if I came across as defensive. I was trying to be explicit to clear up confusion. The most obvious example would be closing a terminal window in which SSH is running (not typing exit). If you execute with & it may not keep running according to the examples I present above. If you execute it with nohup it is guaranteed to keep running.
 

last day (15 days later) »