2

Let's say I start a program with a non-interactive SSH call (without pseudo-terminal) and exit the session via CTRLC:

$ ssh user@server -- sleep 123
^C

This way, the program, in this case sleep 123 for example, is still running on server, even after the SSH session was terminated:

$ ssh user@server -- ps -ef | grep "sleep 123"
user     12430       1  0 19:28 ?        00:00:00 sleep 123

I am aware of ssh's -t option to have SIGINT sent to the remote sleep instead of the local ssh. But I'm looking for a way to make the remote program stop without having to resort to a pseudo-terminal and specify extra options in the ssh call.

3

1 Answer 1

0

I came up with a partial solution for certain cases.

The idea is this: When ssh gets killed on the client, whatever program was being run by the command on server loses its parent process and gets assigned init as its parent process.

So if you're able to edit the program that's running on server to continuously check the parent PID, then you'll notice once it switches to 1 and therefore ssh on the client must have been terminated.

cat <<-"EOF" | ssh user@server sh
while :; do
sleep 1
ppid="$(ps -p "$$" -o ppid= | tr -d "[:blank:]")"
[ "$ppid" = 1 ] && break
done
EOF
1
  • Or see if reading stdin gives EOF or writing stdout gives SIGPIPE (or if ignored EPIPE); many programs do one or both of these automatically, but not sleep. Commented Jan 19, 2023 at 1:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .