Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

find Process ID on Win XP?

by sachingupta (Novice)
on Oct 24, 2006 at 00:54 UTC ( [id://580179]=perlquestion: print w/replies, xml ) Need Help??

sachingupta has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, How do I find the process ID of any process by process name on Win XP? Lets say Notepad.exe or performer.exe?? What i want to do is, check if the process/program/application is running if running close it and relaunch it otherwise just launch the application. Thanks. Sachin

Replies are listed 'Best First'.
Re: find Process ID on Win XP?
by Marza (Vicar) on Oct 24, 2006 at 01:20 UTC
Re: find Process ID on Win XP?
by syphilis (Archbishop) on Oct 24, 2006 at 01:27 UTC
    Hi,
    I think you can do this by using Win32::PerfLib - or perhaps the Win32::Process::Info module can also provide what you need.

    I have a rather lengthy demo script that uses Win32::PerfLib to (supposedly) print out the same info as provided by Task Manager. It doesn't work properly (in that it fails to provide all of the info it is supposed to), but it does produce a list of all of the running processes, along with their respective PIDs. So you should be able to adapt it to meet your needs. I'm using Windows 2000 and perl 5.8.8. Hopefully it works well enough for you, too:
    #!perl -w # ==================================================================== +== # $Author: Jmk $ # $Date: 19.05.98 7:12 $ # $Archive: /Jmk/scripts/saa/process.pl $ # $Revision: 2 $ # ==================================================================== +== # shows the task list like the task manager but of any computer use Win32::PerfLib; $server = ""; Win32::PerfLib::GetCounterNames($server, \%counter); %r_counter = map { $counter{$_} => $_ } keys %counter; #foreach $key(keys %counter) { #print "$key: $counter{$key}\n"; #} #__END__ $process_obj = $r_counter{Process}; $process_id = $r_counter{'ID Process'}; $processor_time = $r_counter{'% Processor Time'}; #$process_obj = 230; #$process_id = 784; #$processor_time = 6; #$elapsed = 684; #$memory = 180; #$page_faults = 28; #$virtual_memory = 186; #$priority = 682; #$threads = 680; $perflib = new Win32::PerfLib($server); $proc_ref0 = {}; $proc_ref1 = {}; $perflib->GetObjectList($process_obj, $proc_ref0); sleep 5; $perflib->GetObjectList($process_obj, $proc_ref1); $perflib->Close(); $instance_ref0 = $proc_ref0->{Objects}->{$process_obj}->{Instances}; $instance_ref1 = $proc_ref1->{Objects}->{$process_obj}->{Instances}; foreach $p (keys %{$instance_ref0}) { $counter_ref0 = $instance_ref0->{$p}->{Counters}; $counter_ref1 = $instance_ref1->{$p}->{Counters}; foreach $i (keys %{$counter_ref0}) { next if $instance_ref0->{$p}->{Name} eq "_Total"; if($counter_ref0->{$i}->{CounterNameTitleIndex} == $process_id +) { $process{$counter_ref0->{$i}->{Counter}} = $instance_ref0->{$p}->{Name}; $id{$counter_ref0->{$i}->{Counter}} = $p; } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $process +or_time) { $Numerator0 = $counter_ref0->{$i}->{Counter}; $Denominator0 = $proc_ref0->{PerfTime100nSec}; $Numerator1 = $counter_ref1->{$i}->{Counter}; $Denominator1 = $proc_ref1->{PerfTime100nSec}; $proc_time{$p} = ($Numerator1 - $Numerator0) / ($Denominator1 - $Denominator0 ) * 100; $cputime{$p} = int($counter_ref1->{$i}->{Counter} / 100000 +00); } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $memory) { $memory{$p} = int($counter_ref0->{$i}->{Counter} / 1024); } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $page_fa +ults) { $page_faults{$p} = $counter_ref1->{$i}->{Counter}; } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $virtual +_memory) { $virtual_memory{$p} = int($counter_ref0->{$i}->{Counter} / + 1024); } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $priorit +y) { $priority{$p} = $counter_ref0->{$i}->{Counter}; } elsif($counter_ref0->{$i}->{CounterNameTitleIndex} == $threads +) { $threads{$p} = $counter_ref0->{$i}->{Counter}; } } } print " PID Process CPU CPU-Time Memory PF Virt. +Mem Priority Thr\n"; # 0 Idle 93.73 20:51:40 16 K 1 0 K Un +known 1 foreach $p (sort { $a <=> $b } keys %process) { $id = $id{$p}; $seconds = $cputime{$id}; $hour = int($seconds / 3600); $seconds -= $hour * 3600; $minute = int($seconds / 60); $seconds -= $minute * 60; if ($priority{$id} > 15) { $prio = "Realtime"; } elsif ($priority{$id} > 10 ) { $prio = "High"; } elsif ($priority{$id} > 5 ) { $prio = "Normal"; } elsif ($priority{$id} > 0 ) { $prio = "Low"; } else { $prio = "Unknown"; } printf("% 4d %-14s%5.2f % 3d:%02d:%02d % 8d K % 8d % 8d K %8s % 3 +d\n", $p, $process{$p}, $proc_time{$id}, $hour, $minute, $seconds +, $memory{$id}, $page_faults{$id}, $virtual_memory{$id}, $prio, $threads{$id}); }

    Cheers,
    Rob
Re: find Process ID on Win XP?
by eyepopslikeamosquito (Archbishop) on Oct 24, 2006 at 02:16 UTC

    In addition to the Perl-based solutions, notice that Windows XP has a tasklist command. For example:

    tasklist /fi "imagename eq notepad.exe"
    The sysinternals pslist utility will also do the job. You could call these external programs from your Perl script via backticks and scrape their output.

Re: find Process ID on Win XP?
by planetscape (Chancellor) on Oct 24, 2006 at 15:41 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://580179]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (None)
    As of 2024-04-25 01:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found