This is flawed in several ways:
1) your process flow does NOT protect command_in_remote_host from ssh disconnects!
2) after exit or logout there is no shell to process the other commands.
You have to start the screen utility on the remote host. The commands you execute there will keep running regardless of disconnects.
Your flow should look like this:
a) ssh to host from your desktop terminal
b) start screen on remote host
c) start command in screen session and send notification.
Actually, there is neither screen nor mail/mailx present in the remote host. Hence I started screen from local desktop.
---------- Post updated at 09:04 PM ---------- Previous update was at 06:28 PM ----------
If i press ctrl-a or ctrl-d manually instead of logout/exit as second, then it works fine.
But, i don't see a way to implement programatically ctrl-a or ctrl-d in a shell script.
If there is no screen utility on the remote host you can use nohup instead. It does not offer a virtual terminal like screen but protects the command you started on the remote host from beeing killed when you lose connection.
Without a mailer on the remote host you could use the mailer on your local machine when your ssh-session terminates (something like ssh remote_host "nohup command_on_remote_host"; echo "it's done"|mail -s "Completed" $USER@domain.com ), but I do not think that makes much sense because you'd receive the mail when the ssh-session quits for any reason - disconnects included.