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
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.
* * * * * 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.
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
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