bash脚本在ssh之后执行命令 [英] bash script execute commands after ssh
问题描述
我试图通过我的第一个脚本执行一些命令,但是它不起作用.
I am trying to execute a few commands via my first script but it's not working.
#!/bin/bash
#connect to server
echo "Connecting to the server..."
ssh -t root@IP '
#switch user to deploy
su - deploy
#switch path
echo "Switching the path"
cd /var/www/deploys/bin/app/config
#run deploy script
echo "Running deploy script"
/usr/local/bin/cap -S env=prod deploy
#restart apache
sudo /bin/systemctl restart httpd.service
bash -l
'
发生了什么事?我已成功连接到服务器,更改了用户,然后我什么也没看到.当我像在终端中一样按ctrl + c时,应该执行的命令的一些输出出现了,但是有一些错误.
What is happening? I am successfully connected to the server, the user is changed and then I don't see nothing happening. When I press ctrl + c just like that in terminal, some output from the command that should be executed appears but there are some errors.
为什么启动脚本后我看不到终端中发生的一切?我做错了吗?
Why I don't see everything what is happening in terminal after launching the script? Am I doing it the wrong way?
顺便说一句:当我尝试手动连接并自己运行命令时,一切运行正常.
BTW: when I try connect manually and run the commands myself, everything is working nicely.
使用CentOS 7.
Using CentOS 7.
推荐答案
通过ssh登录并执行一组命令的简单方法是
Clean way to login through ssh and excecute a set of commands is
ssh user@ip << EOF
#some commands
EOF
此处EOF
用作命令列表的定界符
here EOF
acts as the delimitter for the command list
脚本可以修改为
ssh -t root@IP << EOF
#switch user to deploy
su - deploy
#switch path
echo "Switching the path"
cd /var/www/deploys/bin/app/config
#run deploy script
echo "Running deploy script"
/usr/local/bin/cap -S env=prod deploy
#restart apache
sudo /bin/systemctl restart httpd.service
bash -l
EOF
将执行该命令并在此之后关闭连接
will excecutes the command and closes the connection there after
这篇关于bash脚本在ssh之后执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!