temporarily suspend crontab

Issues:
Cron jobs are running everyday at 8PM to hot backup database, archivelog, and some other files.

Sometimes crontab scheduled backups need to be suspended for some other tasks randomly but doesn't happen all the time.

Objective:
I need to create simple script or command ( example : stop_cron, start_cron) that will suspend cronjob for that particular day.

Purpose is that we have "Operator" ( no root privilege, and no dba / unix background ) that need to run simple command to stop or start certain cront jobs out of 20+ job scheduled in crontab.

I am wondering is there way to merge sh script into backup script so when "stop_cron" command is executed then only that particular crontab will be skipped even though schedule fired off the job from crontab.

thank you

You could...

0 3 * * * [ ! -f /some/lock_file ] && /path/to/backup/script

The operators would then have a script that would create/remove the lock file (where they have permissions to write). You could also implement that lock file logic into the backup script.

The danger in all of this is that if someone forgets to re-enable the backup script, backups won't be performed. I suppose you could also implement another job that would remove any lock file older than say 3 days.

One way is to use a control file for any "state" oriented scripts.

Create a directory in /var/tmp called "croncontrol"
Create a script called "disable" that accepts variables depending on the cronjob name (backup, reportmailer, etc).
This script will touch /var/tmp/croncontrol/backup or /var/tmp/reportmailer, etc.

Create a wrapper for your cron script (or add a line to the existing cron script) that checks for the file /var/tmp/croncontrol/backup (or mailer or whaterver). If the file exists, exit and do not run (although I'd recommend that it mail out that the script is not being run). If the file does not exist, proceed with the normal script function.

To enable the cron scripts to run again, write a simple script that accepts the same variables to rm the 'touched' file in /var/tmp/croncontrol.

Simple - but not terribly robust.

Thank you for suggestion. Will it be possible to guide sample scripts? Not much background on shell script writing. Regards

---------- Post updated at 10:19 AM ---------- Previous update was at 10:18 AM ----------

Thank you Peterro, Can you suggest any sample script I can look into? Regards

I'd probably do something like avronius mentioned. Pseudo code for disable:

Check $0 for name of script to be enable or disable
Check $1 for the backup to disable/enable
Case the backup in
  full - if script name is enable, remove full file
          if script name is disable, touch full file
  home_dirs - if script name is enable, remove home_dirs file
               if script name is disable, touch home_dirs file
 ...etc, etc...

Once disable script is written, create an enable soft link that points to the disable script so you can use the same script for both purposes.

Well, here's a quick and dirty "enable/disable" script

#!/usr/bin/bash

function usage ()
{
   echo "Usage:"
   echo "      $0 [-enable|-disable|-help] [<process>]"
}

if [ $# -lt 2 ] ; then
   echo "process must be specified"
   usage
   exit 1
fi

case $1 in
   -enable|--enable|-e|--e)
      rm "/var/tmp/croncontrol/$2"
      exit 0
   ;;
   -disable|--disable|-d|--d)
      touch "/var/tmp/croncontrol/$2"
      exit 0
   ;;
   -help|--help|-h|--h)
      usage
      exit 0
   ;;
   -*|--*)
      echo "Error: no such option $1"
      usage
      exit 1
   ;;
   *)
      usage
      exit 1
   ;;
esac
exit 0

In this case, you'd use peterro's cron commands with the /var/tmp/crontab patch:

0 3 * * * [ ! -f /var/tmp/croncontrol/backup ] && /path/to/backup/script

If you call your script "manageCron" and store it in /usr/local/bin, you would run the command like this to disable the backup scripts:

/usr/local/bin/manageCron -d backup

---------- Post updated at 03:15 PM ---------- Previous update was at 03:13 PM ----------

I've added the full name as well as the --option format - choose whichever works best for you in your environment.

All of the following will produce the same result:

/usr/local/bin/manageCron -d backup
/usr/local/bin/manageCron -disable backup
/usr/local/bin/manageCron --d backup
/usr/local/bin/manageCron --disable backup

---------- Post updated at 03:26 PM ---------- Previous update was at 03:15 PM ----------

If you prefer to be posix compliant (most of us want this!) make the following change to the first 3 lines:

#!/bin/sh

usage()

Thank you very much avronius!

Your script worked like a charm.

I just followed your procedure and created crontab and was able to enable and disabled the cron schedule.

00 11 * * 3 /usr/local/bin/weekselector odd && /usr/local/bin/archTEST/test/even.sh > /usr/local/bin/archbakTST/test/odd_wed.log 2 >&1
00 11 * * 4 /usr/local/bin/weekselector even && /usr/local/bin/archTEST/test/even.sh > /usr/local/bin/archbakTST/test/even_thr.log 2 >&1
#
# This Cron job will be disabled with command
#
20 10 * * * [ ! -f /var/tmp/croncontrol/backup ] && /usr/local/bin/archTEST/test/even.sh > /usr/local/bin/archbakTST/test/100_with_switch.
log 2 >&1

and used your on and off switch command

$ ./manageCron -e backup  << enable
$ ./manageCron -d backup  << disable

and test output log was created whenever cron was enabled.

My last question will be is there any particular significant to parameter backup ?

I created /var/tmp/croncontrol but "on and off" command really doesn't do anything with backup parameter.

thank you.

Sorry for not responding to this sooner...

Sorry for the confusion, but this is not how the manageCron script works.

The command takes two arguments:
The first argument is the enable/disable flag.
You can use -d or --d or -disable or --disable to create a file.
You can use -e or --e or -enable or --enable to delete a file.

The second argument is the name of the file that you want created - this is the name of the file that cron will look for before starting the process that you are managing.

$ ./manageCron -e filename
$ ./manageCron -d filename

The process is simply the name of the file that you will be "touching" and then checking for in the crontab before running the script:

[ ! -f /var/tmp/croncontrol/filename ]

I hope that this helps - I'll send a message to you as well to ensure that you get this information.

Cheers,

  • Avron