pigfox
pigfox

Reputation: 1401

Shell script stops when calling SSH

I am attempting to automate a few things on AWS with one script.

  1. log in and shut down docker-compose then remove all images
  2. copy local files to server
  3. log in and start docker-compose

My script is

#log in and shut down docker-compose then remove all images
ssh -i "~/Documents/AWS-Keys/mykey.pem" [email protected]
docker-compose down
docker image prune -f
exit

#copy local files to server
scp -r -i "~/Documents/AWS-Keys/mykey.pem" ./ubuntu [email protected]:/home

#log in and start docker-compose
ssh -i "~/Documents/AWS-Keys/mykey.pem" [email protected]
docker-compose up -d
exit

I have also tried logout instead of exit, same result.

Running

$ ./upload.sh 

The output is:

Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-1038-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Tue Mar  2 21:52:40 UTC 2021

  System load:                      0.07
  Usage of /:                       66.0% of 7.69GB
  Memory usage:                     36%
  Swap usage:                       0%
  Processes:                        115
  Users logged in:                  1
  IPv4 address for xxxxxxxxxxxxxxx: XXX.XX.X.X
  IPv4 address for docker0:         XXX.XX.X.X
  IPv4 address for eth0:            XXX.XX.X.XXX

 * Introducing self-healing high availability clusters in MicroK8s.
   Simple, hardened, Kubernetes for production, from RaspberryPi to DC.

     https://microk8s.io/high-availability

3 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable


Last login: Tue Mar  2 21:51:47 2021 from XXX.XX.X.XXX

[email protected]:~$ 

After getting some feedback I also tried

ssh -i "~/Documents/AWS-Keys/mykey.pem" [email protected]
docker-compose down;
docker image prune -f;
exit

Same result.

Upvotes: 0

Views: 179

Answers (1)

petrch
petrch

Reputation: 1968

My understanding is that you want to run the command on the server, in that case just write it after ssh:

ssh -i "~/Documents/AWS-Keys/mykey.pem" [email protected] "docker-compose down ;docker image prune -f"

a longer script you can send via HEREDOC

ssh -i "~/Documents/AWS-Keys/mykey.pem" [email protected]  <<COMMANDS
docker-compose down
docker image prune -f
COMMANDS

Upvotes: 2

Related Questions