Crontab in AWS CloudFormation Userdata

后端 未结 3 948
春和景丽
春和景丽 2021-02-10 13:21

How to set crontab when using AWS CloudFormation Userdata?

I am setting

(crontab -l ; echo \"0 * * * * wget -O - -q http://www.example.com/cron.php\") |          


        
相关标签:
3条回答
  • 2021-02-10 13:29

    I had similar issue like others are saying here that these commands work fine from CLI but when added through userdata not working. After several hours of head scratching search finally realized I had to add line "#!/bin/bash" before crontab setup commands. This fixed my problem, some very well experienced people might know this already but if you are bit new to this realm may not know this. My script look like below.

    !/bin/bash sudo su echo '*/1 * * * * command1 command2' > /tmp/mycrontab.txt sudo -u ubuntu bash -c 'crontab /tmp/mycrontab.txt'

    0 讨论(0)
  • 2021-02-10 13:34

    To do this properly you should do the following inside the bash script:

    crontab -l > /tmp/mycrontab
    echo '0 * * * * wget -O - -q http://www.example.com/cron.php' >> /tmp/mycrontab
    crontab /tmp/mycrontab
    
    0 讨论(0)
  • 2021-02-10 13:38

    This will work, set this in your template, for your instance:

    "UserData": {
        "Fn::Base64": {
            "Fn::Join": [
                "",
                [
                    "#!/bin/bash\n",
                    "echo '0 * * * * wget -O - -q http://www.example.com/cron.php' > /tmp/mycrontab.txt\n",
                    "sudo -u ubuntu bash -c 'crontab /tmp/mycrontab.txt'\n",
                ]
            ]
        }
    }
    
    0 讨论(0)
提交回复
热议问题