4

I have a script that looks somewhat like this:

command1
command2
ssh login@machine_with_lots_of_ram:~/script_that_needs_ram.sh
command4 output_file_from_above

where command 4 needs the output of the ssh command.

How do I tell the program to wait until the ssh script finishes before continuing? or better yet, how do I set the ssh script running on the remote machine after command 1 and hold the program from running command 4 until it finishes?

  • 3
    Why do you think that it is isn't waiting? What results have you seen? – Stephen Rauch Jan 19 '17 at 22:44
4

The SSH session will not exit until the command (script) has finished executing on the remote server.

Depending on whether the script outputs data to a file on the server or if it outputs data to standard output you can do one of two things.

  1. If it outputs data to a file on the server:

    ssh user@host script.sh
    scp user@host:remote_output local_output
    process_output local_output
    

    This basically uses scp to copy the data over from the server to the local machine.

  2. If it outputs data to standard output:

    ssh user@host script.sh >local_output
    process_output local_output
    

    This redirects the standard output of the script to a local file.

To set the program running first and then wait:

ssh user@host script.sh &

# do other stuff

wait
scp user@host:remote_output local_output
process_output local_output

or

ssh user@host script.sh >local_output &

# do other stuff

wait
process_output local_output

The wait will pause the script until the ssh command (which runs as a background process) exits.

0

If you need to capture the output of the remote command, something like this should work:

command1
command2
ssh login@machine_with_lots_of_ram "~/script_that_needs_ram.sh" > remote_output.log
command4 remote_output.log
# optionally:  rm remote_output.log

If command4 will take input from standard input, you can even:

ssh login@machine_with_lots_of_ram "~/script_that_needs_ram.sh" | command4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.