WelcomeUser Guide
ToSPrivacyCanary
DonateBugsLicense

©2024 Poal.co

380

I had a problem at work with trying to remove cron jobs using Ansible. The way Ansible manages cron jobs is it sticks a particular comment above the job when it is added. Removal is easy then, just get rid of the job under the comment.

What if the job wasn't added by Ansible with that comment above? Well, we have the shell. Behold it in all its glory:

crontab -l | grep -v job_to_remove.sh | crontab -

We list the crontab, pipe it to grep to remove the job from the output, then we pipe that right back into crontab. How slick is that?

I had a problem at work with trying to remove cron jobs using Ansible. The way Ansible manages cron jobs is it sticks a particular comment above the job when it is added. Removal is easy then, just get rid of the job under the comment. What if the job wasn't added by Ansible with that comment above? Well, we have the shell. Behold it in all its glory: `crontab -l | grep -v job_to_remove.sh | crontab -` We list the crontab, pipe it to grep to remove the job from the output, then we pipe that right back into crontab. How slick is that?

(post is archived)

[–] 1 pt

I was going to say put your cron jobs in separate files in /etc/cron.d/, but it sounds like you’re dealing with random jobs added by who knows what.

I’ve never seen crontab - before but it’s in the crontab manpage. The Debian manpage even mentions using sed with crontab -l | crontab -.

[–] 0 pt

Yeah, it's like that. I'm removing them because we're going to Ansible scheduled jobs, so the Ansible execution nodes will be running the formerly cron jobs against all of the servers.