Send SIGTERM signal to a process running inside ssh

4

3

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

Posted 2012-01-25T10:47:48.177

Reputation: 1 137

Are you looking for something as described in the following question? How to inject commands at the start of an interactive SSH session? Unfortunately the only answer is not probably suitable for you.

– pabouk – 2014-02-13T19:02:43.227

Answers

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

Posted 2012-01-25T10:47:48.177

Reputation: 21

0

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

Dan D.

Posted 2012-01-25T10:47:48.177

Reputation: 5 138

Pity, I hope this is not the only solution :) Thanks for your answer! – jabalsad – 2012-01-25T11:38:14.320

0

-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

Posted 2012-01-25T10:47:48.177

Reputation: 183