1

I am running a script from one machine on another machine using ssh.

ssh <host> a.pl

Now, a.pl starts several other instances of a.pl using the 'system' method and executing each of them in the background. I have this integrated with LDAP and when one of the instances dies, the main a.pl script comes to know about this as it continuously keeps on monitoring them. Consequently, on finding that one of the instance has died, the main script dies too and the remaining instances are then owned by init (1).

I would expect ssh to exit when this main script dies. But, somehow it seems that ssh keeps on waiting on all the processes that were launched. ssh exits when I kill the remaining instances.

Is this the expected behavior? If yes, why is this so?

1 Answer 1

1

Yes. This is expected behaviour. You don't want to have processes from logged out users hanging around your system. For this use case, there are services.

Internally, it is basically caused by the fact, that ssh is waiting for the time when the executed process will close file handles (stdin, stdout, stderr), which are redirected to client. By running another scripts, they will inherit the same file handles and so they are not completely closed before all of the children will completely close them.

1
  • I figured this out and this is indeed because of open file handles that are owned by the sshd process. The forked instances were writing stdout to a FIFO pipe, which was owned by sshd, and hence ssh was not exiting. Redirecting stdout to a file or /dev/null solved the problem for me. Thanks for your help, @Jakuje.
    – bpositive
    Jul 30, 2015 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.