Visit Our UNIX and Linux User Community


temporarily suspend crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting temporarily suspend crontab
# 1  
Old 08-17-2009
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
# 2  
Old 08-17-2009
You could...

Code:
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.
# 3  
Old 08-17-2009
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.
# 4  
Old 08-17-2009
Quote:
Originally Posted by avronius
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 ----------

Quote:
Originally Posted by peterro
You could...

Code:
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.
Thank you Peterro, Can you suggest any sample script I can look into? Regards
# 5  
Old 08-17-2009
I'd probably do something like avronius mentioned. Pseudo code for disable:

Code:
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.
# 6  
Old 08-17-2009
Well, here's a quick and dirty "enable/disable" script
Code:
#!/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:
Code:
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:
Code:
/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:
Code:
/usr/local/bin/manageCron -d backup

Code:
/usr/local/bin/manageCron -disable backup

Code:
/usr/local/bin/manageCron --d backup

Code:
/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:
Code:
#!/bin/sh

usage()


Last edited by avronius; 08-17-2009 at 06:16 PM.. Reason: Adding comments
# 7  
Old 08-18-2009
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.

Code:
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

Code:
$ ./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.

Last edited by Paul.S; 08-19-2009 at 02:38 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #759
Difficulty: Medium
Commodore filed for bankruptcy in 1994 and its assets were purchased by a German PC manufacturer who created the subsidiary company Amiga Technologies.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Hibernate and Suspend

I have a C++ program which ends up getting run on every conceivable distro. What I can't do in C++, I can do by shelling out to the O/S. I am trying to find a portable way to send the system into hibernate and suspend modes. For users who have pm-utils, of course, I can use that, but I am trying... (4 Replies)
Discussion started by: BrandonShw
4 Replies

2. UNIX for Dummies Questions & Answers

suspend a *background* running job

Is there a way to suspend (TSTP?) a job that is running in the background, _without_ first bringing it to the foreground and inputting Ctrl-Z from the keyboard? IOW, something similar to issuing the shell's bg builtin command on a job ID to resume a job that is suspended in the background,... (2 Replies)
Discussion started by: uiop44
2 Replies

3. Solaris

Solaris 10 Suspend System

One of our Solaris 10 Guru's created a Solaris 10 image and he disabled the Suspend System option. How do I enable it again? example right click on the desktop the go to the bottom Suspend System in grayed out. Any help would be great. ---------- Post updated at 09:02 PM ----------... (1 Reply)
Discussion started by: deaconf19
1 Replies

4. Shell Programming and Scripting

Suspend of crontab temprorarily

Hello Friends, I would like to find out if there is a way to suspend crontab script? I need to suspend the scheduling of crontab scripts in case of an alarm and when alarm ends need to start them again automaticaly, could you suggest me a method? one of participiant of the forum with name... (6 Replies)
Discussion started by: EAGL€
6 Replies

5. Shell Programming and Scripting

How to suspend a user account?

Hi, guys. I have two questions: I need to write a script, which can show all the non-suspended users on system, and suspend the selected user account. There are two things I am not sure: 1. How can I suspend user's account? What I think is: add a string to the encrypted password in shadow... (2 Replies)
Discussion started by: daikeyang
2 Replies

6. Solaris

Suspend in opensolaris

How does one enable the suspend to hard drive or ram and sleep features on a desktiop running Open Solaris? (2 Replies)
Discussion started by: FloridaBSD
2 Replies

7. UNIX for Advanced & Expert Users

Suspend to write file

We have a common directory , everyone can put file to it , but I found that there is a problem if the user continouosly write files to this directory while the another application is running at the same time , so I want temporaily "protect" the directory , no files can be write to it at a specific... (3 Replies)
Discussion started by: ust
3 Replies

8. Shell Programming and Scripting

suspend a process

Hello, Two child processes work at the same time because they communicate one another. In KSH, does it exist a good way to suspend a parent process until one of the two child processes stops. It seems that the command 'wait' works well for one process but for two processes, it suspends the... (7 Replies)
Discussion started by: piou78
7 Replies

9. UNIX for Dummies Questions & Answers

UNIX Process Suspend

Hi, I have this doubt.... When some program is running and if we press CTRL+Z...it is suspended... what should we do to continue its execution I know that KILL can be used to completely terminate the process....but is there any way to continue... Thanks (3 Replies)
Discussion started by: proton
3 Replies

Featured Tech Videos