When you run ./a.out
in a terminal over SSH, and then the SSH session times out, ./a.out
is killed. Why is this? Some say that it's because of SIGHUP. But running nohup ./a.out
still has the same problem in that when the SSH session times out, the nohup ./a.out
process is still killed.
I've seen some say that it's because children are killed when the parent is killed. But I've seen it claimed elsewhere that this isn't normally the case.
An alternative explanation is that the stdout of ./a.out is tied to the terminal. Thus, after the terminal is killed, when ./a.out tries writing to the terminal, it will receive a SIGPIPE, which will cause it to be killed.
What is the real reason why nohup ./a.out
dies when SSH times out?
Here is the actual command I'm running: nohup ./a.out |& tee logs.out
&
operator, which I assume will bind the stdin and stdout pipes elsewhere? – wonderer Jul 15 '21 at 14:23&
operator; but that doesn’t bind inputs or outputs anywhere, that’snohup
’s job. – Stephen Kitt Jul 15 '21 at 14:30nohup ./a.out |& tee logs.out
, only./a.out
is actually running withnohup
to protect it from dying. Thetee
is a separate process but pipes fail if one part of them fails, so as soon as your session exits,a.out
keeps running (nohup) but thetee
fails (no nohup) so the entire pipe, includinga.out
is killed. – terdon Jul 15 '21 at 14:32./a.out &
, it continues running even after my SSH session exits. – wonderer Jul 15 '21 at 14:36nohup
might not be necessary in your case, but it’s easier to use it than wonder ;-). – Stephen Kitt Jul 15 '21 at 14:44