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

Hi monks!
I wrote a backup script in my customer system. Because they are not familiar with Unix. I install activeperl in their workstation and write a simple snipet:
# omit parameters use strict; use warnings; use Net::Telnet; sub init{ read_par(); #telnet_login("sh /backup/auto_backup.sh > /backup/backup.log &"); telnet_login("echo 1 >>1.txt"); #just a test } init(); sub telnet_login{ my $command = shift; my $tl = Net::Telnet->new() or die "???\n"; $tl->open($remote_server); $tl->login($username,$password); my @output = $tl->cmd($command);
I open a cmd window and run it, It seems work very well. So I create a new scheduled task like below:
run: C:\perl\bin\perl D:\snippet.pl start in: C:\perl\bin and enabled check box is also ticked. #please see scheduled task for real appreance!
But it's very strange, It doesn't work totally! I can see a cmd window flash on the screen on specified time. but on unix side, 1.txt file doesn't change!
what problem on my steps? and how can I resolve this issue? because the system is not next to me, except for standard modules, we don't use crontab or other any schedule::* modules. worse of all, I am the single one that know perl in my company!
Please Help me! any answer are really appreciated!!!

I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: Help! perl in scheduled task doesn't work!
by BrowserUk (Patriarch) on Oct 18, 2007 at 10:13 UTC

    It seems likely that you have a permissions issue, but that's basically a guess.

    Add <STDIN>; to the end of the script. It may cause the cmd window to remain open and allow you to see any error message that might be produced.


    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: Help! perl in scheduled task doesn't work!
by moritz (Cardinal) on Oct 18, 2007 at 10:02 UTC
    You could try to print out %ENV in a scheduled task and compare it to one that is run on command line.

    And you should check every action that could go wrong, for example if you use open somewhere in your script (you're not showing the whole script...)

Re: Help! perl in scheduled task doesn't work!
by eyepopslikeamosquito (Archbishop) on Oct 18, 2007 at 12:10 UTC

    Why not just set up a cron job on the Unix box to do the backup? That would seem to be a simpler, more robust way to perform the backup automatically.

Re: Help! perl in scheduled task doesn't work!
by andreas1234567 (Vicar) on Oct 18, 2007 at 10:24 UTC
    I would have kept a log of some kind so that one can go back and inspect what happened later on. E.g. using Log::Log4perl and Log::Dispatch::FileRotate:
    use strict; use warnings; use Log::Log4perl; use Log::Dispatch::FileRotate; my $conf = q( log4perl.category.myapp = DEBUG, FileRotateAppender log4perl.appender.FileRotateAppender = Log::Dispatch::File +Rotate log4perl.appender.FileRotateAppender.filename = myapp.log log4perl.appender.FileRotateAppender.mode = append log4perl.appender.FileRotateAppender.size = 100000 log4perl.appender.FileRotateAppender.max = 5 log4perl.appender.FileRotateAppender.layout = PatternLayout log4perl.appender.FileRotateAppender.layout.ConversionPattern=[%p] % +d %M %F:%L:- %m%n ); Log::Log4perl::init(\$conf); my $log = Log::Log4perl::get_logger("myapp"); sub telnet_login { my $command = shift; $log->debug(qq{Got command:'$command}); my $tl = undef; if ($tl = Net::Telnet->new()) { $log->info(q{Init'd new session}); } else { $log->logdie(q{Failed to init telnet: } . $@); } } __END__
    Disclaimer: I've never used the proposed modules on Win32.
    --
    Andreas
Re: Help! perl in scheduled task doesn't work!
by LittleGreyCat (Scribe) on Oct 18, 2007 at 13:24 UTC
    One minor thing - I note you have a relative path name for '1.txt'. It is always safer to use an absolute path name just in case the script runs in an unexpected location

    Nothing succeeds like a budgie with no teeth.
Re: Help! perl in scheduled task doesn't work!
by xiaoyafeng (Deacon) on Oct 19, 2007 at 01:52 UTC
    Thank all for your answers!!
    I've tried all the ways you mentioned. like below:
    # omit parameters use strict; use warnings; use Net::Telnet; open (STDERR, ">>err.log") or die "error!$!\n"; sub init{ read_par(); #telnet_login("sh /backup/auto_backup.sh > /backup/backup.log &"); telnet_login("echo 1 >>1.txt"); #just a test } init(); my $tt = <STDIN> sub telnet_login{ my $command = shift; my $tl = Net::Telnet->new() or die "???\n"; $tl->open($remote_server); $tl->login($username,$password); my @output = $tl->cmd($command); print STDERR %ENV; }
    But it still just flashed and don't retain the cmd window! moreover, there isn't any err.log in system after running.

    It seems scheduled task just run perl command which has not any parameter, I guess.

    I don't know why, fortunately, I find a easy way to walk around. activeperl has a tool named pl2bat to translate pl to bat. I just translate my snippet into bat and modify scheduled task configuration accordingly. It works!

    Anyway, Thank monks again! and hope any gurus could figure out the reason invoke this strange issue.
    UPDATE:
    Thank andreas1234567 for pointing out typo.

    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
      moreover, there isn't any err.log in system after running.
      Your code does not compile (at least not for me):
      $ perl use strict; use warnings; open (STDERR ">>err.log") or die "error!$!\n"; __END__ Missing comma after first argument to open function at - line 4, near +"">>err.log") " Execution of - aborted due to compilation errors.
      --
      Andreas
Re: Help! perl in scheduled task doesn't work!
by Anonymous Monk on Nov 13, 2009 at 18:14 UTC
    I'm having a similar issue, and was able to solve it, strangely enough, by removing the absolute path from the perl command ('C:\Perl\bin\perl' caused a 'Could not start' status in Scheduled Tasks).
      The Task can not be run, because it doesn't find the file 'C:\Perl\perl'. You have either to specify the full file name with the ending '.exe', resulting in 'C:\Perl\perl.exe' or as you did with only 'perl', which will use the PATH Environment and make the full path from it. This solved the problem for me. Thanks.