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
Related videos on Youtube
Author by
Jake
Updated on June 04, 2022Comments
-
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 over 11 yearsdid you also tried the command with the full path to tar?
-
metalheart over 11 yearswhat 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 over 11 years@Jake you tried the absolute path to the binaries, like
/bin/tar
and/bin/date
in your cronjob? -
Jake over 11 yearsit looks like $(date +%Y%m%d) command is not working.
-
-
Deepan Prabhu Babu over 8 yearsi used ec2-user and it did not work. When i used root it worked. Just in case, this helps some one.
-
D.Tate almost 8 yearsAdding "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 over 3 yearsthat will not make a difference!
-
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 over 3 yearsAt 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. 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 anano
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 over 3 yearsI'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-editor
-
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.