3

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

0

1 Answer 1

3

In your case, tee logs.out writes to the terminal, and will be killed when it attempts to write after the SSH connection closes. This will in turn cause nohup to be killed as well, the next time a.out writes to its standard output.

If you want nohup ./a.out to survive, you should run it in the background with no pipe, and separately watch the nohup.out file:

nohup ./a.out &
tail -f nohup.out

or, using your log file,

nohup ./a.out > logs.out &
tail -f logs.out
5
  • By "running in the background" you mean launching it with the & operator, which I assume will bind the stdin and stdout pipes elsewhere?
    – wonderer
    Commented Jul 15, 2021 at 14:23
  • Yes, with the & operator; but that doesn’t bind inputs or outputs anywhere, that’s nohup’s job. Commented Jul 15, 2021 at 14:30
  • 1
    @wonderer when you run nohup ./a.out |& tee logs.out, only ./a.out is actually running with nohup to protect it from dying. The tee 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 the tee fails (no nohup) so the entire pipe, including a.out is killed.
    – terdon
    Commented Jul 15, 2021 at 14:32
  • Is the nohup actually needed? I find that when I just run ./a.out &, it continues running even after my SSH session exits.
    – wonderer
    Commented Jul 15, 2021 at 14:36
  • See Difference between nohup, disown and & for a discussion of this. nohup might not be necessary in your case, but it’s easier to use it than wonder ;-). Commented Jul 15, 2021 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .