We help IT Professionals succeed at work.

CRON schedule from and too

richsark
richsark asked
on
436 Views
Last Modified: 2012-05-08
Hello all,

I have a cronjob from machine A running at:
59 8,12,18,23 * *  * /path/to/myscript

I need to setup another cron on machine B to run 8 min after the one from machine A

The reason behind this, is that I have database exports from machine A, then machine B will run another script to import it.

Thanks

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007

Commented:
I'm not really sure what you're asking here.

If you want to set up a cronjob on machine B, then it would look like

7 9,13,19,0 * * * /path/to/script

However, a better way would be for the script on machine A to kick off the import on machine B when it has finished.  That's assuming you have ssh access to machine B.

Author

Commented:
Hi,

If your interested on my script have a look to confirm my theory. Also you can see I clean up files older then X days so I dont risk drive-fill-up.


Server A:

#!/bin/sh

. /opt/qip/etc/qiprc
. /opt/sybase/SYBASE.sh
DATE=`/bin/date +%Y%m%d-%H:%M`

# Clean up the old database extracts first
if [ -f /home/ammuser/export/QIPexport.*.tar.gz ]
then
    mv /home/ammuser/export/QIPexport.*.tar.gz /home/ammuser/export/Archive
fi

# Dump the raw tables
#
/opt/qip/usr/bin/qip-export -d /home/ammuser/export -s QIPSYBASE -U qipadmin -Z 01ac0aaf78b520067294882e54bd7c1923ba -k

