Question:
How to set crontab when using AWS CloudFormation Userdata?
I am setting
1 2 |
(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?
Answer:
This will work, set this in your template, for your instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
"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
1 2 3 4 5 6 7 8 |
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" |