Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crontab in AWS CloudFormation Userdata

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") | crontab -

But the cron is not setting. Is there a specific format which I should be using?

like image 291
unnik Avatar asked Aug 23 '16 13:08

unnik


2 Answers

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",
            ]
        ]
    }
}

and in YAML

UserData: 
    Fn::Base64:
      Fn::Join:
        - ""
        - - "#!/bin/bash\n"
          - "echo '0 * * * * wget -O - -q http://www.example.com/cron.php' > /tmp/mycrontab.txt"
          - "sudo -u ubuntu bash -c 'crontab /tmp/mycrontab.txt'\n"
like image 91
Daniel777 Avatar answered Sep 28 '22 01:09

Daniel777


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
like image 30
pbrewer Avatar answered Sep 28 '22 01:09

pbrewer