【问题标题】:how to add crontab scheduler through user-data script in aws ec2?如何通过 aws ec2 中的用户数据脚本添加 crontab 调度程序?
【发布时间】:2019-03-23 09:43:21
【问题描述】:

我正在尝试添加一个 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-terminal 运行时,所有这些步骤都可以正常工作, cloud-init-logs 中也没有任何步骤失败。我第一次能够获取云监视指标,但之后它们没有通过,所以这意味着 crontab 无法正常工作,如何解决?

【问题讨论】:

标签: amazon-web-services cron aws-sdk cloud-init


【解决方案1】:

我遇到了类似的问题,就像其他人在这里所说的那样,这些命令在 CLI 中可以正常工作,但是通过 userdata 添加时不起作用。经过几个小时的头疼搜索终于意识到我必须在 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'

【讨论】:

    【解决方案2】:

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

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

    为此,我使用了:

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

    我尝试了其他 5 种在控制台中有效但在用户数据中无效的解决方案。我真的希望这可以帮助其他人在未来自动化他们的部署

    【讨论】:

      【解决方案3】:

      为了在 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

      【讨论】:

        【解决方案4】:

        您也可以使用 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-cron\E:x\n'
        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
        

        【讨论】:

        • 我尝试了这里的方法,但如果我通过用户数据脚本传递它仍然不起作用,尽管当我从终端尝试时它确实有效
        • 也许你做错了。以简洁的方式发送用户数据脚本。按原样使用上述语法。如果它在终端上工作,它必须在脚本中工作。查看我更新的答案,我也更新了您的问题代码块。
        • 我已经更新了更改,即使您也将其更改为:- crontab
        【解决方案5】:

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

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

        【讨论】:

        • remove ) 字符或添加 ( before echo
        • 我不明白第一个命令将错误流重定向到 /dev/null 的用法。从您的角度来看,这本身就足够了。 echo "*/5 * * * * /path/to/job -with args" | crontab -
        • 我没有从这里得到我的脚本要改变什么
        猜你喜欢
        • 2021-06-14
        • 2012-06-10
        • 2018-10-12
        • 2014-08-09
        • 1970-01-01
        • 1970-01-01
        • 2022-01-20
        • 1970-01-01
        • 2020-01-18
        相关资源
        最近更新 更多