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
How to Lock In HP-UX naren_chella HP-UX 7 07-07-2006 09:49 AM
Lock File canman UNIX for Dummies Questions & Answers 4 05-06-2005 06:28 AM
host lock up alex_slb UNIX for Dummies Questions & Answers 1 06-24-2002 09:23 PM
how to lock keyboard without using lock command dianayun UNIX for Dummies Questions & Answers 7 06-21-2002 07:05 PM
lock file! sskb UNIX for Dummies Questions & Answers 4 10-24-2001 06:10 PM

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-18-2008
strunz strunz is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 3
Lock for this script

Hi,

My requirement is to service a process and below is the script which i wrote for that and works fine, I have kept it in a crontab and running this everyminute, how do I lock this if its already running and i dont want to open if its running and not completed yet.

The crontab need to run for the next minute after it completes running the older one.

#!/bin/ksh

while [ true ]; do

i=$(ps -ef | grep PROCESS_NAME |grep -v grep |wc -l)

if [ "$i" -lt 2 ]

then

echo "`date '+%Y-%m-%d-%H:%M:%S'` WARNING !!! PROCESS IS DOWN"

else

echo "`date '+%Y-%m-%d-%H:%M:%S'` PROCESS IS UP"

fi

done

Help me to keep a lock for this, I am using AIX 5.3
  #2 (permalink)  
Old 02-18-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Three options:
Don't run this from crontab but instead use a while loop with a sleep 60 at the end and background it. No chance of it doubleing up on itself then.

Or

Have your script look for 'itself' in the process table before doing anything, if it's finds itself there already, exit. You've already written the code to do this in the script you provided above, just have to duplicate it and change PROCESS_NAME to your script name.

Or

Start the script with this:
Code:
if [ -f /tmp/scriptlockfile ]
then
    exit 0
else
    touch /tmp/scriptlockfile
fi
and end it with:
Code:
rm -f /tmp/scriptlockfile
  #3 (permalink)  
Old 02-19-2008
Lukeadams Lukeadams is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 4
Yeah, what dragon said.

Personally, I like option 3. There's a million ways ya might get it done, but the three suggestions are probably the basis for most of 'em.

Keep in mind the potential use of the shell variables $! and $$. $! is the PID of the last process run in the background and $$ is the PID of the script itself. You might use the latter to create more-or-less unique lockfile names (time stamp is probably better for that, actually). Still, they may come in useful as your script matures.

Last edited by Lukeadams; 02-19-2008 at 12:28 AM..
  #4 (permalink)  
Old 02-19-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
look into 'man fuser' - no need for lockfiles or ps-ing.
  #5 (permalink)  
Old 02-19-2008
strunz strunz is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 3
Hi all,

Will this work if i keep it in my crontab, please see if any syntax error is there.

#!/bin/ksh

while [ true ]; do

for i=$(ps -ef | grep SCRIPT_NAME |grep -v grep |wc -l)

if [ "$i" -lt 1 ]

then

echo "`date '+%Y%m%d|%H%M%S'` Script Already running!!!"

exit 0

while [ true ]; do

i=$(ps -ef | grep PROCESS_NAME |grep -v grep |wc -l)

if [ "$i" -lt 2 ]

then

echo "`date '+%Y%m%d|%H%M%S'` WARNING !!! PROCESS IS DOWN"

else

echo "`date '+%Y%m%d|%H%M%S'` PROCESS IS RUNNING"

fi

done
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 12: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