我在本地系统上有一个脚本,它调用远程服务器(服务器A)上的脚本来执行。脚本中触发远程服务器脚本的命令类似于
ssh <user>@<server ip> "bash -s < ./shell_backup_trx_db.sh"
远程服务器上的脚本shell_backup_trx_db.sh
进行数据库备份并将其放在服务器A文件系统上。如何在本地脚本内部使用上述命令确保ssh命令成功执行并继续提前执行脚本或者中止它?
答案 0 :(得分:1)
只要远程脚本返回适当的退出代码,就不会有问题。
只有在前一个命令退出而没有错误时才使用条件控制操作符&&
继续执行下一个命令:
ssh <user>@<server ip> "/path/to/shell_backup_trx_db.sh" && echo "Done with backup"
编辑:如果要根据ssh命令是通过还是失败执行多个命令,可以使用if..else..fi
语句。
示例:
if ssh <user>@<server ip> "/path/to/shell_backup_trx_db.sh";
then
echo "Backup completed successfully";
./next_script.sh;
else
echo "Backup failed";
exit;
fi
答案 1 :(得分:1)
要在ssh失败时(无论出于何种原因)退出脚本,你可以使用类似的东西。
if ! ssh ...; then
echo ssh failed
exit
fi
答案 2 :(得分:0)
if ssh <user>@<server-ip> stat shell_backup_trx_db.sh \> /dev/null 2\>\&1
then
# Trigger Production Database back for both mysql and mongo.
ssh <user>@<server-ip> "bash -s < ./shell_backup_trx_db.sh"
echo "Database backup successfully completed."
# Prompt user to check manually that database backup is completed successfully.
read -p "Ensure database backup process is successfully completed. Login to dev/stg server and run 'ls command'. Press [Enter] key to continue execution."
else
# Backup script could not be accessed, abort script execution any further.
echo "Back up script not found, aborting script execution, Please take backup before." 1>&2
exit 1
fi