Disclaimer: the answer below is not for SIGTERM, but SIGINT. This is not an answer to the question due to oversight.
The problem you are observing is due to a missing tty which should be in control of the process you try to run. If there is no tty available, ssh is unable to send the signals to the process. When you use the option -t
to the ssh
command, it will force pseudo-terminal allocation which makes it possible to send signals over ssh :
ssh -t root@localhost /root/print-signal.py
man ssh
-t
Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t
options force tty allocation, even if ssh has no local tty.
A very nice explanation how and why is given by Giles on unix.stackexchange.
Here you see how it works :
[terminal 1]% ssh server ./print_signal.py
On an other terminal you see then that print_signal.py
is running on PID=26992
under the ssh
with PID=26991
without a tty (username@notty
)
[terminal 2]% ssh server ps -f
UID PID PPID C STIME TTY TIME CMD
username 26991 26989 0 17:06 ? 00:00:00 sshd: username@notty
username 26992 26991 0 17:06 ? 00:00:00 python ./print_signal.py
username 27347 27345 0 17:07 ? 00:00:00 sshd: username@notty
username 27348 27347 0 17:07 ? 00:00:00 ps -f
After killing the ssh process with kill or CTRL-C, the process is still active but runs now under /sbin/init
(PPID=1
)
[terminal 2]% ssh server ps -f
UID PID PPID C STIME TTY TIME CMD
username 26992 1 0 17:06 ? 00:00:00 python ./print_signal.py
username 27453 27451 0 17:08 ? 00:00:00 sshd: username@notty
username 27454 27453 5 17:08 ? 00:00:00 ps -f
using the -t
flag nicely kills the process on the other side:
[terminal 1]% ssh -t server ./print_signal.py
On an other terminal you see then that print_signal.py
is running on PID=39277
under the ssh
with PID=39276
bound to a tty (username@pts/10
)
[terminal 2]% ssh server ps -U username -f
UID PID PPID C STIME TTY TIME CMD
username 39276 39274 0 17:22 ? 00:00:00 sshd: username@pts/10
username 39277 39276 1 17:22 pts/10 00:00:00 python ./print_signal.py
username 39317 39314 0 17:22 ? 00:00:00 sshd: username@notty
username 39318 39317 5 17:22 ? 00:00:00 ps -U username -f
After killing the ssh process
[terminal 1]% ssh -t server ./print_signal.py
My PID: 39277
^CCaught signal SIGINT (2), exiting.
Connection to server closed
The process is now clearly terminated on the other server
[terminal 2]% ssh server ps -f
UID PID PPID C STIME TTY TIME CMD
username 39768 39765 0 17:26 ? 00:00:00 sshd: username@notty
username 39769 39768 6 17:26 ? 00:00:00 ps -U username -f