The UNIX and Linux Forums  


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
default cron shell dummy_9746 Shell Programming and Scripting 2 10-13-2008 03:25 PM
Cron execution of shell script ashish.sharma Shell Programming and Scripting 3 09-08-2008 03:57 AM
Is it possible to have more than one Cron in shell script? Yamini Thoppen AIX 4 12-17-2007 03:23 AM
Shell script & cron deppy82 Shell Programming and Scripting 3 08-02-2007 02:00 PM
Shell + Oracle + Cron job pathanjalireddy Shell Programming and Scripting 1 04-07-2005 03:07 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-03-2009
thepurple thepurple is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 134
which one is better Run by Cron or Shell

Dear Experts,

I have a script defined in Cron which runs every 1 minute.


Code:
* * * * * /export/home/myscript.sh >/dev/null 2>&1

Now the issue is executing that myscript.sh sometime will take 2/3 or more minutes to finish execution.

But after 1 minute cron will invoke another instance(process) execute the myscript. In that case do you think there is a probability to overlapping the instance and may create problem.


If there is a chance for overlapping may be the below shellscript(run it in background) is okay which will run every 1 minute. Script will invoke different instant after every minute.


Code:
#!/usr/bin/bash
while :
do
    sleep 60 & pid=$!
    /export/home/myscript.sh
    wait $pid
done

Could you please make your valuable suggestion?

//purple
  #2 (permalink)  
Old 01-03-2009
Panos1962 Panos1962 is offline
Registered User
  
 

Join Date: Dec 2008
Location: Thessaloniki, GREECE
Posts: 29
There is another solution to this problem, but you have to add some code to your `myscript.sh'. Put some kind of lock while your program is runing, e.g. if your program is a shell script, you can add the following lines:


Code:
...
lock=/export/home/.myscript.lock
trap "rmdir ${lock}; exit 2" 1 2 3 15
# Now lock. If lock fails, then propably the program is runing.
mkdir ${lock} || exit 0
...
Here comes the script code
...
# Now unlock, so another instance of this program can run in the future.
rmdir ${lock}

I prefer this kind of coding in such cases, because it's better to protect this kind of programs to run overlaping processes. So, either you prefer cron, or sleeping nohup shell scripts, the process is vulnerable to run in simultaneus instances; if you use some kind of locking this is impossible.
Bye...
  #3 (permalink)  
Old 01-03-2009
2k7.vipin 2k7.vipin is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 5
Can you try this way

#!/usr/bin/bash
while :
do
/export/home/myscript.sh
pid=$!
wait $pid
done
  #4 (permalink)  
Old 01-03-2009
thepurple thepurple is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 134
Quote:
Originally Posted by Panos1962 View Post
Code:
...
lock=/export/home/.myscript.lock
trap "rmdir ${lock}; exit 2" 1 2 3 15
# Now lock. If lock fails, then propably the program is runing.
mkdir ${lock} || exit 0
...
Here comes the script code
...
# Now unlock, so another instance of this program can run in the future.
rmdir ${lock}
is it just like..


Code:
#!/usr/bin/bash
lock=/export/home/.myscript.sh.lock
trap "rmdir ${lock}; exit 2" 1 2 3 15

mkdir ${lock} || exit 0
...
Here comes my script code
...
rmdir ${lock}


After that i can put the script in Crontab.

In that case every 1 minute cron will invoke process and child process. But meanwhile, the program (myscript) can be running with different process and child process id.

right?

//purple
  #5 (permalink)  
Old 01-03-2009
thepurple thepurple is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 134
moreover,

let say myscript.sh is running and having the below process id-

solaris:/home/user1> ps -ef|grep myscript.sh
solaris 25907 10427 0 14:36:50 ? 0:00 /export/home/myscript.sh

i put the myscript.sh in cron as 1 minute interval. So, every 1 minute interval solaris again run the myscript.sh.

DO you think solaris May try to assing the next myscript.sh with procss id 25907( which indicating overlapping)?
procss id 25907 still running.....
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 04:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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