2015-01-14 - Troubleshooting Cron on EC2
Today I had trouble getting a cron job to work.
The cron command was:
# run the lunchbot every weekday at 12:30 pm
30 12 * * 1-5 /home/ubuntu/bothouse/lunchbot/cron.sh > /home/ubuntu/bothouse/lunchbot/cron.log
The first problem, I discovered, was that cron.sh didn't have permissions set to be executable by the user (ubuntu).
ls -l
total 16
-rw-rw-r-- 1 ubuntu ubuntu 0 Jan 14 13:09 cron.log
-rw-rw-r-- 1 ubuntu ubuntu 164 Jan 14 12:44 cron.sh
-rw-rw-r-- 1 ubuntu ubuntu 1078 Jan 14 11:09 LICENSE
-rw-rw-r-- 1 ubuntu ubuntu 4062 Jan 14 13:07 lunchbot.js
-rw-rw-r-- 1 ubuntu ubuntu 254 Jan 14 11:09 README.md
I fixed that with chmod
chmod u+x cron.sh
In later cases, I also had to make sure cron.log was available with:
chmod u+x cron.log
Ok, then after some testing, I realized that cron was using UTC time, not local time to the machine (US East Coast).
Googling around, I found out how to set the time zone for the EC2 box:
echo "America/New_York" | sudo tee /etc/timezone
sudo dpkg-reconfigure --frontend noninteractive tzdata
According to the documentation, this will also adjust for Daylight Saving Time, which is pretty cool -- and wouldn't have happened if I just used UTC and offset the hours.
So now checking the date gets:
date
Wed Jan 14 12:59:32 EST 2015
BUT! I also discovered that cron doesn't use that UNTIL YOU REBOOT.
So I rebooted my EC2 instance from the AWS dashboard, and now all works as expected.