在 Ubuntu 服务器启动时拉取 GitHub 仓库

在 Ubuntu 服务器启动时拉取 GitHub 仓库

我有一个自动扩展的 EC2 组,并且我在服务器之间使用 git 进行集中配置。它们设置了密钥,因此不需要密码。

我想确保当实例从 AMI 启动和创建时(可能带有过时的配置),它会使用常规 git pull 命令提取配置。该命令必须以特定用户身份执行,因为只有该用户可以执行 git pull,因为它的主目录中有 RSA 密钥。

因此本质上在服务器启动时某个用户需要在某个存储库中运行 git pull。

答案1

您可以在 /etc/rc.local 中设置该命令。修改镜像中的 rc.local,使其包含以下内容:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

pushd /certain/repository
su -s /bin/bash -c "git pull" <username>
popd
exit 0

只需将其替换为要以何种权限运行“git pull”的用户即可。另外,以防万一,不要忘记运行:

chmod +x /etc/rc.local

这是来自 RedHat 相关文档中有关 rc.local 的部分,但在所有发行版中基本都是相同的:

/etc/rc.d/rc.local 脚本由 init 命令在启动时或更改运行级别时执行。将命令添加到此脚本的底部是一种简单的方法,可以执行必要的任务,例如启动特殊服务或初始化设备,而无需在 /etc/rc.d/init.d/ 目录中编写复杂的初始化脚本并创建符号链接。

相关内容