in reply to Process Already Running
A safe approach can be based on file locking (flock), which is *meant* to guarantee exclusive access. The following code shows how that can be done in a Unix-ish environment. I'm sure there is already a module on CPAN (or two) that does this in a more portable way, but I couldn't find it in a hurry.
Update: I forgot to mention that the file /var/run/ttt.pid must be pre-created and given ownership and permissions so that the user that runs the process can write it.#!/usr/local/bin/perl use strict; use warnings; $| = 1; use Fcntl qw( :flock); my $name = 'ttt'; # process name my $pid_file = "/var/run/$name.pid"; open my $pid_handle, '+<', $pid_file or die "Can't access '$pid_file': + $!"; flock $pid_handle, LOCK_EX | LOCK_NB or die "Process $name is still ac +tive"; truncate $pid_handle, 0 or die "Truncating '$pid_file' failed: $!"; print $pid_handle "$$\n"; # Normal processing starts here # # ... sleep 10; # for testing
Anno
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Process Already Running
by Photius (Sexton) on Sep 13, 2007 at 21:28 UTC | |
by Anno (Deacon) on Sep 13, 2007 at 22:31 UTC | |
by Photius (Sexton) on Sep 14, 2007 at 01:33 UTC | |
by Photius (Sexton) on Sep 14, 2007 at 16:49 UTC |