Unix administration - Program-wrapper to avoid multiple running instances

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > February 2006 > Program-wrapper to avoid multiple running instances





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Program-wrapper to avoid multiple running instances
norman@unnu.org

2006-02-17, 10:40 pm

Hello,
i am looking for a simple wrapper, that checks if an instance of the
wrapped program is already running. Something like this:

$ wrapper -p /path/to/pid/file /my/program

If an instance is already running the wrapper program should exit
quitely.

Any suggestions?

Norman

Bit Twister

2006-02-17, 10:40 pm

On 16 Feb 2006 08:46:24 -0800, norman@unnu.org wrote:
> Hello,
> i am looking for a simple wrapper, that checks if an instance of the
> wrapped program is already running. Something like this:
>
> $ wrapper -p /path/to/pid/file /my/program
>
> If an instance is already running the wrapper program should exit
> quitely.
>
> Any suggestions?


man pgrep
man ps
man grep

and for some scripting documentation
! bash script introduction documentation
http://tldp.org/LDP/intro-linux/html/index.html
! bash script advanced documentation
http://tldp.org/LDP/abs/html/index.html

Doug Freyburger

2006-02-17, 10:40 pm

Bit Twister wrote:
> norman@unnu.org wrote:
>
>
>
>
> man pgrep
> man ps
> man grep


Some programs already record their PID for this purpose so
it is a well known method. Having a wrapper program to
generalize it to everyone is a great idea. Rather than doing
it is a one-off shell script, are you interested in doing it in
C or C++ or whatever and delivering to to one of the free
software foundations?

Get the PID out of the file, see if that PID is running. If
not launch the specified program, get its PID, record it
in the file. Exit leaving the program daemon-ized.

Stephane CHAZELAS

2006-02-17, 10:40 pm

2006-02-16, 08:46(-08), norman@unnu.org:
> Hello,
> i am looking for a simple wrapper, that checks if an instance of the
> wrapped program is already running. Something like this:
>
> $ wrapper -p /path/to/pid/file /my/program

[...]

Most Linux distributions have such things. Probably other
systems as well.

Generally something called "start-stop-daemon".

--
Stéphane
norman@unnu.org

2006-02-19, 5:50 pm

> Most Linux distributions have such things. Probably other
> systems as well.


> Generally something called "start-stop-daemon".


I don't want to run it as a daemon process. Just as simple as i
described before: A wrapper, that only runs a programm if it is not
already running. This should help to avoid to start multiple long
running tasks of the same kind.

norman@unnu.org

2006-02-19, 5:50 pm

> Some programs already record their PID for this purpose so
> it is a well known method. Having a wrapper program to
> generalize it to everyone is a great idea. Rather than doing
> it is a one-off shell script, are you interested in doing it in
> C or C++ or whatever and delivering to to one of the free
> software foundations?


If a had strong experiences in programming C or C++, i would do it. But
i am more a Ruby guy coming from the web-development world. So maybe
someone else could do this simple job.

> Get the PID out of the file, see if that PID is running. If
> not launch the specified program, get its PID, record it
> in the file. Exit leaving the program daemon-ized.


That's exactly what i was thinking of!

Doug Freyburger

2006-02-19, 8:47 pm

norman@unnu.org wrote:
> Doug Freyburger wrote:
>
>
> If a had strong experiences in programming C or C++, i would do it. But
> i am more a Ruby guy coming from the web-development world. So maybe
> someone else could do this simple job.


My point was it doesn't take strong skills for such a small
project. It's a perfect starter program for someone who
doesn't already have experience in the specific language.
it's going to be under 50 lines. About the size of the typical
homework assignment for a week in an undergrad programming
course. If you've done whatever Ruby is, you have enough
knowledge of developement to use your experience as a
base to build upon.

Stephane CHAZELAS

2006-02-20, 7:48 am

2006-02-19, 15:30(-08), norman@unnu.org:
>
>
> I don't want to run it as a daemon process.


What do you mean?

In which way don't you want it to be started as a daemon?
A daemon is called "daemon" usually by the nature of what it
does. There's no special way to start a daemon. That's some
process that runs on the /background/ and whose actions can be
invoked by other means that terminal interaction.

start-stop-daemon won't do anything special to start the
program, if you don't use the --background option.

If you use the --background option, it will detach the process
from the current terminal and its parent process, and have its
stdin/stdout/stderr point to /dev/null, which is a sensible
thing to do if that process is running in the background
independently of any terminal.

So

start-stop-daemon -Sbvmp /var/pid/sleep.pid -x /bin/sleep -- 100

will start sleep in background detached from the terminal.

start-stop-daemon -Svmp /var/pid/sleep.pid -x /bin/sleep -- 100 &

will start sleep as a background job of the current terminal (if
the shell supports job control). That will be the same as:

/bin/sleep 100 &

except for the handling of the pid file.

This, above is for the start-stop-debian found on debian, I
guess it's the same for other distribution.

$ start-stop-daemon --version
start-stop-daemon 1.13.13
$ start-stop-daemon --help
start-stop-daemon VERSION for Debian - small and fast C version written by
Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, public domain.
[...]

--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com