I've setup a cron job by Ansible but when I use crontab -l
it's said that my cron is empty.
Here's my script to set it up.
- name: Setup cron to run backup.sh every midnight
cron:
name="Backup S3 to GS"
minute="0"
hour="0"
job="sh ~/backup.sh"
cron_file=backup_s3
user=vagrant
But when I go inside the vagrant machine and ls /etc/cron.d/
I can see that backup_s3
file is there. But when I use command crontab -l
it's said it's empty.
This is the content of backup_s3
#Ansible: Backup S3 to GS
0 0 * * * vagrant sh ~/backup.sh
I know that it's not running because I don't get any email saying that the backup is done and when I run the script manually it's working fine.
Okay. There are several layers of confusion here.
First, the crontab you see when you edit (crontab -e
) or view (crontab -l
) is a user cron. This sits in a magic spool directory/file. You can't edit it directly (approximately speaking), and it's not a good place to put any serious crons.
Since you are using cron_file=
, Ansible is doing the appropriate thing by placing an entry in /etc/cron.d/
. That means individual files can be placed there, which is much more sane than trying to edit a document. (look at all the people struggling with lineinfile
here on stackoverflow)
This is why it isn't showing up in crontab -l
, and it's a good thing.
As far as output from cron is concerned, does email even work for your Vagrant system? It likely doesn't. There are good ways around this. First, look at /var/log/cron
. If you don't have one, look for CRON
entries in /var/log/syslog
. They may indicate if there are problems.
Next, crons typically don't have good access to a user shell. That means you should avoid ~
. Further, if your permissions are wrong on backup.sh
, it may not get executed. Finally, you can pipe output so you can see it. Here's what I'd recommend doing with your cron entry:
job="/bin/sh /home/vagrant/backup.sh >> /home/vagrant/backup.log"
You can also modify the minute
/hour
so it runs more frequently- so you don't have to wait overnight to see what is happening.
Once you've done that, you have plenty of places to look for information. There are two places in /var/log
, there's a new backup.log
which will give you information (if it exists, the cron has been run; if there is data in it, you should be able to figure out any problems).
Change the job
line. Look for execution in /var/log
and /home/vagrant/backup.log
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With