Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to temporarily turn off certain unix cronjobs using a script

Tags:

unix

I have few cronjobs running everyday Mon-friday.At times during holidays, i turn them off manually so that it doesnt run that day and turn back on the next day.

Is there a way to automate this using a script

like image 856
Unixnewbie11 Avatar asked Oct 14 '10 16:10

Unixnewbie11


People also ask

How do I temporarily disable cron job?

You can stop a single cron job by removing its line from the crontab file. To do that, run the crontab -e command and then delete the line for the specific task. Alternatively, you can stop the cron job by commenting it out in the crontab file.

What is the use of * * * * * In cron?

* * * * * is a cron schedule expression wildcard, meaning your cron job should run every minute of every hour of every day of every month, each day of the week.


1 Answers

Ignacio was suggesting something like this in your crontab:

 31 1 * * *    [ -f /var/run/cron-holiday ] || /usr/local/bin/whatever-command

then at the start of a holiday weekend, as root:

 # touch /var/run/cron-holiday

and on Monday:

 # rm /var/run/cron-holiday

This is nice and simple but does have the drawback that if you forget to delete the file, your cronscripts will never run again, which could be bad.

An alternative is to have a file listing holiday dates and do something like this:

31 1 * * *   grep -q `date -I` /etc/cron-holidays || whatever-command-here

where the /etc/cron-holidays file contains lines like

2011-04-01
2011-12-25

etc

like image 63
poolie Avatar answered Nov 03 '22 02:11

poolie