1

I need to run some commands locally and then some command on a remote machine all using a single local bash script.

For simplicity just say I want to do the following and execute it on my local desktop machine.

#!/bin/bash

#upload some files to a remote machine
cd /tmp
./upload-files.sh

#run commands on remote machine
ssh myuser:[email protected]
cd /tmp/uploads    <--- these commands don't run in the ssh connection
./process-uploads.sh
exit

#run command locally again.
cd -
echo 'complete!'

Any ideas of how to do this?

2 Answers 2

1

You can use here-doc with ssh command:

#!/bin/bash

#upload some files to a remote machine
cd /tmp
./upload-files.sh

#run commands on remote machine
ssh -t -t myuser:[email protected]<<EOF
cd /tmp/uploads
./process-uploads.sh
exit
EOF

#run command locally again.
cd -
echo 'complete!'
Sign up to request clarification or add additional context in comments.

2 Comments

Do I need the double -t -t?
With single -t I got a warning Pseudo-terminal will not be allocated because stdin is not a terminal.
0

If you want to run only one command:

ssh myuser:[email protected] 'cd /tmp/uploads; ./process-uploads.sh'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.