On Mar 12, 2004, at 9:06 AM, Christian Stalp wrote:

Hello together,
I have a question regarding process-control. I want to write a perl script
which must not running in more than one process in the same time. The script
creates a directory copys a zip-file in it, unpack it and reads every file in
this package. However, the files must not get opened by another process.
How can I make sure, that my perl-scipt runs only in one process, or eaysier,
is there a function "flock" which blocks a whole directory and not only a
single file?

Step 1, when your script launches, have it create a file in some convenient directory. Maybe call it "LOCK", or something similar. It doesn't need a thing in it, just make a file.


Step 2, if the file already exists when your script launches, have it abort with some notification.

Note: You can, and should, combine the check for the file and creation of it with Perl's sysopen() built-in.

You can also go farther, say writing the creating processes' PID to the file and using that as a secondary check to make sure the process is actually running. This is starting to be work though of super questionable value for a simple script. Use the proper amount of paranoia for your particular case.

Step 3, ensure that your script unlink()s the file on exit. Perl's END { } blocks are ideal for this.

Hope that helps.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to