Cannot get cron to work on Amazon EC2?
Cron can be run in Amazon-based linux server just like in any other linux server.
- Login to console with SSH.
- Run
crontab -e
on the command line. - You are now inside a vi editor of the crontab of the current user (which is by default the console user, with root permissions)
- To test cron, add the following line:
* * * * * /usr/bin/uptime > /tmp/uptime
- Now save the file and exit vi (press Esc and enter :wq).
- After a minute or two, check that the uptime file was created in /tmp (
cat /tmp/uptime
). - Compare it with the current system uptime by typing the
uptime
command on the command line.
The scenario above worked successfully on a server with the Amazon Linux O/S installed, but it should work on other linux boxes as well. This modifies the crontab of the current user, without touching the system's crontabs and doesn't require the user inside the crontab entry, since you are running things under your own user. Easier, and safer!
On some AWS Ubuntu EC2 machines, cron jobs cannot be edited or made to run by using crontab -e
or even sudo crontab -e
(for whatever reason). I was able to get cron jobs working by:
touch /home/ubuntu/crontest.log
to create a log filesudo vim /etc/crontab
which edits the system-wide crontab- add your own cron job on the second to last line using the root user, such as
* * * * * root date && echo 'It works!'>> /home/ubuntu/crontest.log 2>&1
which dumps stdout and stderr into the logfile you created in step 1 - Verify it is working by waiting 1 minute and then
cat /home/ubuntu/crontest.log
to see the output of the cron job
Your cron daemon is not running. When you're running ps aux | grep crond
the result is showing that only the grep command is running. Be aware of this whenever you run ps aux | grep blah
.
Check the status of the cron service by running this command.
Try:
sudo service crond status
Additional information here: http://www.cyberciti.biz/faq/howto-linux-unix-start-restart-cron/.