| [reply] |
If the pid file is removed for some reason, a next invocation can falsely determine no other invocation is running. OTOH, if an invocation dies unexpectedly, it may leave behind the pid file.
If the pid contained in the pid lock file is not in the process table, you should display a message asking if the process is running. If not, you can prompt the user to remove the file.
If the file does not exist (false negative), there is a logic error in your program (the pid lock file should be the first object created and the last destroyed).
You'd have to use file locks. But then you may as well use file locks in the first place.
The point of using a predictable lock file (program_name.pid) in a predictable location (/var/run ?) is to handle all of the situations you describe. Data files used by the program may or may not be predictable. Directory locations may or may not be predictable (relative vs. absolute paths).
Using the conventions that have grown over the years (/var/run for pid lock files) work for a reason. YMMV
| [reply] |
If the pid file is removed for some reason, a next invocation can falsely determine no other invocation is running. OTOH, if an invocation dies unexpectedly, it may leave behind the pid file.
If the pid contained in the pid lock file is not in the process table, you should display a message asking if the process is running. If not, you can prompt the user to remove the file.
Yes, but that's an if. And you conveniently left off the next line:
By the time the program is invoked again, the pid may have been reused;
So, we now have a pid file, with the pid it contains in the process table. Now what?
If the file does not exist (false negative), there is a logic error in your program (the pid lock file should be the first object created and the last destroyed).
I'm sure you are aware there are OSses that allow processes to remove files they haven't written to. I'm unware of any programming technique that prevents other processes (for instance, sometime typing 'rm file') from removing files. Please enlighten us, so we never have to make such logical errors in our programs again!
The point of using a predictable lock file (program_name.pid) in a predictable location (/var/run ?) is to handle all of the situations you describe. Data files used by the program may or may not be predictable.
Hmmm, a pid file is a data file. It contains data, doesn't? Data you're using to determine whether an instance is running or not.
Using the conventions that have grown over the years (/var/run for pid lock files) work for a reason. YMMV
I've been around long enough to know that while pid files often work, they sometimes don't. For the reasons I explained. You've never had to remove a pidfile to allow a program to start? Get some more experience.
| [reply] |