Home > Archive > PERL Beginners > March 2004 > how to make sure, that the script is running one time?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to make sure, that the script is running one time?
|
|
| Christian Stalp 2004-03-18, 6:23 pm |
| 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?
Gruss Christian
| |
| Bob Showalter 2004-03-18, 6:23 pm |
| 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.
Are you extracting the files just to read through them? Why not just extract
to a pipe and read the data from that? Then you can avoid creating the files
in the first place?
> How can I make sure, that my perl-scipt runs only in one process
This is typically done by acquiring a lock on a file (usually a "pid" file
that contains the process id number). There's a Proc::PID::File module on
CPAN that looks pretty neat, though I haven't used it personally.
> , or
> eaysier, is there a function "flock" which blocks a whole directory
> and not only a single file?
Not really a way to do that with a lock, and locks are "advisory" only,
depending on your O/S. You might want to play with directory permissions...
| |
| James Edward Gray II 2004-03-18, 6:23 pm |
| 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
| |
| Randal L. Schwartz 2004-03-18, 6:23 pm |
| >>>>> "Christian" == Christian Stalp <stalp@imbei.uni-mainz.de> writes:
Christian> I have a question regarding process-control. I want to
Christian> write a perl script which must not running in more than one
Christian> process in the same time. The script creates a directory
Christian> copys a zip-file in it, unpack it and reads every file in
Christian> this package. However, the files must not get opened by
Christian> another process. How can I make sure, that my perl-scipt
Christian> runs only in one process, or eaysier, is there a function
Christian> "flock" which blocks a whole directory and not only a
Christian> single file?
See "There Can Be Only One (the HIGHLANDER solution)" at
<http://www.stonehenge.com/merlyn/We...ques/col54.html>.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
|
|
|
|
|