首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用SSH时Shell脚本停止

调用SSH时Shell脚本停止
EN

Stack Overflow用户
提问于 2021-03-02 22:04:52
回答 1查看 92关注 0票数 0

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

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

我的剧本是

代码语言:javascript
复制
#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,同样的结果。

正在运行

代码语言:javascript
复制
$ ./upload.sh 

产出如下:

代码语言:javascript
复制
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:~$ 

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

代码语言:javascript
复制
ssh -i "~/Documents/AWS-Keys/mykey.pem" ubuntu@XX.XXX.XX.XXX
docker-compose down;
docker image prune -f;
exit

同样的结果。

EN

回答 1

Stack Overflow用户

发布于 2021-03-02 22:11:46

我的理解是,您希望在服务器上运行命令,在这种情况下,只需在ssh之后编写它:

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

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

代码语言:javascript
复制
ssh -i "~/Documents/AWS-Keys/mykey.pem" ubuntu@XX.XXX.XX.XXX  <<COMMANDS
docker-compose down
docker image prune -f
COMMANDS
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66447824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档