【问题标题】:Shell script stops when calling SSH调用 SSH 时 Shell 脚本停止
【发布时间】:2021-03-02 22:04:52
【问题描述】:

我正在尝试使用一个脚本在 AWS 上自动化一些事情。

  1. 登录并关闭 docker-compose 然后删除所有图像
  2. 将本地文件复制到服务器
  3. 登录并启动 docker-compose

我的脚本是

#log in and shut down docker-compose then remove all images
ssh -i "~/Documents/AWS-Keys/mykey.pem" ubuntu@XX.XXX.XX.XXX
docker-compose down
docker image prune -f
exit

#copy local files to server
scp -r -i "~/Documents/AWS-Keys/mykey.pem" ./ubuntu ubuntu@XX.XXX.XX.XXX:/home

#log in and start docker-compose
ssh -i "~/Documents/AWS-Keys/mykey.pem" ubuntu@XX.XXX.XX.XXX
docker-compose up -d
exit

我也试过logout而不是exit,结果一样。

跑步

$ ./upload.sh 

输出是:

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

ubuntu@ip-XXX.XX.X.XXX:~$ 

在得到一些反馈后我也尝试了

ssh -i "~/Documents/AWS-Keys/mykey.pem" ubuntu@XX.XXX.XX.XXX
docker-compose down;
docker image prune -f;
exit

同样的结果。

【问题讨论】:

  • 您尝试了错误的命令。复制并粘贴赞成的答案。

标签: amazon-web-services docker docker-compose sh


【解决方案1】:

我的理解是你想在服务器上运行命令,那样的话就写在ssh之后:

ssh -i "~/Documents/AWS-Keys/mykey.pem" ubuntu@XX.XXX.XX.XXX "docker-compose down ;docker image prune -f"

您可以通过 HEREDOC 发送更长的脚本

ssh -i "~/Documents/AWS-Keys/mykey.pem" ubuntu@XX.XXX.XX.XXX  <<COMMANDS
docker-compose down
docker image prune -f
COMMANDS

【讨论】:

  • 尝试了建议的解决方案。结果相同。
  • 好的,你能用你到底做了什么以及输出是什么来更新这个问题吗?
  • 我在每个命令行之后添加了分号“:”,如上所示。像 docker-compose down 一样;输出仍然和上面一样:输出是:
  • 那么,您是否也有如上所示的引号中的命令?因为没有它,它将在本地执行。或者使用 HEREDOC 尝试其他语法。
猜你喜欢
  • 2019-09-12
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
相关资源
最近更新 更多