For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2004 > Detecting a Process using Perl Win32









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 Detecting a Process using Perl Win32
Tom Tingdale

2004-10-25, 8:56 pm

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. Sinan Unur

2004-10-25, 8:56 pm

Tom Tingdale <tom.tingdale@sbcglobal.net> wrote in
news:cntqn0ljli76jgit7sel1hjk9om7e6co4m@
4ax.com:

> 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...o-1.002/Info.pm

Sinan.
Bryan Williams

2004-10-26, 3:56 am

Tom Tingdale <tom.tingdale@sbcglobal.net> wrote in message news:<cntqn0ljli76jgit7sel1hjk9om7e6co4m@4ax.com>...
> 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;
}
}
Tom Tingdale

2004-10-27, 8:56 pm

Thank you Bryan. I appreciate it!

Tom

On 25 Oct 2004 22:51:45 -0700, bitbucketz2002@yahoo.com (Bryan
Williams) wrote:

>Tom Tingdale <tom.tingdale@sbcglobal.net> wrote in message news:<cntqn0ljli76jgit7sel1hjk9om7e6co4m@4ax.com>...
>
>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;
> }
>}


Tom Tingdale

2004-10-27, 8:56 pm

Thank you Bryan. I appreciate it!

Tom

On 25 Oct 2004 22:51:45 -0700, bitbucketz2002@yahoo.com (Bryan
Williams) wrote:

>Tom Tingdale <tom.tingdale@sbcglobal.net> wrote in message news:<cntqn0ljli76jgit7sel1hjk9om7e6co4m@4ax.com>...
>
>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. Sinan Unur

2004-10-28, 8:56 pm

bitbucketz2002@yahoo.com (Bryan Williams) wrote in
news:38a10436.0410252151.6f7094a3@posting.google.com:

> Tom Tingdale <tom.tingdale@sbcglobal.net> wrote in message
> news:<cntqn0ljli76jgit7sel1hjk9om7e6co4m@4ax.com>...
>
> 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.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com