Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get cron to work on Amazon EC2?

Tags:

I've spent two days trying to understand why I can not get cron to work on my Ubuntu EC2 instance. I've read the documentation. Can anyone help? All I want is to get a working cronjob.

I am using a simple wget command to test cron. I have verified that this works manually from the command line:

/usr/bin/wget -O /home/ubuntu/backups/testfile http://www.nytimes.com/

My crontab file looks like this:

02 * * * * /usr/bin/wget -O /home/ubuntu/backups/testfile http://www.nytimes.com/

I have single spaces between the commands and I have a blank line below the command. I've also tried to execute this command from the system level sudo crontab -e. It still doesn't work.

The cron daemon is running:

ps aux | grep crond                                                                                                                    ubuntu    2526  0.0  0.1   8096   928 pts/4    S+   10:37   0:00 grep crond 

The cronjob appear to be running:

$ crontab -l 02 * * * * /usr/bin/wget -O /home/ubuntu/backups/testfile http://www.nytimes.com/ 

Does anyone have any advice or possible solutions?

Thanks for your time.

like image 424
turtle Avatar asked May 06 '12 16:05

turtle


People also ask

Why does my cron job not run?

You might discover that your job works when run via the command line, but won't run via cron. This may be due to the location you're executing from or the PATH you're using. The use of relative paths is a common cause of cron issues. In cron, the PATH is set by default to /bin:/usr/bin .

How do I run a cron job on AWS?

One way of running cron jobs in the cloud is to use a function as a service (FaaS), like Lambda in the AWS ecosystem. Functions execute when they are triggered to do so, and they run code in the cloud without the need to provision or maintain any infrastructure.

How do I know if a cron job is running in EC2 instance?

To check to see if the cron daemon is running, search the running processes with the ps command. The cron daemon's command will show up in the output as crond. The entry in this output for grep crond can be ignored but the other entry for crond can be seen running as root. This shows that the cron daemon is running.


2 Answers

Cron can be run in Amazon-based linux server just like in any other linux server.

  1. Login to console with SSH.
  2. Run crontab -e on the command line.
  3. You are now inside a vi editor of the crontab of the current user (which is by default the console user, with root permissions)
  4. To test cron, add the following line: * * * * * /usr/bin/uptime > /tmp/uptime
  5. Now save the file and exit vi (press Esc and enter :wq).
  6. After a minute or two, check that the uptime file was created in /tmp (cat /tmp/uptime).
  7. 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!

like image 146
Druvision Avatar answered Dec 19 '22 23:12

Druvision


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/.

like image 38
eipark Avatar answered Dec 20 '22 00:12

eipark