ssh: propagate signal to remote process

3

2

This is the general version of: Send SIGTERM signal to a process running inside ssh

It is possible to send Ctrl-C to the remote process if the process gets a pty (-tt):

# Runs for 5 seconds
(sleep 5; echo '^C'; sleep 5) | time ssh -tt localhost burnP6

I had hoped the same would work for Ctrl-Z, but, alas, no:

# Continues to run - does not suspend
(sleep 5; echo '^Z'; sleep 5) | time ssh -tt localhost burnP6

If I get an interactive session, Ctrl-C and Ctrl-Z work fine.

Is there a way I can send other signals (I am especially interested in Ctrl-Z)?

I cannot use the suggested "ssh hostname 'kill -TERM $pid'" as I do not know the pids on the remote system.

Ole Tange

Posted 2013-05-12T19:40:14.247

Reputation: 3 034

What do you mean by "(sleep 5; echo '^C'; sleep 5)"? – Raúl Salinas-Monteagudo – 2014-10-27T13:29:22.487

Wait 5 seconds. Send Ctrl-C (ASCII character 3, type Ctrl-V Ctrl-C in Bash). Wait 5 seconds. – Ole Tange – 2014-10-27T14:33:26.387

But echoing '^C' does not generate a SIGINT signal. You mean it is pseudocode for doing that by hand? – Raúl Salinas-Monteagudo – 2014-10-27T15:51:14.587

1It does generate INT on the remote if you use ssh -tt. So no: It is not pseudocode. The above example with ^C actually works. – Ole Tange – 2014-10-27T19:32:02.693

Note that Ctrl-Z sends a SIGTSTP, not a SIGTERM. Even if you get Ctrl-Z working you're limited to Ctrl-C, Ctrl-, and Ctrl-Z, not arbitrary signals.

– dimo414 – 2017-03-29T08:09:18.583

Answers

0

Do you really need a tty?

You could just use ssh server killall burnP6 is there is just one process with such name.

Or you could somehow write the IP of your process in a file and then use something like `ssh server kill $(cat /tmp/burnP6.pid)".

Or a simpler and more robust implementation would be to use the package daemon, meant to daemonize otherwise non-daemon-featured programs. It lets you automatically write your daemon's PID to a file and kill it easily. It can even relaunch your process in case it dies.

To start it:

ssh server daemon -n burnP6 -- /usr/bin/burnP6

To stop it:

ssh server daemon --stop -n burnP6 

Raúl Salinas-Monteagudo

Posted 2013-05-12T19:40:14.247

Reputation: 1 058

I do not need a pty, but burnP6 is just an example. The command could be: echo fyrrc 10 | rot13 | bash. In other words you will have no idea of what the command run will be, and other processes may run commands of the same name (and these must be unaffected) so your solution cannot depend on knowing which command will be run. It is also unacceptable to have to type a command: Your solution must be activated by Ctrl-C and Ctrl-Z. – Ole Tange – 2014-10-27T12:55:53.860