【发布时间】:2015-04-17 17:05:28
【问题描述】:
我的 machineA 中有一个 tar (abc.tar.gz) 文件,我需要将其复制并解压到我的 machineB 中。在我的machineB 中,我有一个正在运行的应用程序,它是该 tar 文件的一部分,所以我需要编写 shell 脚本,通过在 machineA 上运行 shell 脚本来在 machineB 上执行以下操作 -
- 在
machineB上执行命令sudo stop test_server - 删除
/opt/data/文件夹中的所有内容,留下该 tar 文件。 - 将该 tar 文件复制到此位置
machineB中/opt/data/ - 现在解压
/opt/data/文件夹中的 tar 文件。 - 现在执行这个命令
sudo chown -R posture /opt/data - 之后执行此命令启动服务器
sudo start test_server
在machineA 中,我会将我的shell 脚本和tar 文件放在同一个位置。到目前为止,我已经通过阅读教程了解了 shell 脚本,因为我最近开始使用 shell 脚本。
#!/bin/bash
set -e
readonly SOFTWARE_INSTALL_LOCATION=/opt/data
readonly MACHINE_LOCATION=(machineB) # I will have more machines here in future.
readonly TAR_FILE_NAME=abc.tar.gz
ssh david@${MACHINE_LOCATION[0]} 'sudo stop test_server'
ssh david@${MACHINE_LOCATION[0]} 'sudo rm -rf /opt/data/*'
sudo scp TAR_FILE_NAME david@${MACHINE_LOCATION[0]}:$SOFTWARE_INSTALL_LOCATION/
ssh david@${MACHINE_LOCATION[0]} 'tar -xvzf /opt/data/abc.tar.gz'
ssh david@${MACHINE_LOCATION[0]} 'sudo chown -R posture /opt/data'
ssh david@${MACHINE_LOCATION[0]} 'sudo start test_server'
我做对了,还是我们可以改进上述 shell 脚本中的任何内容?我需要在我们的生产机器上运行上面的 shell 脚本。
是否可以显示每个步骤的状态消息,说明此步骤已成功执行,因此转到下一步,否则会终止 shell 脚本并显示正确的错误消息?
【问题讨论】: