Solved

cron_job

Posted on 2008-10-14
10
496 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

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!

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

623 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