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

Dear Monks

I am trying to run a perl script from Vista machine, using Task scheduler daily at a set time. As a test script I am running

my $fname = "output\.txt"; unless(open(OUTPUT,">$fname")) { exit } print OUTPUT "This worked\n"; close(OUTPUT);
I tried running this using the action "c:\perl\bin\perl.exe "d:\scripts\test.pl" and no luck.

I then put "perl test.pl" in a bat file in the directory "d:\scripts", no luck...

Does anyone have any experience run perl scripts on Vista using Task Scheduler? I would be most appreciative...

Replies are listed 'Best First'.
Re: Vista Task Scheduler
by BrowserUk (Patriarch) on Apr 12, 2008 at 06:03 UTC

    How are you scheduling your script? If you're doing via the at command, then you must remember that it will be schedule to run under the system account. You can try scheduling runas /user:domain\user ..., but I never got that to work sucessfully.

    The easiest way is to schedule it using the task scheduler (via start->Control Panel->scheduled tasks->right click->new->scheduled task (or the Vista equivalent)) where it allows you to specify the domain\user name and password. That works for me.

    Or, see the command schtasks /?

    Update: The following command worked for me. Note: You will be prompted for your password. I've wrapped the last option for posting:

    schtasks /Create /tn fred /sc once /st 07:55:00 /tr "c:\perl\bin\perl.exe \test\junko.pl

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Vista Task Scheduler
by walto (Pilgrim) on Apr 12, 2008 at 05:38 UTC
    Running your script manually worked for me. The file output.txt is in the directory from where I executed c:\perl\bin\perl.exe "d:\scripts\test.pl. I am not sure where the scheduler would place your output.txt so I advise to set a directory within your code (make sure you have proper permissions for the directory:
    use strict; chdir "c:/Users/Admin"; my $fname = "output.txt"; #open OUTPUT, ">$fname" or die "$fname: $!\n"; is more common but you +will miss the message when run by scheduler unless(open(OUTPUT,">$fname")) { exit } print OUTPUT "This worked\n"; close OUTPUT;
    You can use File::HomeDir to find your home direcory.It is in the ppm repository.
Re: Vista Task Scheduler
by Corion (Patriarch) on Apr 12, 2008 at 12:23 UTC

    Personally, I had nothing but problems when trying to schedule things with at.exe, or at least, too many. I got them to go away by using Schedule::Cron. The downside of Schedule::Cron is that it doesn't run tasks unless there is somebody logged in to the machine unless you make your "cron" into a service (with srvany.exe I guess).

Re: Vista Task Scheduler
by tachyon-II (Chaplain) on Apr 12, 2008 at 06:50 UTC

    If path is not the issue then permissions will be, either way if you were logging the error it would make debugging easier. As it happens scheduled tasks maintains a log file (Schedlgu.txt), in the c:\Windows folder. You can view the log from the Scheduled Tasks window by clicking View Log on the Advanced menu. You did look in there didn't you?

    I suspect it fails to work because your script can not find "output.txt" (note you don't need to escape . in strings). The reason we don't know this is becuase you should be doing something like:

    unless(open(OUTPUT,">$fname")) { log_error("Could not open $fname because $!\n"); exit }

    If you just exit silently you never know what happened. How you implement log_error is up to you. Writing to a file is generally easy and reliable although if you get a permission denied error on opening that how do you log that!

Re: Vista Task Scheduler
by ikegami (Patriarch) on Apr 12, 2008 at 23:27 UTC
    Assuming it's like previous version of Windows, you need to give Perl a console (/interactive using the command line interface to the scheduler) or use wperl instead of perl.

      Could you give a simple example of using at with either /interactive and perl or wperl please?

        Actually, I didn't need /interactive.

        >type c:\script.pl open(my $fh, '>>', 'c:\\script.out') or die; print $fh (scalar(localtime()), "\n"); >at 1:59 perl c:\script.pl Added a new job with job ID = 1 >at Status ID Day Time Command Line ---------------------------------------------------------------------- +--------- 1 Today 1:59 AM perl c:\script.pl >time The current time is: 1:59:36.77 Enter the new time: >at There are no entries in the list. >type c:\script.out Sun Apr 13 01:59:00 2008