Help with detaching the screen programmatically

Hi,

I am using screen utility for protecting from ssh disconnects.
My process flow i ssomething like :

a) I start screen from my desktop terminl.

b)In the screen session, i ssh to one host and execute a command

c)Once the above command is completed,i would like to exit via ctr-a or ctrl-d and send a mail to myself.

Currently, i try

command_in_remote_host;logout ;echo "it's done"|mail -s "Completed" $USER@domain.com
command_in_remote_host;exit ;echo "it's done"|mail -s "Completed" $USER@domain.com

But, the last command is not getting executed after the ssh is disconnected.

Is there any way i can programmatically simulate ctrl-a or ctrl-d , so that it can detach and send the mail?

Please advise.

Thanks

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.

The correct sequence of commands:

command_in_remote_host; echo "it's done"|mail -s "Completed" $USER@domain.com ; exit

Hi,

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.

I would like to receive the mail only after the command executed in the remote host ends.so, if I use nohup then I will receive the mail immediately