M
Michele Dondi
I apologize if the subject is not really appopriate to this post, but
I really couldn't think of a better one...
To come to the point, I have a process that (partially) daemonizes and
uses a mechanism to prevent having more than one instance running on
any given machine.
As of it is now, I must say that it has been working perfectly for
quite a while and I've not had any problem with it. Basically the key
code snippet is as follows:
my $lockfile = "$lockdir/$pfx-$host";
sysopen my $lock, $lockfile, O_CREAT | O_EXCL
or die "$0 already running on $host\n";
my $havelock = 1;
# ...
However (also) for learning purposes, I've been thinking of improving
it to cope with the unlikely possibility that a badly crashed machine
prevented my signal handlers or C<END> block to take care of lockfile
removal.
So the additional steps that I'm taking into account are:
(i) writing the pid of the running instance into the lockfile and
check for its existence,
(ii) once (i) is done, then also check that the process corresponding
to the stored pid corresponds to a program called like $0.
Now, the questions are: would (i) be a good idea? Would (ii) be a good
idea too?
Also, (and on a Perl-related basis, finally!) if the answers to the
above questions are positive, then when I open a file with sysopen(),
can/should I subsequently use the "standard" IO operators (i.e. those
I'd use with an open()ed file) and functions to read and write to it?
As for the point (ii) above, since the program is to be run on Linux
anyway, it would seem sensible to me to read the relevant info from
/proc myself (don't know if it could be doable under other UNICES): is
it recommended to use some module or ad hoc code instead?
Last, since I'm not much confident with this kind of programming,
could you please be so kind and post some explicit minimal code
snippet?
TIA,
Michele
I really couldn't think of a better one...
To come to the point, I have a process that (partially) daemonizes and
uses a mechanism to prevent having more than one instance running on
any given machine.
As of it is now, I must say that it has been working perfectly for
quite a while and I've not had any problem with it. Basically the key
code snippet is as follows:
my $lockfile = "$lockdir/$pfx-$host";
sysopen my $lock, $lockfile, O_CREAT | O_EXCL
or die "$0 already running on $host\n";
my $havelock = 1;
# ...
However (also) for learning purposes, I've been thinking of improving
it to cope with the unlikely possibility that a badly crashed machine
prevented my signal handlers or C<END> block to take care of lockfile
removal.
So the additional steps that I'm taking into account are:
(i) writing the pid of the running instance into the lockfile and
check for its existence,
(ii) once (i) is done, then also check that the process corresponding
to the stored pid corresponds to a program called like $0.
Now, the questions are: would (i) be a good idea? Would (ii) be a good
idea too?
Also, (and on a Perl-related basis, finally!) if the answers to the
above questions are positive, then when I open a file with sysopen(),
can/should I subsequently use the "standard" IO operators (i.e. those
I'd use with an open()ed file) and functions to read and write to it?
As for the point (ii) above, since the program is to be run on Linux
anyway, it would seem sensible to me to read the relevant info from
/proc myself (don't know if it could be doable under other UNICES): is
it recommended to use some module or ad hoc code instead?
Last, since I'm not much confident with this kind of programming,
could you please be so kind and post some explicit minimal code
snippet?
TIA,
Michele