Solved

cron_job

Posted on 2008-10-14
10
488 Views
Last Modified: 2013-12-27
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
Comment
Question by:sam15
  • 5
  • 4
10 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 22713407
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
 

Author Comment

by:sam15
ID: 22713651
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 22713832
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
Technology Partners: 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!

 

Author Comment

by:sam15
ID: 22714104
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 22714154
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
 

Author Comment

by:sam15
ID: 22714697
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
 
LVL 40

Expert Comment

by:omarfarid
ID: 22714767
you may run the perl batch and the end of the job to check and delete the file if the database is up
0
 

Author Comment

by:sam15
ID: 22715050
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
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 22715165
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
 
LVL 39

Expert Comment

by:Adam314
ID: 22715194
If when the database goes down, the process ends, you could use ps to see if the program is running.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSH commands for Nas4free 21 492
Convert grep lines to perl 6 75
Linux Scripting 3 162
How to retrieve remote exit code when using rsh from Linux to Windows ? 13 56
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question