Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

cron_job

is there a way to make conditional cron job.

I have a job now that is schedule to run every 5 minutes. It simply rns a batch program in perl.
What i want is to stop the cron when the database has a downtime record scheduled.

If system is scheduled to go down on weekend then I want cron to stop during that time. I am thinking that unix now does not see the database records and it should nto matter. the only way is to re-schedule the cron using the batch prgoram which read the db data and then decides when to cron.

is this correct and how you get around this.
0
sam15
Asked:
sam15
  • 5
  • 4
1 Solution
 
omarfaridCommented:
You can do simple thing in your cronjob script:

if [ -f /path/to/dontrun ]
then
      exit
fi
rest of your script here

here if you don't want crontab job to run just

touch /path/to/dontrun (this will create an empty file)

once finished and you want crontab to run on the next schedule, then

rm /path/to/dontrun

0
 
sam15Author Commented:
i am not sure i understand the logic you wrote. can you explain how that works.

would the cron job still runs every 5 minutes? how would it know about the downtime defined in DB?
0
 
omarfaridCommented:
My logic is simple:

- when you want to maintain your database and bring it down, create the file /path/to/dontrun (either by hand or by script that is bringing the database down)
- the crontab job schedule to run every 5 min will remain the same, but the job script should have the added logic I gave at the script beginning. So, when the script run every 5 min and find the file it will exit otherwise run.
- when the database maintenance is over, remove the file /path/to/dontrun, either manually or by script.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sam15Author Commented:
Oh i see, but there is some manual work involved here.

The downtime is defined in the DB table on a different server.  this requires somehow the DB to create/ftp th file from server A to server B where the cron job runs and then delete it somehow.
it is not easy to do.
0
 
omarfaridCommented:
Yes but if you want the crontab to know the down time from a database then you need the script to connect to the database and query the timing - which requires the the database to be up and a database client to connect to the remote database. If you use oracle database then you may use tnsping which comes with oracle client to test database being up or down
0
 
sam15Author Commented:
well the perl batch program is doing this: query the DB usnig http call and get a record for scheduled downtime maybe i can get perl to create that text file you mention for the cron so when cron runs it will not do anything. not sure how the file will be deleted when downtime expires. you think this is better solution.
0
 
omarfaridCommented:
you may run the perl batch and the end of the job to check and delete the file if the database is up
0
 
sam15Author Commented:
would this work

the text file that cron checks will have a date/time range. when cron runs it checks that date/time and compares it against systime and decide whether to call the batch or not.

if the file is there and date/time is not within range then it will call the batch. If the file does not exist (we delete it manually) then also it calls the batch program.
0
 
omarfaridCommented:
Time range comparison is difficult

On the other hand you may put the time at which the database will come up, e.g.

200810142350 which is 2008 oct 14 11:50 PM

I choose this format to guarantee that it is always going higher.

Then your script can get system date and time by

dt=`date +%Y%m%d%H%M`

then compare the stored value in the file and $dt

dt=`date +%Y%m%d%H%M`
if [ -f /path/to/dontrun ]
then
      edt=`cat /path/to/dontrun`
      if [ $dt -lt $edt ]
      then
            exit
      fi
fi

(put your code here)
0
 
Adam314Commented:
If when the database goes down, the process ends, you could use ps to see if the program is running.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now