# Clean up all the audt files
rm /home/ammuser/export/*aud.qef

# tar and gzip the *.qef files
#
cd /home/ammuser/export/
/bin/tar -cvf ./QIPexport.$DATE.tar ./*.qef
/bin/gzip ./QIPexport.$DATE.tar

# scp the file to the DR-ESM box 11.34.29.40
#
/usr/bin/scp /home/ammuser/export/QIPexport.$DATE.tar.gz 11.34.29.40:/home/ammuser/exports-from-Prod

if [ $? -eq 0 ]
then
  rm /home/ammuser/export/*.qef
else
  rm /home/ammuser/export/*.qef
#  mailx -s "QIP export filed" DL-SM-N-Middleware@email.com < /export/home/qip-exports/Export-Failure.txt
#  exit 1
fi

/usr/bin/scp  /home/ammuser/Done-Flag 11.34.29.40:/home/ammuser/exports-from-Prod

# move file to trash and delete
mv /home/ammuser/export/QIPexport.$DATE.tar.gz /home/ammuser/Archive/
# Cleaning up older than 3 days old Archive files
/usr/bin/find /home/ammuser/Archive -name "*gz" -mtime +33 -exec rm {} \;

======================================================

Server B:

#!/bin/sh


. /opt/qip/etc/qiprc
. /opt/sybase/SYBASE.sh

#run as user qip or root


DATE=`/bin/date +%Y%m%d-%H:%M`
FILE=`ls -lrt /home/ammuser/exports-from-Prod/QIPexport*.tar* | tail -1 | awk '{print $9}'`
TARFILE=`echo $FILE |  cut -f1,2,3 -d"."`
BASEFILE=`echo $FILE |  cut -f1,2 -d"."`

if [ ! -f /home/ammuser/Done-Flag ]
then
   echo "The export of the DB has not completed or has failed."
#   mailx -s "QIP export filed" DL-SM-N-Middleware@email.com < /export/home/qip-imports/Import-Failure.txt
#   exit 1
else
   rm /home/ammuser/Done-Flag
fi

# Make sure QIP is not running on ESM-DR
#/bin/pkill qip

#unzip & untar the tar ball
cd /home/ammuser/exports-from-Prod
/bin/gunzip $FILE
/bin/tar -xvf $TARFILE

#import data
/opt/qip/usr/bin/qip-import -d /home/ammuser/exports-from-Prod/ -S QIPSYBASE -U qipadmin -Z 01ac0aaf78b520067294882e54bd7c1923ba -k

if [ $? -eq 0 ]
then
#   cp -r $BASEFILE
#   cp /home/ammuser/exports-from-Prod/*.qef
/bin/gzip $TARFILE
   mv *.tar.gz /home/ammuser/Archive/
fi

# Restart QIP on ESM-DR-NOT-NEEDED
#/opt/qip/etc/qip-rs-startup

# Cleaning up older than 3 days old Archive files
/usr/bin/find /home/ammuser/Archive -name "*gz" -mtime +33 -exec rm {} \;

Kerem ERSOYPresident

Commented:
Hi,

I'd suggest you add a logic like below to the beginning of your cron script in system B and run it every minute with a command like that:

* * * * * /path/to/script


Cheers,
K.


#!/bin/bash
 
NOW=$(date --date -now +%s)
TS=$(date --date "$(ls -al  /home/ammuser/Done-Flag --full-time |awk '{ print $6 " " $7}')")
FILE=$(date --date "$TS" +%s)
 
if [ $((NOW-FILE)) -ge 480 ]; then 
   .
   .
 
else
  exit 1
fi  
  

Open in new window

Kerem ERSOYPresident

Commented:
It simply checks the modification date of the file called  /home/ammuser/Done-Flag and and converts it to seconds. Then does the same for the current time. If the file is older than 8 minutes (480 seconds) then runs the content and otherwise exits.


Cheers,
K.

Author

Commented:
OK, So I need two crons on System B as well as two scripts, yours and mine.

Server B

Mine to run:

7 9,13,19,0 * * * /path/to/script

Yours:
* * * * * /path/to/script

Just so I know, your script checks the date and time before my script triggers?
CERTIFIED EXPERT
Top Expert 2007

Commented:
I think the easiest solution is to implement my 2nd suggestion.  Given that you already scp the file across, then on server A, add the following line to the end of the script

ssh 11.34.29.40 /path/to/script
Kerem ERSOYPresident

Commented:
> OK, So I need two crons on System B as well as two scripts, yours and mine.

Nope there will be only one scirpt. that would run at:

* * * * * /path/to/script

(every minute)

you will integrate your script into mine so that:


#!/bin/bash
 
NOW=$(date --date -now +%s)
TS=$(date --date "$(ls -al  /home/ammuser/Done-Flag --full-time |awk '{ print $6 " " $7}')")
FILE=$(date --date "$TS" +%s)
 
if [ $((NOW-FILE)) -ge 480 ]; then 
   . ./path/to/your/script 
else
  exit 1
fi

Open in new window

Kerem ERSOYPresident

Commented:
Opps this:

if [ $((NOW-FILE)) -ge 480 ]; then
   . ./path/to/your/script
else

should read:

if [ $((NOW-FILE)) -ge 480 ]; then
   . /path/to/your/script
else


Second dot is a typo. The first one means that the new script should be called in the same shell not initiating a child.

Cheers,
K:

Author

Commented:
Ok, this is what I gathered :) that will run on Server B

Only 1 Cron on Server B every min:

* * * * * /path/to/script


Question:
When I reference your line:

if [ $((NOW-FILE)) -ge 480 ]; then
   . /opt/ams/scripts/imports.sh

Essentially I am calling the script itself so I dont really need the whole thing. I would assume I only needed this one: See snip-it part two


#!/bin/bash
 
NOW=$(date --date -now +%s)
TS=$(date --date "$(ls -al  /home/ammuser/Done-Flag --full-time |awk '{ print $6 " " $7}')")
FILE=$(date --date "$TS" +%s)
 
if [ $((NOW-FILE)) -ge 480 ]; then
   . /opt/ams/scripts/imports.sh 
else
  exit 1
fi 
#!/bin/sh 
. /opt/qip/etc/qiprc
. /opt/sybase/SYBASE.sh 
#run as user qip or root 
 
DATE=`/bin/date +%Y%m%d-%H:%M`
FILE=`ls -lrt /home/ammuser/exports-from-Prod/QIPexport*.tar* | tail -1 | awk '{print $9}'`
TARFILE=`echo $FILE |  cut -f1,2,3 -d"."`
BASEFILE=`echo $FILE |  cut -f1,2 -d"."` 
if [ ! -f /home/ammuser/Done-Flag ]
then
   echo "The export of the DB has not completed or has failed."
#   mailx -s "QIP export filed" DL-SM-N-Middleware@email.com < /export/home/qip-imports/Import-Failure.txt
#   exit 1
else
   rm /home/ammuser/Done-Flag
fi 
# Make sure QIP is not running on ESM-DR
#/bin/pkill qip 
#unzip & untar the tar ball
cd /home/ammuser/exports-from-Prod
/bin/gunzip $FILE
/bin/tar -xvf $TARFILE 
#import data
/opt/qip/usr/bin/qip-import -d /home/ammuser/exports-from-Prod/ -S QIPSYBASE -U qipadmin -Z 01ac0aaf78b520067294882e54bd7c1923ba -k 
if [ $? -eq 0 ]
then
#   cp -r $BASEFILE
#   cp /home/ammuser/exports-from-Prod/*.qef
/bin/gzip $TARFILE
   mv *.tar.gz /home/ammuser/Archive/
fi 
# Restart QIP on ESM-DR-NOT-NEEDED
#/opt/qip/etc/qip-rs-startup 
# Cleaning up older than 3 days old Archive files
/usr/bin/find /home/ammuser/Archive -name "*gz" -mtime +33 -exec rm {} \;
 
 
 
 
 
 
======
So I think I only need the script to be like so every min in cron
======
 
 
#!/bin/bash
 
NOW=$(date --date -now +%s)
TS=$(date --date "$(ls -al  /home/ammuser/Done-Flag --full-time |awk '{ print $6 " " $7}')")
FILE=$(date --date "$TS" +%s)
 
if [ $((NOW-FILE)) -ge 480 ]; then
   . /opt/ams/scripts/imports.sh 
else
  exit 1
fi 

Open in new window

Kerem ERSOYPresident

Commented:
Yup exactly.

my script will be calling yours once the flag file is older than 480 seconds (or 8 minutes)
President
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2007

Commented:
Am I missing something obvious here?  What's the issue with my one crontab, one additional ssh line suggestion?  It's by far the easiest to implement.

Author

Commented:
You have my attention,  please explain.
CERTIFIED EXPERT
Top Expert 2007

Commented:
As previously explained, just add

ssh 11.34.29.40 /path/to/script

to the end of your script on server A to kick of the import on server B.  This way, you don't need to muck around with crontabs or any script modifications on server B, as the script will only be kicked off when the export has completed.
CERTIFIED EXPERT
Top Expert 2007

Commented:
So can someone explain what is wrong with my simple solution?

Author

Commented:
Nothing is wrong... what if the ssh fails to run or there is an issue on server b? can it run agian or better yet is there a way to insure the script runs and completes on server B

Thanks

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.