Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

starting a win2000-service with delay

by tos (Deacon)
on Sep 19, 2003 at 07:09 UTC ( #292600=perlquestion: print w/replies, xml ) Need Help??

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

Howdy brethren,

for some reasons i need to start a win2000-service only about 2 hours, and NOT immediatly, after system-startup automatically. The service runs under another user than my login-user but i know its pw. My login-user has administration-rights.

I remember some time ago i red something about service-controlling with perl.

Any suggestions how to realize this ?

greetings, tos

Replies are listed 'Best First'.
Re: starting a win2000-service with delay
by rupesh (Hermit) on Sep 19, 2003 at 10:22 UTC

    You can use the Win32::Service to start and stop services in your system.
    This is a small script to get you started.
    use Win32::Service; #set up a hash of known service states my %statcodeHash = ( '1' => 'stopped.', '2' => 'start pending.', '3' => 'stop pending.', '4' => 'running.', '5' => 'continue pending.', '6' => 'pause pending.', '7' => 'paused.' ); my %serviceHash; #Win32::Service::StartService("", <some service>); #go get 'em... Win32::Service::GetServices("", \%serviceHash); foreach $key(keys %serviceHash) { my %statusHash; Win32::Service::GetStatus("", "$key", \%statusHash); #print "$statusHash{\"CurrentState\"} \n"; if ($statusHash{"CurrentState"} =~ /[1-7]/) { print $serviceHash{"$key"} . " is currently " . $statcodeHas +h{$statusHash{"CurrentState"}} . "\n"; } }
    Use the sleep command for the delay, If you want to start the service after sometime.

    Did you ever notice that when you blow in a dog's face, it gets mad at you but when you take him on a car ride,he sticks his head out the window and likes it?
Re: starting a win2000-service with delay
by inman (Curate) on Sep 19, 2003 at 12:14 UTC
    Write a perl script that when run (on start up) uses the windows scheduler to create a scheduled job to start the service two hours later. The job will run once and then be removed from the scheduled list. The user that the service runs as is configured in the services control panel.

    The following example starts the service 'myService' after 7200 seconds (two hours).

    #! /usr/bin/perl -w
    use strict;
    use warnings;
    my $command = "net start myService";
    my $delay = 7200;
    my $startTime = time + $delay;
    my ($seconds, $minutes, $hours, $day) = localtime $startTime;
    `at $hours:$minutes /next:$day $command`;


Re: starting a win2000-service with delay
by PodMaster (Abbot) on Sep 19, 2003 at 07:41 UTC
    You could start a perl program (or not) which sleeps (or schedules) the starting of said service (net start "service name"). How you chose is up to you, but I personally wouldn't involve perl (perl at startup only slows startup).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: starting a win2000-service with delay
by flounder99 (Friar) on Sep 19, 2003 at 11:58 UTC
    Maybe you could use windows' at.
    my $delay = 3600; my $$service2start = "C:\\path\\mycoolservice.exe"; my $starttime = sprintf "%d:%02d:%02d", (localtime(time+$delay))[2,1,0 +]; exec("at", $starttime, "net", "start", $service2start);
    This is untested but it might start you in the right direction.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://292600]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2021-01-27 17:49 GMT
Find Nodes?
    Voting Booth?