Operating System - HP-UX
1829679 Members
9024 Online
109992 Solutions
New Discussion

Re: Can cronjob script change the timing for itself to be executed?

 
Rashid Ali
Frequent Advisor

Can cronjob script change the timing for itself to be executed?

May I know whether I can reschedule the cronjob script depending on whether other process has finished? I want to get it run only after other process finished.

Thanks,

6 REPLIES 6
Patrick Wallek
Honored Contributor

Re: Can cronjob script change the timing for itself to be executed?

I don't know that you could use the cron command directly, but you could probably use the 'at' command. Do a 'man 1 at' for more information.
Jim Turner
HPE Pro

Re: Can cronjob script change the timing for itself to be executed?

Hi,

The mechanism I've used for this in the past is a file semaphore. Have your initial job touch a file like /tmp/notyet. Have your cron job test for the existence of /tmp/notyet. If /tmp/notyet exists, sleep for a while then check again. Once the file /tmp/notyet no longer exists, your script can proceed.

All the best,
Jim
Sridhar Bhaskarla
Honored Contributor

Re: Can cronjob script change the timing for itself to be executed?

Zhaung,

With a little bit of complication I think you can do it.

There should be two files

1) one cron wrapper - cron_wrapper
2) Your original script - your_script

Now your cron_wrapper will contain the following. This is only a rough draft. The original script will be more complicated as you need to take care of Hours, minutes etc.,

ps -ef |grep -v grep |grep your process

if [ $? = 0 ] #the Process is running
then
HOUR=`date +%H`
MYHOUR=`echo "$HOUR + 1 " |bc` #this will schedule the job one hour later.
echo "* $MYHOUR * * * your_path/cron_wrapper" >> /tmp/mycron$$
crontab /tmp/mycron$$ > /dev/null 2>&1
rm /tmp/mycron$$
echo "Rescheduled to $MYHOUR" >> $your_log
exit
else
your_patch/your_script >> $your_log 2>&1
crontab -r
fi

Initially you need to put
"* $MYHOUR * * * your_path/cron_wrapper"
in your crontab. Depending on whether the process is running or not, your cron_wrapper will take care of it.

It will be much more complicated if you want to introduce minutes also. Because you need to check to see if your MY_HOUR or MY_MIN will become greater than 60.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Praveen Bezawada
Respected Contributor

Re: Can cronjob script change the timing for itself to be executed?

Hi
The method we use to prevent multiple instances of a script from running when scheduled through cron, is to configure cron to run only once and at the end of the script schedule the cron again to run only once. In this way we can be sure that there will be no two instances of the script running simultaneously.
This has worked for us...

...BPK...
Brian Pyle
Frequent Advisor

Re: Can cronjob script change the timing for itself to be executed?

Here's an example of testing for a running process, and not starting again if it's already running. This is just a piece of a bigger pie, but I think you can get the jist from this :) Brian

#!/usr/bin/sh
###############################################################################################################
## Start Arc License Mangager
###############################################################################################################
echo ""
echo ""
echo "#########################################################################"
echo " Start Arc License Manager"
echo "#########################################################################"

check_arc_license=`check_arc_license`
if [ $? -gt 0 ]
then
arc_license_started=.false.
count1=1
while [ "X$arc_license_started" = "X.false." ]
do
echo "Trying to start the Arc/Info License Manager $count1 !" | tee -a $logfile
start_arc_license=`su arcadmin -c start_arc_license >/dev/null 2>&1 &`
echo "Waiting 5 Seconds for the Arc/Info License Manager to Start!" | tee -a $logfile
dotcount=1
while [ $dotcount -le 5 ]
do
print -n .
sleep 1
((dotcount=$dotcount+1))
done
print -n " --->"
check_arc_license=`check_arc_license`
if [ $? = 0 ]
then
arc_license_started=.true.
echo "Arc License Manager STARTED ! :)" | tee -a $logfile
echo ""
echo ""
else
if [ $count1 -ge 5 ]
then
echo "hmm.... :( -- Could not start Arc License Manager after $count1 Tries !" | tee -a $logfile
echo $start_arc_license | tee -a $logfile
echo "I Quit :( -- Aborting: `basename $0`: `date`" | tee -a $logfile
exit 1
fi
sleep 5
((count1=$count1+1))
fi
done
else
echo "hmm.. :) Arc License Manager already running on node: `hostname`" | tee -a $logfile
fi
echo " " >> $logfile


Here's the check_arc_license script. It doesn't have to be seperate, but I did it this way so I could use it over :)

#!/usr/bin/sh
host=`hostname`
check_node=`lmstatus | grep "$host: license server UP"`
error_trap=$?
if [ $? = 0 ] && [ $error_trap = 0 ]
then
exit 0
else
exit 1
fi
Follow The Path With Heart
Ralf Hildebrandt
Valued Contributor

Re: Can cronjob script change the timing for itself to be executed?

You can have the script reschedule itself by using "at -f script now + 5 minutes" as last line in the script. No need for lockfiles...
Postfix/BIND/Security/IDS/Scanner, you name it...