3

Crontab is not working on Amazon EC2 Linux Server.

I have saved below codes in /etc/crontab file

crontab
# For details see man 4 crontabs 
# Example of job definition: 
# .---------------- minute (0 - 59) 
# | .------------- hour (0 - 23) 
# | | .---------- day of month (1 - 31) 
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ... 
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 
# | | | | | 
# * * * * * user-name command to be executed 
* 10 * * * tar cvfpz /home/backup/web_$(date +%Y%m%d).tar.gz /home/web

I have started crontab command already, but this one didn't work.

I also have saved this line in "crontab -e" too, but cron won't work.

* 10 * * * tar cvfpz /home/backup/web_$(date +%Y%m%d).tar.gz /home/web

Is there anyone who had same experience like me?

Thank you.

Jake
  • 1,175
  • 3
  • 16
  • 23
  • did you also tried the command with the full [path](http://stackoverflow.com/questions/2388087/how-to-get-cron-to-call-in-the-correct-paths) to tar? – pce Dec 18 '12 at 11:14
  • what does it say if you redirect output to some file? like this: `* 10 * * * tar cvfpz /home/backup/web_$(date +%Y%m%d).tar.gz /home/web >> /tmp/output.txt 2>&1` – metalheart Dec 18 '12 at 11:24
  • @Jake you tried the absolute path to the binaries, like `/bin/tar` and `/bin/date` in your cronjob? – pce Dec 18 '12 at 11:29
  • it looks like $(date +%Y%m%d) command is not working. – Jake Dec 18 '12 at 11:33

5 Answers5

2

I recently began using Amazon's linux distro on ec2 instances and after trying all kinds of things for cron all I needed was:
sudo service crond start
crontab -e
This allowed me to set a cron job as "ec2-user" without specifying the user. For example:
0 12 * * * python3 example.py
In fact, specifying a user here prevented it from running.

j3py
  • 1,119
  • 10
  • 22
1

Solved the problem.

I used this code and it works!

* 2 * * * root tar cvfpz /home/backup/web_`date +\%Y\%m\%d`.tar.gz /home/web
Jake
  • 1,175
  • 3
  • 16
  • 23
  • 1
    i used ec2-user and it did not work. When i used root it worked. Just in case, this helps some one. – Deepan Prabhu Babu Aug 07 '15 at 01:16
  • Adding "root" in front of my command magically worked also.... @Jake : you can mark to "accept" your own answer. (A little more of a description would be helpful though) – D.Tate Jun 04 '16 at 16:23
0

You should use crontab -e to create cron for the logged user, so that you don't need to inform the username. See here: https://stackoverflow.com/a/16986464/1777152

Community
  • 1
  • 1
lsilvs
  • 104
  • 1
  • 3
0

For people who are dealing with AWS machines and EBS you need to specify the root keyword before the command since ec2-user isn't allowed to run crontabs. Of course there's a way to fix that.

you can edit the crontab by typing sudo nano /etc/cron.d/mycrontabs or crontab -e

* * * * * root bla bla

  • Also make sure e that the file is ended with a new line
Jamal Alkelani
  • 606
  • 7
  • 19
-1

Don't use nano, use the native sudo crontab -e command.

Max S.
  • 1,383
  • 14
  • 25
  • that will not make a difference! – Jamal Alkelani Nov 10 '20 at 12:26
  • @dlammy It does make a difference. When I edited cron it didn’t work when I used the nano command. – Max S. Nov 11 '20 at 00:43
  • At the end they're all the same if you're using Vim, nano, sublime ... etc, I'm pretty sure you've made sth wrong. BTW, I'm using nano always – Jamal Alkelani Nov 11 '20 at 09:23
  • @dlammy It can't be the same thing when editing cron via the native `crontab -e` command and editing it directly via a `nano` or other editors, at least not on my AWS EC2 server. Best practice is to always use the native command. It's convenient to assume it's the same thing until you have actually tested it which I have done. – Max S. Nov 12 '20 at 07:40
  • I'm always using nano for editing anything, and I'm always using nano for editing crontabs, for your knowledge if you dive deep into the crontab -e command you will see it's opening the file using one of the OS editors, and if you don't have a default editor specified in your OS for editing files then you'll be prompted to choose one. please check the following link https://www.howtogeek.com/410995/how-to-change-the-default-crontab-editor/ – Jamal Alkelani Nov 12 '20 at 08:31
  • @dlammy Changing the crontab editor is different from editing the cron directly with the nano command such as `sudo nano /etc/cron.d/mycrontabs`. I am not against changing the crontab editor. – Max S. Nov 12 '20 at 12:08