ssh and shell through ssh : how to exit?

The exit in the shell script does not work because it is exiting from the script, not the shell. To exit from the shell after the script completes do

ssh user@ipaddress '~/my_script.sh && exit'

This will run the script then exit from the shell.


The ssh connection remains open when the process started by ssh (here, a shell) exits, if there are other processes that are still using it. I don't know the exact rules that the ssh daemon follows, but a connection is in use, at least, if the standard output of any child process is still connected to the original pipe provided by ssh. Compare:

ssh somehost 'sleep 5 &'  # exits after 5 seconds
ssh somehost 'sleep 5 >/dev/null &'  # exits immediately seconds

When you start a daemon, you should background it and close its file descriptors. At least, use this:

./qaswvd </dev/null >/dev/null 2>/dev/null &

You may want to add nohup in front; it won't make a difference here but would be useful if the script was run from a terminal. Many programs that are designed to act as daemon have a command-line options to get them to fork, close file descriptors, ignore signals and other niceties. Check in the qaswvd documentation if it has one. You could also investigate “daemonizer” utilities.


I saw this post when I googled for the solution of the same situation. Although it's kinda late to solve @Oliver's problem, since the accepted answer was clearly not working either to OP or me (don't know why it's accepted), I'd still like to post my own solution for future googler. It's simple: add a -f option in ssh command:

ssh -f user@ipaddress '~/my_script.sh'

EDIT

The effect of -f option is to put ssh in the background, so without any further option added it is possible that the connection is still alive even it seems that it's broken. One may refer to the answer given in another post on this site if interested.