Detecting a Process using Perl Win32

T

Tom Tingdale

I have a perl program that launchs every minutes using a Windows Task
Scheduler to process some files. If the Perl program is still running,
I don't want to run it again until the previous Perl application has
finished. It is possible to detect whether or not my Perl application
is already running?
 
A

A. Sinan Unur

I have a perl program that launchs every minutes using a Windows Task
Scheduler to process some files. If the Perl program is still running,
I don't want to run it again until the previous Perl application has
finished. It is possible to detect whether or not my Perl application
is already running?

One relatively more portable way is to use lock files. See

perldoc -q lock

You can also try Win32::process::Info:

http://search.cpan.org/~wyant/Win32-Process-Info-1.002/Info.pm

Sinan.
 
B

Bryan Williams

Tom Tingdale said:
I have a perl program that launchs every minutes using a Windows Task
Scheduler to process some files. If the Perl program is still running,
I don't want to run it again until the previous Perl application has
finished. It is possible to detect whether or not my Perl application
is already running?

sub runUpdate{
my $pi = Win32::process::Info->new();
my $Updates = 0;

foreach $proc ($pi->GetProcInfo())
{
foreach ($proc)
{
if ($proc->{Name} eq "update.exe")
{
#Count the number of running instances
$Updates++
}
}
}
# if no running update.exe found start it otherwise exit
if ($Updates <= 1)
{
doit();
}
else
{
warn "An instance of the Updater is running, exiting now.";
exit;
}
}
 
T

Tom Tingdale

Thank you Bryan. I appreciate it!

Tom

sub runUpdate{
my $pi = Win32::process::Info->new();
my $Updates = 0;

foreach $proc ($pi->GetProcInfo())
{
foreach ($proc)
{
if ($proc->{Name} eq "update.exe")
{
#Count the number of running instances
$Updates++
}
}
}
# if no running update.exe found start it otherwise exit
if ($Updates <= 1)
{
doit();
}
else
{
warn "An instance of the Updater is running, exiting now.";
exit;
}
}
 
T

Tom Tingdale

Thank you Bryan. I appreciate it!

Tom

sub runUpdate{
my $pi = Win32::process::Info->new();
my $Updates = 0;

foreach $proc ($pi->GetProcInfo())
{
foreach ($proc)
{
if ($proc->{Name} eq "update.exe")
{
#Count the number of running instances
$Updates++
}
}
}
# if no running update.exe found start it otherwise exit
if ($Updates <= 1)
{
doit();
}
else
{
warn "An instance of the Updater is running, exiting now.";
exit;
}
}
 
A

A. Sinan Unur

(e-mail address removed) (Bryan Williams) wrote in
sub runUpdate{
my $pi = Win32::process::Info->new();
my $Updates = 0;

foreach $proc ($pi->GetProcInfo())
{
foreach ($proc)
{
if ($proc->{Name} eq "update.exe")
{
#Count the number of running instances
$Updates++
}
}
}


Huh? In Perl, one would do:

my $instances = grep { /^update.exe$/ } @{[ $pi->GetProcInfo()};
# if no running update.exe found start it otherwise exit
if ($Updates <= 1)
{
doit();
}

Ahem, the criterion was to not start an update if update.exe was already
running. So, there should be no instances of 'update.exe' running.

However, this is neither here nor there. Since the operation of counting
the number of instances of update.exe in the process table is not atomic,
this code will not prevent two or more instances of update.exe from
running.

That is, an update process might be launched between searching the
process table for 'update.exe' entries, and the check on the number of
instances.

Sinan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top