Cannot get cron to work on Amazon EC2?

2020-02-04 08:14发布

问题:

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.

回答1:

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!



回答2:

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



回答3:

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:

  1. touch /home/ubuntu/crontest.log to create a log file
  2. sudo vim /etc/crontab which edits the system-wide crontab
  3. 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
  4. Verify it is working by waiting 1 minute and then cat /home/ubuntu/crontest.log to see the output of the cron job


回答4:

Don't forget to specify the user to run it as. Try creating a new file inside your /etc/cron.d folder named after what you want to do like getnytimes and have the contents of that file just be:

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



回答5:

In my case the cron job was working but the script failed it was running failed. The failure reason was due to the fact that I used relative path instead of absolute path in my include line inside the script.



回答6:

running

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

gives me an error

/home/ubuntu/backups/testfile: No such file or directory

is this your issue? I guess cron is not writing this error to anywhere you can redirect stderr to stdout and see the error like this :

02 * * * * /usr/bin/wget -O /home/ubuntu/backups/testfile http://www.nytimes.com/ > /home/ubuntu/error.log 2&>1 


回答7:

One of the possible reasons for cronjob not running: Ensure that the server time and your cronjob time are same. I wasted a lot of time because of this.