Send signals to remote process with Python

前端 未结 1 476
礼貌的吻别
礼貌的吻别 2021-01-27 15:56

There are two machines, where one has a script wait_for_signal.sh, and the second one has a script named controller.py. The code of each script is show

相关标签:
1条回答
  • 2021-01-27 16:22

    You're invoking ssh with the -T option, meaning that it won't allocate a PTY (pseudo-TTY) for the remote session. In this case, there's no way to signal the remote process through that ssh session.

    The SSH protocol has a message to send a signal to the remote process. However, you're probably using OpenSSH for either the client or the server or both, and as far as I can tell, OpenSSH doesn't implement the signal message. So the OpenSSH client can't send the message, and the OpenSSH server won't act on it.

    There is an SSH extension to send a "break" message which is supported by OpenSSH. In an interactive session, the OpenSSH client has an escape sequence that you can type to send a break to the server. The OpenSSH server handles break messages by sending a break to the PTY for the remote session, and unix PTYs will normally treat a break as a SIGINT. However, breaks are fundamentally a TTY concept, and none of this will work for remote sessions which don't have a PTY.

    I can think of two ways to do what you want:

    1. Invoke ssh with the -tt parameter instead of -T. This will cause ssh to request a TTY for the remote session. Running the remote process through a TTY will make it act like it's running interactively. Killing the local ssh process should cause the remote process to receive a SIGHUP. Writing a Ctrl-C to the local ssh process's standard input should cause the remote process to receive a SIGINT.

    2. Open another ssh session to the remote host and use killall or some other command to signal the process that you want to signal.

    0 讨论(0)
提交回复
热议问题