How to delete a cron job with Ansible? - linux

I have about 50 Debian Linux servers with a bad cron job:
0 * * * * ntpdate 10.20.0.1
I want to configure ntp sync with ntpd and so I need to delete this cron job. For configuring I use Ansible. I have tried to delete the cron entry with this play:
tasks:
- cron: name="ntpdate" minute="0" job="ntpdate 10.20.0.1" state=absent user="root"
Nothing happened.
Then I run this play:
tasks:
- cron: name="ntpdate" minute="0" job="ntpdate pool.ntp.org" state=present
I see new cron job in output of "crontab -l":
...
# m h dom mon dow command
0 * * * * ntpdate 10.20.0.1
#Ansible: ntpdate
0 * * * * ntpdate pool.ntp.org
but /etc/cron.d is empty! I don't understand how the Ansible cron module works.
How can I delete my manually configured cron job with Ansible's cron module?

User's crontab entries are held under /var/spool/cron/crontab/$USER, as mentioned in the crontab man page:
Crontab is the program used to install, remove or list the tables used to drive the cron(8) daemon. Each user can have their own crontab, and though these are files in /var/spool/ , they are not intended to be edited directly. For SELinux in mls mode can be even more crontabs - for each range. For more see selinux(8).
As mentioned in the man page, and the above quote, you should not be editing/using these files directly and instead should use the available crontab commands such as crontab -l to list the user's crontab entries, crontab -r to remove the user's crontab or crontab -e to edit the user's crontab entries.
To remove a crontab entry by hand you can either use crontab -r to remove all the user's crontab entries or crontab -e to edit the crontab directly.
With Ansible this can be done by using the cron module's state: absent like so:
hosts : all
tasks :
- name : remove ntpdate cron entry
cron :
name : ntpdate
state : absent
However, this relies on the comment that Ansible puts above the crontab entry that can be seen from this simple task:
hosts : all
tasks :
- name : add crontab test entry
cron :
name : crontab test
job : echo 'Testing!' > /var/log/crontest.log
state : present
Which then sets up a crontab entry that looks like:
#Ansible: crontab test
* * * * * echo Testing > /var/log/crontest.log
Unfortunately if you have crontab entries that have been set up outside of Ansible's cron module then you are going to have to take a less clean approach to tidying up your crontab entries.
For this we will simply have to throw away our user's crontab using crontab -r and we can invoke this via the shell with a play that looks something like following:
hosts : all
tasks :
- name : remove user's crontab
shell : crontab -r
We can then use further tasks to set the tasks that you wanted to keep or add that properly use Ansible's cron module.

If you have very complicated crontab entries so you can also delete it by shell module of ansible as shown in below example.
---
- name: Deleting contab entry
hosts: ecx
become: true
tasks:
- name: "decroning entry"
shell:
"crontab -l -u root |grep -v mybot |crontab -u root -"
register: cronout
- debug: msg="{{cronout.stdout_lines}}"
Explanation:- You have to just replace "mybot" string on line 8 with your unique identity of crontab entry. that's it. for "how to delete multiple crontab entries by ansible" you can use multiple strings in grep as shown below
"crontab -l -u root |grep -v 'strin1\|string2\|string3\|string4' |crontab -u root -"

Related

Delete/clear active cron job

How can I delete or clear all the cron job that I made previously and just run new cron job that I assigned? I'm using crontab -r but it just clear in the crontab display, but it still runs that cron job and the previous cron job that I have already deleted by using that code.
After I clear cron job using crontab -r, I run crontab -l and it shows this output.
No crontab for trygcp
Use this command,
crontab -e
to access crontab, in there you can deleted specific line or job you created.
About the output you are getting, that only means there is no crontab created under the username trygcp
What you can do is this:
crontab -u [username] -e
Where:
-u define user
-e edit user's crontab
This command will create a crontab under your username, but remember you must have root privilege for you to do this.

How to update the crontab and check if its running correctly?

I've modified the crontab with:
sudo crontab -e
and added a cron for a script to run every minute so I can test if it works:
1 * * * * /scripts/backup-script.sh > /scripts/backup-script.logs
Afterwards, I tried to restart the cron service to restart the server but the cron doesn't seem to be working, I tried to use:
crontab -l
and it appears to have the old content as if I didnt even modify it. However, going into crontab -e does show the updated content.
To make your script run every minute your cron record must be:
* * * * * /scripts/backup-script.sh > /scripts/backup-script.logs
What you enter will run every 1st minute every hour
And if you add record via crontab command you do not need to touch cron daemon
To see the cron record you add with sudo crontab -e you must check it with command sudo crontab -l. Otherwise you list cron record of different user

Active cron job

I am trying to make a cron job for the first time but i have some problems making it work.
Here is what i have done so far:
Linux commands:
crontab -e
My cronjob looks like this:
1 * * * * wget -qO /dev/null http://mySite/myController/myView
Now when i look in:
/var/spool/cron/crontabs/
I get the following output:
marc root
if i open the file root
i see my cronjob (the one above)
However it doesnt seem like it is running.
is there a way i can check if its running or make sure that it is running?
By default cron jobs do have a log file. It should be in /var/log/syslog (depends on your system). Vouch for it and you're done. Else you can simply append the output to a log file manually by
1 * * * * wget http://mySite/myController/myView >> ~/my_log_file.txt
and see what's your output. Notice I've changed removed the quiet parameter from wget command so that there is some output.

crontab being saved in tmp/ in debian

I'm trying to make a crontab with crontab -e, but it saves it in tmp/crontab.FTt6nI/crontab
the crons don't work so I guess that's the problem. But I don't understand why.
type:
crontab -l
to show list of crontab, your newly added crontab should be on the list. you could set the crontab to email the output to you by > youremail#aaa.com, in this way you can assure the cronjob is already run.
example:
* * * * * /usr/bin/php /home/username/public_html/cron.php > aaa#aaa.com
make sure the crond is running:
/etc/init.d/crond status
if it down, start it (centos/rhel):
/etc/init.d/crond start
debian/ubuntu:
/etc/init.d/cron start
hope that help.

Modify directory crontab looks in for crontab file?

I have a crontab file at /etc/crontab. I didn't place it there, it was already there, along with a lot of other files in /etc. However, when I type the command crontab -e, it creates a new file. I want it to look for crontab in /etc. This is what the command line says when I type crontab -e.
no crontab for root - using an empty one
~
~
~
~
~
"/tmp/crontab.vx7AxR" 0L, 0C
After a complete edit, this is what I end up with. Any ideas?
[root#server ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
"/tmp/crontab.uJzKS0":1: bad day-of-month
errors in crontab file, can't install.
Do you want to retry the same edit?
Contents of the crontab file:
0 * * * * /var/www/html/myfile.py
Thanks, I appreciate it. I wish there was more info out there about customizing crontab.

Resources