I'm making deployment scripts for my project, and I'm making a function that should be able to run a command both locally and remotely (on a server through ssh) and return the text output.
When running a local command, I can easily know if the command has finished by waiting for its PID. When I send a command over an open SSH connection, I get output back, but I don't know if the command exited or if the command is still running and will generate more output later.
Currently I've "solved" this by opening a new connection to the server for each command, which needless to say is excruciatingly slow.
This seems to be like it might be a common issue and might have a simple solution, perhaps even something that's built into SSH, so I'm asking here: How can I, over an open SSH connection, know whether the commands I've sent are finished. If I could somehow collect the exit code too that would be great.
To give a more concrete example, this basically demonstrates my problem in Ruby:
io = IO.popen(["ssh", "-q", "my-server"], "r+")
io.write("some command\n")
# Sleep for some arbitrary amount of time, because I don't know when the
# command has finished :(
sleep 1
output = io.readpartial(1_000_000)
io.write("some command\n")
# Sleep for some arbitrary amount of time, because I don't know when the
# command has finished :(
sleep 1
output = io.readpartial(1_000_000)
Mycommnad; InterestingPID=$? ; ... ;wait $InterestingPID
as multi-line command of ssh instruction? BTW can you usewait
?my_command -myoptions AndParameters
) withLetsStoreInterestingPID=$?
just after the command, and after some other commandswait $LetsStoreInterestingPID
...