in reply to Re^2: Process Already Running
in thread Process Already Running

Heh, I didn't even notice the bug in your proposed scheme.

In your sample code above, why did you open the pid file with +< (is this read-write but don't create?)?

First off, I don't want to clobber the file before I've made sure I'm allowed to run. That's why plain write access won't do. Write access is required because many systems (foolishly, imo) forbid an exclusive lock on a file that's only open for reading. So even if I didn't intend to write my PID into the file (which is inessential, see below), simple read access would break in some places.

And what is the purpose for writing the Perl Process ID $$ to the file? Please help me understand how that is used?

That is absolutely inessential and not used at all. The locking scheme needs a file, unique to the specific program, that can be exclusively locked by the process. I have chosen a convention that is common in Unix systems, associating a process named ttt with a file /var/run/ttt.pid. As the name implies, it is commonly used to store the PID of the running process, and so I've done the same. The locking mechanism would also work without writing anything to the file, or with any other file for that matter.

I forgot to mention (but will update) that you would have to pre-create the pid file /var/run/ttt.pid (once), and give it ownership and permissions that allow the process write access. A real program should probably explain that in an error message if the file is not found.

Anno

Replies are listed 'Best First'.
Re^4: Process Already Running
by Photius (Sexton) on Sep 14, 2007 at 01:33 UTC
    Thanks!! That helps greatly.
Re^4: Process Already Running
by Photius (Sexton) on Sep 14, 2007 at 16:49 UTC
    Thanks! That really helped my understanding.