26

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

5 Answers 5

24

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.

0
4

When your ssh session ends, your shell will get a SIGHUP. (hang-up signal). You need to make sure it sends that on to all processes started from it. For bash, try shopt -s huponexit; your_command. That may not work, because the man page says huponexit only works for interactive shells.

I remember running into this with users running jobs on my cluster, and whether they had to use nohup or not (to get the opposite behaviour of what you want) but I can't find anything in the bash man page about whether child processes ignore SIGHUP by default. Hopefully huponexit will do the trick. (You could put that shopt in your .bashrc, instead of on the command line, I think.)

Your ssh -t should work, though, since when the connection closes, reads from the terminal will get EOF or an error, and that makes most programs exit.

2

Do you know what the options you're passing to ssh do? I'm guessing not. The -n option redirects input from /dev/null, so the process you're running on the remote host probably isn't seeing SIGINT from Ctrl-C.

Now, let's talk about how bad an idea it is to allow remote root logins:

It's a really, really bad idea. Have a look at HOWTO: set up ssh keys for some suggestions how to securely manage remote process execution over ssh. If you need to run something with privileges remotely you'll probably want a solution that involves a ssh public key with embedded command and a script that runs as root courtesy of sudo.

1
trap "some_command" SIGINT

will execute some_command locally when you press Ctrl+C . help trap will tell you about its other options.

Regarding the ssh issue, i don't know much about ssh. Maybe you can make it call ssh -n -x root@db-host 'killall mysqldump' instead of some_command to kill the remote command?

0

What if you don't want to require using "ssh -t" (for those as forgetful as I am)?

I stumbled upon looking at the parent PID, because CTRL/C from the initiating session results in the ssh-launched process on the remote process exiting, although its child process continues. By way of example, here's my script that is on the remote server.

#!/bin/bash
Answer=(Alive Dead)
Index=0
while [ ${Index} -eq 0 ]; do
  if ! kill -0 ${PPID} 2> /dev/null ; then Index=1; fi
  echo "Parent PID ${PPID} is ${Answer[$Index]} at $(date +%Y%m%d%H%M%S%Z)" > ~/NowTime.txt
  sleep 1
done

I then invoke it with "ssh remote_server ./test_script.sh" "watch cat ~/NowTime.txt" on the remote server shows the timestamp in the file increasing and declaring that the parent process is alive; once I hit CTRL/C in the launching process, the script on the remote server notes that its parent process has died, and the script exits.

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.