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
script to monitor process running on server and posting a mail if any process is dead pradeepmacha Shell Programming and Scripting 13 03-06-2009 07:33 AM
Can a child process return a specific value to a parent process ? Ametis1970 High Level Programming 8 04-09-2008 11:22 PM
Killing of a process and send a mail if the process doesnot come up within 2 minutes Prince89 Shell Programming and Scripting 1 02-15-2008 07:10 PM
how to start a process and make it sleep for 5 mins and then kill that process shrao Shell Programming and Scripting 6 03-27-2007 12:54 PM
my process is going to sleep mode after 12 hours but i need my process in in firsy pr mukesh_rakesh1 UNIX for Advanced & Expert Users 0 09-05-2006 02:43 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 02-07-2007
mpang_ mpang_ is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 89
process

Hi, I have a job which I need to run every 15 minutes, however, I need to ensure that previous job (15 minutes) is done before I can start a new one, how can I do that? THANK YOU SO MUCH!
  #2 (permalink)  
Old 02-07-2007
BOFH BOFH is offline Forum Advisor  
Registered User
  
 

Join Date: Feb 2005
Location: Broomfield, CO
Posts: 406
Code:
#!/bin/ksh

PID=$$

if [[ -f /var/run/script.pid ]]
then
  OLDPID=`cat /var/run/script.pid`
  RUNNING=`ps -e | grep $OLDPID`
  if [[ ! -z "$RUNNING" ]]
  then
    exit 1
  fi
fi

echo $PID > /var/run/script.pid

# your code here

if [[ -f /var/run/script.pid ]]
then
  rm /var/run/script.pid
fi
Carl
  #3 (permalink)  
Old 02-07-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Use a temporary lockfile containing the process id, e.g...
Code:
#!/usr/bin/ksh

LOCKFILE=/tmp/lockfile

#---At the start of your script check to see that a lockfile exists
if [[ -f $LOCKFILE ]]
then
    #----If the lockfile does exist then check that the process is still running
    #     since it may have aborted and left the lockfile behind
    if ps -p $(<$LOCKFILE) >/dev/null
    then
        echo job is still running
        exit
    fi
fi

#---Must be okay to run, so create the lockfile containing the process id
echo $$ > $LOCKFILE

#----Rest of script goes here
:

#----end
rm $LOCKFILE
Not tested.
  #4 (permalink)  
Old 02-08-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,953
Just thought of including this is as a note.


When creating a lockfiles to control process from spawning when the previous instance are still running.

It is better to avoid common names to the locking files and redirecting just the process id of the process to the control file.

First if common names are use, there is high probability that a same kind of naming convention (same name to the locking file ) be used by other scripts as well so that it would also use the same filename for its own purpose.

If process id is used as a value in the locking file, in a busy system there is a high possibility that a process 'A' running with pid -> pid1 is done with its work and again system can grant a new process 'B' the same pid -> pid1 and we end up controlling a process that shouldn’t be actually.

Hence its better to add some more information like parent process id or timestamp something like that to guarantee the uniqueness.

And the final thing could be to lock the file with perm bits once it is written, so those process which tend to overwrite them will receive an error. Though this is not so secured this way is a bit ahead.
  #5 (permalink)  
Old 02-08-2007
mpang_ mpang_ is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 89
thank you so much for all your excellent advices! However, I would like to add one more detail, I actually have 2 jobs (using the same script with different parameters) starting at the same time, using the provided method would be work, is there any work around?
  #6 (permalink)  
Old 02-08-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
Change the name of the lockfile depending on the parameters, e.g...
Code:
LOCKFILE=/tmp/$(basename $0).$(echo $*|tr ' ' '.')
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 08:39 PM.


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