30

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.

4
  • Does this help: stackoverflow.com/questions/13204/…
    – Mike Hogan
    Commented May 6, 2012 at 16:34
  • I would try something simple first to evaluate if the problem is with your command or something else: i.e. try to put: echo "xxx" >> /var/tmp/myfile.txt and see if it was run by checking the /var/tmp/myfile.txt
    – maiklos
    Commented May 6, 2012 at 16:38
  • and: echo "xxx" > /home/ubuntu/backups/testfile.txt ? is it able to create the file if you put it into cron ?
    – maiklos
    Commented May 7, 2012 at 9:32
  • @maiklos Yes, this also works. Still, I can not get crontab to work though. I've been working on this for days now.
    – turtle
    Commented May 7, 2012 at 10:23

7 Answers 7

46

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
  • 3
    extra tip, re step #6, once can also do tail -F /tmp/uptime (-F means it can start watching before the file exists)
    – floer32
    Commented Aug 23, 2017 at 0:19
  • Technically -F is the same as --follow=name --retry. The former option means it can follow new files with the same file name if files are rotated, and the latter means it will keep trying to open a file until it finds it (i.e. what you mentioned). Commented Feb 7, 2018 at 20:49
35

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

5
  • 4
    Had a situation where my cron was not working (the daemon was running because it was outputting the following message to the log - FAILED to authorize user with PAM (Module is unknown)). Based on this answer, restarted the daemon with /etc/init.d/crond restart and cron started working. Commented Apr 14, 2014 at 15:41
  • Restarting crond also did it for me. Not sure why it stopped working in the first place though. Anyone have an idea where to look? Cron log file doesn't show anything interesting.
    – Nick M
    Commented Aug 11, 2014 at 19:37
  • 1
    I'd also be interested in finding the actual solution to FAILED to authorize user with PAM (Module is unknown) rather then just restarting cron each time. I've had it happen fairly frequently. I only really use cron for logrotate, so if the drive starts filling up first thing I usually do is check for the PAM error. I'd like to solve it so it doesn't happen anymore.
    – jamiegs
    Commented Apr 28, 2015 at 18:34
  • Should that be cron with a d?: sudo service crond start
    – David
    Commented Jan 18, 2017 at 22:04
  • 3
    (default) AWS ec2 instances will use sudo service crond status | start | stop | restart - also stated in the article you linked
    – goredwards
    Commented Apr 11, 2017 at 21:55
17

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
  • I've confirmed this method to work. Editing /etc/crontab was the only way I could get CRON to pick up my jobs on Ubuntu EC2. Thanks @enharmonic.
    – Zach
    Commented Apr 11, 2019 at 13:22
  • This solved the problem for the Amazon Linux machines too!
    – Kathir
    Commented Nov 24, 2020 at 7:23
  • works for me too. thanks @enharmonic
    – zharif
    Commented Dec 20, 2021 at 1:52
  • this was the only way I could find that works on Elastic Beanstalk - replace (home with ec2-user ) Commented Aug 6, 2022 at 11:07
3

What did the trick for me was

  1. Make sure the crontab was active:
    sudo service crond status
  1. Restart the crontab by running:
    sudo service crond restart
  1. Reschedule the cron job as usual:
    crontab -e
3

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/
3
  • Ok, thanks. How do I tell cron to 'run' the file once I've created it?
    – turtle
    Commented May 6, 2012 at 20:51
  • It looks like cron should pickup this file automatically once it's placed here. Unfortunately, it still doesn't work.
    – turtle
    Commented May 7, 2012 at 10:24
  • 1
    The user is not essential if you are using "crontab -e" - which opens the vi editor, where you can edit the crontab for the current user. The format is the same as the root cromtab but without the user. And the crontab starts running while I press ZZ (or :wq) on the vi editor. Sample cron line to test it's running: "* * * * * /usr/bin/uptime > /tmp/uptime"
    – Druvision
    Commented Jun 7, 2013 at 14:10
2

In my case the cron job was working but the script 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.

0

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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.