Solved

cron_job

Posted on 2008-10-14
10
479 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now