Ensuring single instance of a program
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Ensuring single instance of a program




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Ensuring single instance of a program  
Kelvin Moss


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-05 08:18 AM

Hi group,

What is the general way to ensure that only a single instance of a
program is running ?

One way I can think of is that process holds a mandatory lock on a
file. If one process is running, then others cannot. What if the
process exits abnormally ? I think the lock will not be disowned. All
attempts to restart the process again will fail until some
administrative action is taken.

Any suggestions ?

Thanks in advance.






[ Post a follow-up to this message ]



    Re: Ensuring single instance of a program  
Rich Teer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-05 08:18 AM

On Wed, 16 Mar 2005, Kelvin Moss wrote:

> One way I can think of is that process holds a mandatory lock on a

Why mandatory?  If you're writing the app, you can avoid mandatory locks.

> file. If one process is running, then others cannot. What if the
> process exits abnormally ? I think the lock will not be disowned. All

Nope; all locks held by a process are released when that process terminates.

> Any suggestions ?

I use a lock file (usually the file that the PID is held in), and use a
discretionary lock.

--
Rich Teer, SCNA, SCSA

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich





[ Post a follow-up to this message ]



    Re: Ensuring single instance of a program  
Kelvin Moss


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-05 08:18 AM

* Rich Teer wrote
>I use a lock file (usually the file that the PID is held in), and use
a
> discretionary lock.

Thanks for the reply. You mean a simple advisory lock (using flock or
fcntl) is good enough. I don't understand this line very well --
"usually the file that the PID is held in", do you mean a lock on my
source file ?

Thanks again.






[ Post a follow-up to this message ]



    Re: Ensuring single instance of a program  
Bill Seivert


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-05 08:18 AM



Kelvin Moss wrote:
> Hi group,
>
> What is the general way to ensure that only a single instance of a
> program is running ?
>
> One way I can think of is that process holds a mandatory lock on a
> file. If one process is running, then others cannot. What if the
> process exits abnormally ? I think the lock will not be disowned. All
> attempts to restart the process again will fail until some
> administrative action is taken.
>
> Any suggestions ?
>
> Thanks in advance.
>
I generally use a file with the pid of the shell script that executes
the program.

The process goes something like this (all done in the shell script):
The pid file is in /tmp with a unique name like the basename of the
script with a ".lock" suffix.  Let's call it pidfile.

mybase=`basename $0`
pidfile=/tmp/$mybase.lock
if [ -f $pidfile ] ; then
# File exists, read the pid.
read pid < $pidfile
if [ `expr "$pid" : '[0-9][0-9]*$'` -ne 0 ] ; then
# pid is a valid number, see if that process is active.
# The sed 1d gets rid of the ps header line.
active=`ps -p $pid | sed 1d`
if [ "$active" != "" ] ; then
# There is an active process with this pid.
if [ `expr "$active" : ".*$mybase"` -ne 0 ] ; then
# The pid is from a previous running of this script.
echo "$mybase: Previous run still active, pid $pid."
exit 1
# Else
# Different process now has this pid, can reuse
# lock file.
fi
# Else
# No active process with this pid, can reuse lock file.
fi
# Else
# No valid pid in pidfile, can reuse lock file.
fi
# Else
# No pidfile, will create lock file.
fi
# Use umask 000 so any user can rewrite the file to reuse the lock.
(umask 000
echo "$$" > $pidfile
)
# Okay to execute the program.

# Finally, remove the pidfile.
/bin/rm -f $pidfile

Granted, there is a timing window here but it isn't very big.
This method handles the case of the script terminating abnormally
without removing the pidfile.


Bill Seivert






[ Post a follow-up to this message ]



    Re: Ensuring single instance of a program  
J.N.Subrahmanyam


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-05 12:52 PM

Basic logic goes thus -

1. Try to acquire an advisory lock (via fcntl) on a pre-defined file such as
'/tmp/daemon.pid'. If it is already locked, exit. Else, acquire the lock.
2. upon  acquiring the lock, truncate the contents and write down the value
of getpid( )
3. hold the lock till the daemon exits

