The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Please send activation mail.. bvijaycom Forum Support Area for Unregistered Users & Account Problems 1 05-19-2008 04:18 AM
Activation jamalwil7 Forum Support Area for Unregistered Users & Account Problems 0 05-15-2008 03:20 PM
Account Activation Problem MIKU Forum Support Area for Unregistered Users & Account Problems 1 08-09-2007 05:17 AM
Did not receive activation mail pl_cyber Forum Support Area for Unregistered Users & Account Problems 0 05-29-2007 11:09 PM
Wireless ipw2200 activation XinU* UNIX for Advanced & Expert Users 4 01-21-2007 01:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-03-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Cronjob Activation

Hi All,

I have set a cronjob to run a script at a frequency of 10 mins.
However, if the script takes more than 10 mins to complete, how will the cronjob behaves?
1) Does it ignore the current process and restart the whole process again?
2) Or does it stop to trigger the script again unless previous process has been completed?
  #2 (permalink)  
Old 02-03-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 921
Post

Quote:
Originally Posted by Raynon View Post
Hi All,

I have set a cronjob to run a script at a frequency of 10 mins.
However, if the script takes more than 10 mins to complete, how will the cronjob behaves?
1) Does it ignore the current process and restart the whole process again?
2) Or does it stop to trigger the script again unless previous process has been completed?
Depends on your cron implementation but generally it will run another copy of the script 10 mins after the first one is started - regardless of how long the first run takes.

Solutions:
  • Make your script reentrant - ie write it such that it running multiple times at once isn't a problem.
  • Make your script aware of how many times it's currently running - ie create a lock file when it starts up and check for this every time you start.
  • Don't use cron, have the script run continuously and manage it's own scheduling - ie sleep 600
  • Don't use cron, get a better scheduler - depending on your enviornment, this could be something like anacron or control-m.
  #3 (permalink)  
Old 02-04-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Quote:
Originally Posted by Smiling Dragon View Post
Depends on your cron implementation but generally it will run another copy of the script 10 mins after the first one is started - regardless of how long the first run takes.

Solutions:
  • Make your script reentrant - ie write it such that it running multiple times at once isn't a problem.
  • Make your script aware of how many times it's currently running - ie create a lock file when it starts up and check for this every time you start.
  • Don't use cron, have the script run continuously and manage it's own scheduling - ie sleep 600
  • Don't use cron, get a better scheduler - depending on your enviornment, this could be something like anacron or control-m.

Hi,

Running the script continously means to say that i have to leave a command prompt window always open. Once closed, the process will be ended, and also another disadvantage is that a infinite looping script will eat up alot of resources, wouldn;t it ?
So i still think cron is the more practical one. But i have no idea about anacron or control-m. Can you enlightened me on that ?

Can you give an example of script being re-entrant ?
I was thinking if i could make the script check for the process to see if the previous process have been completed, can any experts give me some examples of this ? I am using csh by the way.
  #4 (permalink)  
Old 02-06-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 921
Quote:
Originally Posted by Raynon View Post
Running the script continously means to say that i have to leave a command prompt window always open. Once closed, the process will be ended
Not at all, just move it to the background, no different to all the other daemonised stuff you are running on your system. If it likes to write to STDOUT, redirect this to a log file (or /dev/null if you prefer).
Quote:
Originally Posted by Raynon View Post
and also another disadvantage is that a infinite looping script will eat up alot of resources, wouldn;t it ?
Much as before, no problems here either. Just use sleep at the bottom of the loop to stop it thrashing.
Quote:
Originally Posted by Raynon View Post
So i still think cron is the more practical one.
Your call of course.
Quote:
Originally Posted by Raynon View Post
But i have no idea about anacron or control-m. Can you enlightened me on that ?
Anacron is a slighty more advanced version of cron, may or may not do what you want. Free (I think)
Control-M is a BMC product that manages scheduling at an enterprise level, it supports job dependancies, understands limited resources and works across multiple platforms. Costs a bit.

Google for more info on these
Quote:
Originally Posted by Raynon View Post
Can you give an example of script being re-entrant ?
Reentrant just means that it can be run multiple times at once without going wonky. One example:
Not rentrant:
Code:
#!/bin/sh
/usr/bin/do_some_stuff > /var/log/did_some_stuff.log
do some other things
if grep 'it worked' /var/log/did_some_stuff.log
then
  /usr/sbin/assume_we_are_good_to_go
else
  echo "Argh"
fi
Reentrant:
Code:
#!/bin/sh
/usr/bin/do_some_stuff > /var/log/did_some_stuff.$$.log
do some other things
if grep 'it worked' /var/log/did_some_stuff.$$.log
then
  /usr/sbin/assume_we_are_good_to_go
else
  echo "Argh"
fi
rm /var/log/did_some_stuff.$$.log
The first example could end up reading back the log of a different instance of itself. The second version includes the current process ID in the log file which prevents this happening.

Another, simpler, way is to just look in the process table for another copy of the script, if found exit immediatly.

Quote:
Originally Posted by Raynon View Post
I was thinking if i could make the script check for the process to see if the previous process have been completed, can any experts give me some examples of this ? I am using csh by the way.
(Assuming system V as opposed to bsd)
Code:
#!/bin/csh
set numprocs=`ps -ef | grep -v grep | grep $scriptname | wc -l | awk '{ print $1 }'`
if ($numprocs == 1) then
    # carry on
else
   echo "Another instance is already running, exiting..."
   exit 0
endif
  #5 (permalink)  
Old 02-13-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi ,

I am refering to this particular exmaple that you have provided me.

Code:
#!/bin/csh
set numprocs=`ps -ef | grep -v grep | grep $scriptname | wc -l | awk '{ print $1 }'`
if ($numprocs == 1) then
    # carry on
else
   echo "Another instance is already running, exiting..."
   exit 0
endif
What is the significance of the numprocs?
If it equals to 1 , why does it show that the script is not running anymore?

I tried a simple csh script below and it doesn;t really work.
When it is sleeping, the name of the script which is "myscriptname" is not reflected during " ps -ef ", it only shows " usr 28951 28941 0 15:00:39 pts/7 0:00 sleep 100 ".

Can you help ?

Code:
#!/bin/csh

echo xxx
echo ppp 
echo zzz

sleep 100
  #6 (permalink)  
Old 02-13-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 921
Quote:
Originally Posted by Raynon View Post
I am refering to this particular exmaple that you have provided me.
Code:
#!/bin/csh
set numprocs=`ps -ef | grep -v grep | grep $scriptname | wc -l | awk '{ print $1 }'`
if ($numprocs == 1) then
    # carry on
else
   echo "Another instance is already running, exiting..."
   exit 0
endif
What is the significance of the numprocs?
If it equals to 1 , why does it show that the script is not running anymore?
Because one copy is running, the one that's currently executing. It's looking for another version of itself (ie 2 or more) in case there are other copies still running.
Quote:
Originally Posted by Raynon View Post
I tried a simple csh script below and it doesn;t really work.
When it is sleeping, the name of the script which is "myscriptname" is not reflected during " ps -ef ", it only shows " usr 28951 28941 0 15:00:39 pts/7 0:00 sleep 100 ".
Works for me, when I run a csh script with a sleep in it, I see the sleep, _and_ the main script running.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0