Starting a process over ssh using bash and then killing it on sigint

前端 未结 5 660

I want to start a couple of jobs on different machines using ssh. If the user then interrupts the main script I want to shut down all the jobs gracefully.

Here is a shor

5条回答
  •  执念已碎
    2021-02-02 15:53

    It would definitely be preferable to keep your cleanup managed by the ssh that starts the process rather than moving in for the kill with a second ssh session later on.

    When ssh is attached to your terminal; it behaves quite well. However, detach it from your terminal and it becomes (as you've noticed) a pain to signal or manage remote processes. You can shut down the link, but not the remote processes.

    That leaves you with one option: Use the link as a way for the remote process to get notified that it needs to shut down. The cleanest way to do this is by using blocking I/O. Make the remote read input from ssh and when you want the process to shut down; send it some data so that the remote's reading operation unblocks and it can proceed with the cleanup:

    command & read; kill $!
    

    This is what we would want to run on the remote. We invoke our command that we want to run remotely; we read a line of text (blocks until we receive one) and when we're done, signal the command to terminate.

    To send the signal from our local script to the remote, all we need to do now is send it a line of text. Unfortunately, Bash does not give you a lot of good options, here. At least, not if you want to be compatible with bash < 4.0.

    With bash 4 we can use co-processes:

    coproc ssh user@host 'command & read; kill $!'
    trap 'echo >&"${COPROC[1]}"' EXIT
    ...
    

    Now, when the local script exits (don't trap on INT, TERM, etc. Just EXIT) it sends a new line to the file in the second element of the COPROC array. That file is a pipe which is connected to ssh's stdin, effectively routing our line to ssh. The remote command reads the line, ends the read and kills the command.

    Before bash 4 things get a bit harder since we don't have co-processes. In that case, we need to do the piping ourselves:

    mkfifo /tmp/mysshcommand
    ssh user@host 'command & read; kill $!' < /tmp/mysshcommand &
    trap 'echo > /tmp/mysshcommand; rm /tmp/mysshcommand' EXIT
    

    This should work in pretty much any bash version.

提交回复
热议问题