It would be helpful to know why you need to assure that only one instance of the script is running at any given time. The question would boil down to knowing what resource(s) (or what data) require the constraint: maybe it would be easier/safer to create a lock for the resource/data rather than for the process that uses it. For that matter, if it's an issue of ruling out concurrent access to a given chunk of data, maybe a DBMS (mysql or some such) could handle this job for you.
Also, since you talk about this being employed over multiple hosts in the workplace, you should clarify whether or not the "one copy at a time" applies globally to all hosts. That is, will it be okay to have two copies running at once, so long as those two are running on separate hosts? If not, then obviously your current approach won't handle the problem of locking out other hosts that might be competing for the resource/data. (update: your approach could be extended to handle this case by adding hostname to the pid file, and doing ssh or whatever when appropriate to check for the pid on some other host)
Apart from those concerns, and assuming that "one copy per host" is your intention, then you're present code seems good (if it works as-is on windows, which I don't know), and adding flock on the pid file probably won't improve on it much (er...) should be amended according to replies posted above.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|