如何在 AWS EC2 中通过用户数据脚本添加 crontab 调度程序?



我正在尝试添加一个 crontab,以便我可以通过用户数据脚本每 5 分钟获取一次磁盘空间已用和磁盘空间利用率的 cloudwatch 指标。 下面是我的用户数据脚本:

#!/bin/bash
sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64
curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
unzip CloudWatchMonitoringScripts-1.2.2.zip && rm CloudWatchMonitoringScripts-1.2.2.zip && cd aws-scripts-mon
crontab<<EOF
*/1 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl -mem-util --mem-used --mem-avail --swap-util --disk-space-util --disk-space-used --disk-space-avail --memory-units=megabytes --disk-path=/dev/xvda1 --from-cron
EOF
./mon-put-instance-data.pl -mem-util --mem-used --mem-avail --swap-util --disk-space-util --disk-space-used --disk-space-avail --memory-units=megabytes --disk-path=/dev/xvda1

从 aws 终端运行时,所有这些步骤都正常工作, 在云初始化日志中也没有步骤失败。我第一次能够获得云观察指标,但在那之后,它们没有通过,所以这意味着 crontab 不起作用,如何解决这个问题?

不要使用 -e,因为那是编辑模式,你想要这样的东西:

(crontab -l 2>/dev/null || echo ""; echo "*/5 * * * * /path/to/job -with args") | crontab -

更新:添加了空的回显管道,以避免用户数据因缺少root的crontab列表失败而停止(userdata的默认运行用户(,这将返回非零并导致脚本失败。

注意:另请注意,默认情况下,Userdata 仅运行一次,而不是每次重新启动实例时。因此,在实例停止时更改用户数据不允许您测试不同的方法,除非您按照本文档进行修改。如果设置为每次都运行,上面的脚本也会将相同的规则一遍又一遍地连接到 crontab!

您也可以使用 heredoc 以更简洁的方式实现此目的

crontab<<EOF
* * * * * script.sh
EOF

如果您想附加到现有的 crontab,请执行以下操作

crontab<<EOF
$(crontab -l)
* * * * * script2.sh
EOF

现在列出 crontab 使用

crontab -l

此外,手册页说每个用户都可以有自己的 crontab,虽然这些文件是/var/spool/cron 中的文件,但它们并不打算直接编辑。

例如,如果您以 root 用户身份创建 cron,则相应用户的 cron 文件将是

/var/spool/cron/root

请详细查看以下内容

[root@localhost ~]# crontab -l
no crontab for root
[root@localhost ~]# crontab<<EOF
*/5 * * * * script1.sh
EOF
[root@localhost ~]# crontab -l
*/5 * * * * script1.sh
[root@localhost ~]# crontab<<EOF
*/10 * * * * script2.sh
EOF
[root@localhost ~]# crontab -l
*/10 * * * * script2.sh
[root@localhost ~]# crontab<<EOF
$(crontab -l)
* * * * * script3.sh
EOF
[root@localhost ~]# crontab -l
*/10 * * * * script2.sh
* * * * * script3.sh
[root@localhost ~]# crontab<<EOF
$(crontab -l)
* * * * * script4.sh
EOF
[root@localhost ~]# crontab -l
*/10 * * * * script2.sh
* * * * * script3.sh
* * * * * script4.sh
[root@localhost ~]# cat /var/spool/cron/root
*/10 * * * * script2.sh
* * * * * script3.sh
* * * * * script4.sh
[root@localhost ~]#

在您的情况下,它看起来像

#!/bin/bash
sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64
curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
unzip CloudWatchMonitoringScripts-1.2.2.zip && rm CloudWatchMonitoringScripts-1.2.2.zip && cd aws-scripts-mon
crontab<<EOF
echo $'i*/5 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl -mem-util --mem-used --mem-avail --swap-util --disk-space-util --disk-space-used --disk-space-avail --memory-units=megabytes --disk-path=/dev/xvda1 --from-cronE:xn'
EOF
./mon-put-instance-data.pl -mem-util --mem-used --mem-avail --swap-util --disk-space-util --disk-space-used --disk-space-avail --memory-units=megabytes --disk-path=/dev/xvda1

这篇文章很旧,但我花了太长时间寻找这个解决方案。

我试图自动将 cronjob 添加到 ec2,这样我就不必手动安排作业或添加脚本。 所有这些都是通过CloudFormation中的用户数据完成的。 我的解决方案要求我使用系统管理器。U 可能需要将系统管理器更改为 ec2-user。

为此,我使用了:

crontab -u ssm-user/path/to/script

我尝试了其他 5 种在控制台中工作但在用户数据中不起作用的解决方案。我真的希望这有助于其他人在未来自动化他们的部署

为了在 crontab 中添加行,我在用户数据脚本中使用这些行,它对我来说工作正常

须藤苏

crontab -u ec2-user -l

echo -e "*/5 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl -mem-util --mem-used --mem-avail ---swap-util --disk-space-util --disk-space-used --disk-space-avail --memory-units=megabytes --disk-path=/dev/xvda1 --from-cron " |crontab -u ec2-user -

退出

帮助我找到解决方案的链接是 https://serverfault.com/questions/296949/programmatically-add-entry-to-users-crontab

我遇到了类似的问题,就像其他人在这里说的那样,这些命令在 CLI 中工作正常,但通过用户数据添加时不起作用。经过几个小时的挠头搜索,终于意识到我必须在 crontab 设置命令之前添加行"#!/bin/bash"。这解决了我的问题,一些经验丰富的人可能已经知道这一点,但如果你对这个领域有点陌生,可能不知道这一点。我的脚本如下所示。

# !/bin/bash
sudo su 
echo '*/1 * * * * <path-to-python> <path-to-python-script>' > /tmp/mycrontab.txt
sudo -u ubuntu bash -c 'crontab /tmp/mycrontab.txt'

最新更新