Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: how to print out windows service status

by poj (Abbot)
on Jul 31, 2017 at 15:10 UTC ( [id://1196366]=note: print w/replies, xml ) Need Help??


in reply to Re^2: how to print out windows service status
in thread how to print out windows service status

I'm guessing you want $status{'CurrentState'}. Try

#!perl use strict; use Win32::Service qw'GetServices GetStatus'; my @state_name = qw( '' Stopped START_PENDING STOP_PENDING Started CONTINUE_PENDING PAUSE_PENDING PAUSED ERROR ); my $hostname = 'localhost'; my %service=(); GetServices($hostname,\%service) or die "$!"; for my $name (sort keys %service){ my $shortname = $service{$name}; my %status = (); # keys to %status # CheckPoint # ControlsAccepted # CurrentState # ServiceSpecificExitCode # ServiceType # WaitHint # Win32ExitCode GetStatus($hostname, $shortname, \%status); my $state_no = $status{'CurrentState'}; printf " ServiceStatus: %s %s %s \n", $hostname, $shortname, $state_name[$state_no]; }
poj

Replies are listed 'Best First'.
Re^4: how to print out windows service status
by ytjPerl (Scribe) on Jul 31, 2017 at 17:35 UTC
    Hi poj, Can I know what '' is for in the front of your array @state_name? Thanks!

      It's the first element of the array, try print $state_name[0]

        use Win32::Service qw'GetServices GetStatus'; use strict; use POSIX; use autodie; my ($hostname, $servicename, $entry); my (%status, @array); my @state_name = qw( '' Stopped START_PENDING STOP_PENDING Started CONTINUE_PENDING PAUSE_PENDING PAUSED ERROR ); use constant DATETIME => strftime("%Y%m%d%H%M%S", localtime); # my $filename = '/Perl/XiServiceList.txt'; sub buildarray() #array subroutine { # text file read and loaded on startup #open (MYFILE, '>>XiPerlService.txt'); #open for write, o +verwrite #print MYFILE $theTime, " Start of perl module\n"; open( FILE, "< $filename" ) or die "Can't open $filename : $!"; while( <FILE> ) { my $line = $_; chomp $line; push @array, ($line); } close FILE; # my $loop = 1; while ($loop lt 4) #infinite loop if setting this + up to run continually i.e. service { #part of - infinite loop my $count = 0; my $entry; foreach $entry (@array) #loops through entire loaded +array { open (MYFILE, ">> /Perl/daily\.". DATETIME."\.txt"); + #open for write, overwrite if (($count == 0)) #test 1st entry for host addr +ess { $hostname = "$entry"; $count = $count + 1; } else { $servicename = "$entry"; sleep(2); &CheckServiceStatus($hostname,$servicename, \%status); $count = $count + 1; sleep(2); } close MYFILE; } #sleep(300); #part of - infinite loop + $loop = $loop + 1; } #part of - infinite loop } # sub CheckServiceStatus() # check service sub routine { GetStatus($hostname, $servicename, \%status); my $state_no = $status{'CurrentState'}; printf MYFILE " ServiceStatus: %s %s %s \n", $hostname, $servicename, +$state_name[$state_no]; } sub main() { buildarray(); #run array subroutine } main();
        Hi poj, Above is my script, if I have '', all the status are printed out ' ', if I remove '', all the status will be printed out 'stopped', it is not getting actual status, But when I run your script, I could get 'started' and 'stopped', then problem is I do not get all the services my OS have, only certain services. I do not really quite understand it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-19 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found