Unique Process ?

Unique Process ?

Post by Glen Wile » Tue, 24 Nov 1998 04:00:00



: Hi, I wonder if anybody has a solution to my problem.

: When I start a Solaris 2.6 process up, it has to check
: to make sure it is not already running with the same
: command line arguments. Different arguments are fine,
: the same are not. I have thought of 2 ways, but I just
: wondered if anybody had a more elegant solution.

: 1)
: use popen to spin off a shell that does a ps -aef and then grep
: the result for my process name etc, if there is more than one hit
: the process is a duplicate.

bad, the size limit for command lines on ps and the fact that unless
your regex is rather thorough you run the risk of seeing a process that
is not really the one you wnat

: 2)
: create a file with the name of the command line args. If the file
: already exists, the process is a duplicate. Problem with this is
: if the process is killed or crashes the file is left around so the next
: time you try to start it - it will fail incorrectly.

a little messy

A better solution is to maintain a file that contains a line for each
running instance of your program specifying the PID and whatever it
is that makes the distinction important.  Use fcntl() to lock the
line in the file - the kernel will take care of releaseing the lock
once the proc. dies - very clean and very reliable, also more portable
(fcntl() is POSIX).

If there is the possibility of many variations* around in the
file then I would add a flag at column 0 to indicate that a line can be
reused and obtain a write lock on that line before changing it.

--
Glen Wiley                            

3Com - Carrier Systems R&D

"UNIX _IS_ user friendly, its just picky about who its friends are."

*******************************************************************
* Any statements or opinions expressed by me are mine exclusively *
* and have nothing whatsoever to do with my employer...           *
*******************************************************************

 
 
 

1. Unique Process ?

Hi, I wonder if anybody has a solution to my problem.

When I start a Solaris 2.6 process up, it has to check
to make sure it is not already running with the same
command line arguments. Different arguments are fine,
the same are not. I have thought of 2 ways, but I just
wondered if anybody had a more elegant solution.

1)
use popen to spin off a shell that does a ps -aef and then grep
the result for my process name etc, if there is more than one hit
the process is a duplicate.

2)
create a file with the name of the command line args. If the file
already exists, the process is a duplicate. Problem with this is
if the process is killed or crashes the file is left around so the next
time you try to start it - it will fail incorrectly.

Any more ideas.

Michael

2. Easy Servlet Question

3. Running Virtual Hosts as unique processes?

4. Just Computers! Shipments

5. Unique process? How does httpd kill a CGI script?

6. scripts: ksh calls awk: no stdout?

7. Is a process ID unique ?

8. newb DHCP and IP Address question - I researched before posting

9. How to Kill Process and Its sub-process and its sub-process??

10. How to Kill a process and its sub-process and its sub-process at a time..

11. Unique machine ID

12. Finding a unique hostname

13. A Unique Challenge