The standard Unix solution for this is to create a «run file». When you start up, you try to create that file and write your pid to it if it doesn’t exist; if it does exist, read the pid out of it, and if there’s a running program with that pid and your process name, wait/exit/whatever.
The question is, where do you put that file, and what do you call it?
Well, first, you have to decide what exactly «already running» means. Obviously not «anywhere in the world», but it could be anything from «anywhere on the current machine» to «in the current desktop session». (For example, if User A starts your program, pauses it, then User B comes along and takes over the computer via Fast User Switching, should she be able to run the program, or not?)
For pretty much any reasonable answer to that question, there’s an obvious pathname pattern. For example, on a Mac, /tmp is shared system-wide, while $TMPDIR is specific to a given session, so, e.g. /tmp/$
answered May 31 ’12 at 0:19
Thank you @abarnert.
This is how I have presently implemented. At the start of the main(), I would check if a file named .lock exists in the binary’s own directory (I am considering moving it to /tmp). If it is, application exits.
If not, it would create the file. At the end of the application, the .lock file is removed
I haven’t yet written the pid to that file, but I will when exiting the previous instance is required (as of yet I don’t need it, but may in the future). I think PID can be retrieved using
The program will be invoked by a custom scheduler which is running as a root daemon. So it would be run as root.
Seeing the answers, I would assume that there is no direct method of solving the problem.
answered May 31 ’12 at 5:32
There are two reasons to write the pid. First, as you implied, that's how you can kill or otherwise signal the already-running copy (one of the options you asked for in the original question). Second, it lets you distinguish an abandoned file left by a crashed process from a live one (e.g. by doing a kill with signal 0). – abarnert May 31 ’12 at 18:02
Right, there's no direct way of doing this. There are a wide range of indirect methods, but they all boil down to two things: some kind of predictably-named thing (pidfile, socket, named pipe, NT named kernel mutex), or some kind of broadcast mechanism (Mac distributed notification, Windows WM_ broadcast message, etc.). There are libraries that wrap up the details (see stackoverflow.com/questions/380870/… for some Python solutions, for example), although the only C and ObjC ones I know off the top of my head are part of larger frameworks. – abarnert May 31 ’12 at 18:10
I'm still having issues with the 'lock file' concept. Now I'm thinking of using a ps -ef | grep <argv> | grep -vE 'grep' | awk '
another instance of the application is already running exiting что это:The standard Unix solution for this is to create a «run file». When you start up, you try to create that file and write your pid to it if it doesn’t exist; if it does exist, read the pid out of it,