Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

using TaskScheduler to reboot Win32 machine

by ethrbunny (Monk)
on Jul 11, 2007 at 19:52 UTC ( #626069=perlquestion: print w/replies, xml ) Need Help??

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

Im trying to put together a script that will create a task scheduler entry that will reboot a machine 1x / week. I have it working where it will create the task scheduler entry but it's not getting formatted correctly in the resultant item. Since the command in question has spaces in it the task scheduler is putting quotes around the whole command. This is confusing the entry which uses 'perl -e' to run the command and requires quotes around its part. Confused yet? Here's the code:

#!/cygdrive/c/perl/bin/perl -w use Win32::TaskScheduler; use strict; my $taskName = "weekly reboot"; my $APP_NAME = q#c:\\perl\\bin\\perl.exe -MWin32 -e "Win32::InitiateSy +stemShutdown(' ','Rebooting',5,1,1);"#; my $ACCOUNT_NAME = "bob"; my $ACCOUNT_PW = "jones"; my $scheduler = Win32::TaskScheduler->New(); $scheduler->Activate( $taskName ); # # This adds a weekly schedule. my %taskTrigger = ( 'BeginYear' => 2007, 'BeginMonth' => 1, 'BeginDay' => 1, 'StartHour' => 1, 'StartMinute' => 0, 'TriggerType' => $scheduler->TASK_TIME_TRIGGER_WEEKLY, 'Type'=>{ 'WeeksInterval' => 1, 'DaysOfTheWeek +' => $scheduler->TASK_SUNDAY, }, ); $scheduler->NewWorkItem( $taskName, \%taskTrigger ); $scheduler->SetApplicationName( $APP_NAME ); # run forever $scheduler->SetMaxRunTime( $scheduler->INFINITE ); $scheduler->SetAccountInformation( $ACCOUNT_NAME, $ACCOUNT_PW ); $scheduler->Save();

I've tried various combinations of single and double quotes. If I create the task manually and put double quotes around the 'Win32::InitiateSystemShutdown' call it works.

addendum: task scheduler puts quotes around any command that has spaces in it... and doesn't work.. I tried putting 'shutdown.exe -r -t 5 -f' in there.

Replies are listed 'Best First'.
Re: using TaskScheduler to reboot Win32 machine
by bmann (Priest) on Jul 12, 2007 at 01:26 UTC
    The docs (Win32::TaskScheduler) say SetApplicationName expects just the name of the app. Use SetParameters for the rest, ie:

    my $APP_NAME = q#c:\\perl\\bin\\perl.exe#; my $PARAMS = q#-MWin32 -e "Win32::InitiateSystemShutdown(' ','Rebootin +g',5,1,1);"#; # ... $scheduler->SetApplicationName( $APP_NAME ); $scheduler->SetParameters( $PARAMS );

    Note: untested!!! now tested, parameters and command line are properly quoted.

      Nice catch. That was biting me in the rear all afternoon.

      Oddly enough - it appears quoted correctly in the task scheduler but when 'run' it doesn't seem to work. It doesn't give any errors like before ("can't start", etc) though. Hmph.
        Have you checked for the last system message after the call from within Perl? That might tell if you if it's running at all and help determine the issue

        print "SysMsg - " . Win32::FormatMessage(Win32::GetLastError());

        Also, anything in the event log after you attempt the run?

        On the Windows side: I assume user/pass is a static account that will never change? Otherwise if you have a rotating password scheme for security in place, this will haunt you later in a bad way!
Re: using TaskScheduler to reboot Win32 machine
by Anonymous Monk on Jul 11, 2007 at 21:24 UTC
    You might try just using the shutdown command in windows to simplify $APP_NAME:
    Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t +xx] [-c "comment"] [-d up:xx:yy] No args Display this message (same as -?) -i Display GUI interface, must be the fir +st option -l Log off (cannot be used with -m option +) -s Shutdown the computer -r Shutdown and restart the computer -a Abort a system shutdown -m \\computername Remote computer to shutdown/restart/ab +ort -t xx Set timeout for shutdown to xx seconds -c "comment" Shutdown comment (maximum of 127 chara +cters) -f Forces running applications to close w +ithout war ning -d [u][p]:xx:yy The reason code for the shutdown u is the user code p is a planned shutdown code xx is the major reason code (positive +integer less than 256) yy is the minor reason code (positive +integer less than 65536)
Re: using TaskScheduler to reboot Win32 machine
by Cabrion (Friar) on Jul 11, 2007 at 20:26 UTC
    Try double quoting the whole thing and using single quote the part after the -e

    my $APP_NAME = q#"c:\\perl\\bin\\perl.exe -MWin32 -e Win32::InitiateSy +stemShutdown('','Rebooting',5,1,1);"#
Re: using TaskScheduler to reboot Win32 machine
by Anonymous Monk on Jul 11, 2007 at 22:21 UTC
    If you cannot find a way to jigger the quotes, just put your code in a file (say... "myboot.bat") and schedule that batch file.
Re: using TaskScheduler to reboot Win32 machine
by magikstik! (Initiate) on Jul 12, 2007 at 11:20 UTC
    use backticks => ex: $command=qx{ sort `grep -l 'conf' *` }; #don't confuse ` with '
    just wondering, since u have perl installed.
    q#c:\\perl\\bin\\perl.exe -MWin32 -e "Win32::InitiateSystemShutdown(' ','Rebooting',5,1,1);"#; try:
    qx { `perl -MWin32 -e` "Win32::InitiateSystemShutdown(' ','Rebooting',5,1,1);" } ; #cleaner imo yea so...i don't use cygwin, linux ftw.
Re: using TaskScheduler to reboot Win32 machine
by aufflick (Deacon) on Jul 12, 2007 at 11:33 UTC
    OT, but did you know you can run a cron service under Cygwin?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2021-01-15 17:48 GMT
Find Nodes?
    Voting Booth?