5

Is it possible to send a SIGTERM (or other) signal to a process inside ssh, for example:

ssh hostname 'sleep 10; echo done'

What can I do to interrupt the sleep command? If I press ctrl-c, the ssh command gets interrupted.

jabalsad
  • 1,527

3 Answers3

2

It is possible to propagate ctrl+c through to the remote process by implementing an "EOF to SIGHUP" converter via a named pipe on the remote host (see: ssh command unexpectedly continues on other system after ssh terminates).

ssh localhost '
TUBE=/tmp/myfifo.fifo
rm -f "$TUBE"
mkfifo "$TUBE"
<"$TUBE" sleep 100 &  appPID=$!
dd of="$TUBE" bs=1 2>/dev/null
kill $appPID
#kill -TERM -- -$$  # kill entire process group
rm -f "$TUBE"
'
tarok
  • 21
1

-t might do what you want (see https://superuser.com/a/20708/36198). Unfortunately, if you want to read stdin as well, you'll need to do that in two steps, only having Ctrl+C for the second, e.g.

tmp=$(bzcat foo.bz2 | ssh $user@$host '
    t=$(mktemp -t foo.XXXXXXXXXXX);
    cat >"$t";
    echo "$t";
')
ssh -t $user@$host "./cmd \"$tmp\""
ssh $user@$host "rm -f \"$tmp\""    
unhammer
  • 232
  • 1
  • 13
0

If you knew the pid of the remote process then you could do: ssh hostname 'kill -TERM $pid'

Dan D.
  • 6,342