In a bash script I execute a command on a remote machine through ssh. If user breaks the script by pressing Ctrl+C it only stops the script - not even ssh client. Moreover even if I kill ssh client the remote command is still running...
How can make bash to kill local ssh client and remote command invocation on Crtl+c?
A simple script:
#/bin/bash ssh -n -x root@db-host 'mysqldump db' -r file.sql
As other's have mentioned, once you disconnect from ssh anything running within it is gone.
ssh into your remote box. type screen Then start the process you want. Press Ctrl-A then Ctrl-D. This will detach your screen session but leave your processes running.
Using nohup command to Keep Running SSH Sessions After that you can safely log out. With nohup command we tell the process to ignore the SIGHUP signal which is sent by ssh session on termination, thus making the command persist even after session logout.
Eventual I found a solution like that:
#/bin/bash ssh -t -x root@db-host 'mysqldump db' -r file.sql
So - I use '-t' instead of '-n'. Removing '-n', or using different user than root does not help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With