If another instance of the program runs, it will exit in step1. Thus
ensuring only one instance of the program runs any time.

You may refer to pp. 375-376 of "Advanced programming in Unix environment"
by Richard Stevens.
The program 12.5 exactly addresses this issue.

Regards,
Subrahmanyam N Jiddu


"Kelvin Moss" <km_jr_usenet@yahoo.com> wrote in message
news:1111035448.236404.285380@o13g2000cwo.googlegroups.com...
Hi group,

What is the general way to ensure that only a single instance of a
program is running ?

One way I can think of is that process holds a mandatory lock on a
file. If one process is running, then others cannot. What if the
process exits abnormally ? I think the lock will not be disowned. All
attempts to restart the process again will fail until some
administrative action is taken.

Any suggestions ?

Thanks in advance.







[ Post a follow-up to this message ]



    Re: Ensuring single instance of a program  
Kelvin Moss


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-05 12:52 PM


J.N.Subrahmanyam wrote:
> Basic logic goes thus -
>
> 1. Try to acquire an advisory lock (via fcntl) on a pre-defined file
such as
> '/tmp/daemon.pid'. If it is already locked, exit. Else, acquire the
lock.
> 2. upon  acquiring the lock, truncate the contents and write down the
value
> of getpid( )
> 3. hold the lock till the daemon exits
>
> If another instance of the program runs, it will exit in step1. Thus
> ensuring only one instance of the program runs any time.
>
> You may refer to pp. 375-376 of "Advanced programming in Unix
environment"
> by Richard Stevens.
> The program 12.5 exactly addresses this issue.

Thank you, Rich, Bill and Subrahmanyam. I got a decent solution with
your help.






[ Post a follow-up to this message ]



    Re: Ensuring single instance of a program  
Chuck Dillon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-05 10:55 PM

Bill Seivert wrote:

>
>
> Granted, there is a timing window here but it isn't very big.

Put another way:  If it's ok for your script/program to work most of
the time do this.

-- ced


--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.





[ Post a follow-up to this message ]



    Re: Ensuring single instance of a program  
Rich Teer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-05 10:55 PM

On Wed, 16 Mar 2005, Kelvin Moss wrote:

> Thanks for the reply. You mean a simple advisory lock (using flock or

Yes.  The proper name escaped me last night!  :-)

> fcntl) is good enough. I don't understand this line very well --
> "usually the file that the PID is held in", do you mean a lock on my
> source file ?

No.  It's not uncommon for daemons to store their pid in a file, and
this file is usually the one that gets locked.

See chapter 18 of my book, Solaris Systems programming for an example.

--
Rich Teer, SCNA, SCSA

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich





[ Post a follow-up to this message ]



    Re: Ensuring single instance of a program  
Thomas Dickey


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-18-05 01:48 AM

J.N.Subrahmanyam <jnsubbu@india.hp.com> wrote:

> You may refer to pp. 375-376 of "Advanced programming in Unix environment"
> by Richard Stevens.
> The program 12.5 exactly addresses this issue.

Indeed.  I wouldn't have noticed that in this particular context (except hav
ing
the other followups freshly posted), but compare against Teer program 18.5,
taking into account the renaming of Steven's macros (see Stevens page 370
versus Teer page 515), e.g.,

#define write_lock(fd, offset, whence, len) \
lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len)

vs

#define ssp_wlock(fd, offset, whence, len) \
ssp_lock(fd, F_SETLK, F_WRLCK, offset, whence, len)

Stevens as usual conveys the information more lucidly than his imitator.

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net





[ Post a follow-up to this message ]



    Re: Ensuring single instance of a program  
Thomas Dickey


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-18-05 01:48 AM

Thomas Dickey <dickey@saltmine.radix.net> wrote:

> #define ssp_wlock(fd, offset, whence, len) \
> 	ssp_lock(fd, F_SETLK, F_WRLCK, offset, whence, len)

minor correction:

#define ssp_wlock(fd, whence, start, len) \
ssp_lock(fd, F_SETLK, F_WRLCK, whence, start, len)

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:58 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register