Crontab is not working on Amazon EC2 server

11,764

Solution 1

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.

Solution 2

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
Share:
11,764

Related videos on Youtube

Jake
Author by

Jake

Updated on June 04, 2022

Comments

  • Jake
    Jake almost 2 years

    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.

    • pce
      pce over 11 years
      did you also tried the command with the full path to tar?
    • metalheart
      metalheart over 11 years
      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
    • pce
      pce over 11 years
      @Jake you tried the absolute path to the binaries, like /bin/tar and /bin/date in your cronjob?
    • Jake
      Jake over 11 years
      it looks like $(date +%Y%m%d) command is not working.
  • Deepan Prabhu Babu
    Deepan Prabhu Babu over 8 years
    i used ec2-user and it did not work. When i used root it worked. Just in case, this helps some one.
  • D.Tate
    D.Tate almost 8 years
    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)
  • Jamal Alkelani
    Jamal Alkelani over 3 years
    that will not make a difference!
  • Max S.
    Max S. over 3 years
    @dlammy It does make a difference. When I edited cron it didn’t work when I used the nano command.
  • Jamal Alkelani
    Jamal Alkelani over 3 years
    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
  • Max S.
    Max S. over 3 years
    @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.
  • Jamal Alkelani
    Jamal Alkelani over 3 years
    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 howtogeek.com/410995/how-to-change-the-default-crontab-edito‌​r
  • Max S.
    Max S. over 3 years
    @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.