How to send SIGINT (Ctrl-C) to current remote process over SSH (without -t option)

后端 未结 2 1707
逝去的感伤
逝去的感伤 2021-01-05 11:37

I need to send a SIGINT to the remote process running in foreground in an SSH session.

The SSH session is already established, so I cannot use option of starting it

2条回答
  •  长情又很酷
    2021-01-05 12:15

    Short answer:

    ssh -t fs "stty isig intr ^N -echoctl ; trap '/bin/true' SIGINT; sleep 1000; echo f" > foo
    

    and stop the program by CTRL+N.

    Long explanation:

    1.You must use stty option intr to change your server or local interrupt character to not collide with each other.In the command above I've changed the server interrupt character to CTRL+N. You can change your local interrupt character and leave the server's one without any changes.

    2.If you don't want the interrupt character to be in your output (and any other control character) use stty -echoctl.

    3.You must assure that control characters are switched on on the server bash invoked by sshd . If you don't you can end up with processes still hanging around after you logout. stty isig

    4.You actually catch SIGINT signal by trap '/bin/true' SIGINT with empty statement. Without the trap you will not have any stdout after SIGINT signal on your end.

    But without -t option that cannot be quit with a signal to the process that runs over the terminal.

提交回复
热议问题