Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running Hourly

Status
Not open for further replies.

blundellp

Technical User
Apr 6, 2005
25
GB
Can anyone tell me how to make my program run hourly, i am useing windows so i cannot use the 'cron' program. Shall i use an outside windows program or is there a hourly loop command built into pearl? I don't really want to use another cpan pearl module unless its absolutely neccessary. This is the code i am going to run hourly any way if it helps:--

use Win32::process::Info; #Requests to use the Win32 Mod
use Date::EzDate; #Requests to use the date mod
use Date::WeekOfYear;
use warnings;
my $printinfo = Win32::process::Info->new(); #Gets the system process info
my @info = $printinfo->GetProcInfo(); #Stored to an array
my $currentdate = Date::EzDate->new(); #Loads the current Date an Time
my $weekNo = WeekOfYear();

$file = "$weekNo $currentdate->{'{hour}{_%d_%h}'}.txt"; #Names file with week no hour date
$logfile = ">/perl/ass/$file"; #Open file for writing to
open(LOG, $logfile);
print LOG "$weekNo \n";
print LOG $currentdate->{'{day of month}/{month number base 1}/{year two digits}'}, "\n" ,
$currentdate->{'clocktime'}, "\n \n"; #Prints the current date to *.txt
for $pid (@info) #For all information in the array
{
print LOG $pid->{"Name"}."\n"; #Print it to the log file
}
print "Log Successfully created for: $currentdate\n";
close(LOG);
 
with windows you schedule "tasks".

My Computer >> Scheduled Tasks


Or refer to the help files to know how with your version of windows or ask your tech support.

- Kevin, perl coder unexceptional!
 
Ah yes i have found win32::TaskScheduler to run this program for me from within perl.


However when i normally install my modules I do it myself putting the folders where there supposed to go. The helpfile says to run makefile.pl , i have done this but it will not install this module? I am useing windows and i think the install tells you how to do it in linux.
Any help much appreciated
 
Yeah, the module i am showing you uses the windows task scheduler but arranges the task through my perl program. I just can't seem to install the module correctly.
 
you should be using ppm to install modules on windows, that is if you are using activestate perl. Open a DOS window, get to the C:\> prompt and type ppm (hit enter) then type help (hit enter).

- Kevin, perl coder unexceptional!
 
Why are you wanting to use the perl script to schedule the task? Can't ou simply use the windows task scheduler to set up the job?

- Kevin, perl coder unexceptional!
 
I can see the benefit, if the task schedule were to vary but for one which runs each hour, I would have thought the additional interface just complicates a simple task.

Keith
 
But if i want to cross platform my application i would have to invoke the task schedular from within the perl code
 
it won't be cross platform anyway, task scheduler is only for windows, it will not work on Nix boxes or other operating systems.

The task scheduler can also be limited to administrative permisson only.

Running automated tasks on webservers is generally not something just anyone is allowed to do.

- Kevin, perl coder unexceptional!
 
I know but i can write further program to if statement for different OS's to choose task schedular or cron etc, i am total admin so permissions is not a problem. So is there a way to install if the PPM website fails? Is there another mirror or something i'm not suite sure
 
The only place I find that module is on CPAN. If worse comes to worse youcan donwloadthe zip file:

ftp://ftp.perl.org/pub/CPAN/modules/by-module/Win32/Win32-TaskScheduler2.0.3.zip

unzip it and copy the module file into the Win32 folder in your perl/site/lib folder or wherever you put your modules.




- Kevin, perl coder unexceptional!
 
First of all I have to advice you to always use strict.
In your open statement always indicate if you opening the file for reading or writting and not in a variable before the open statement.
And finally, put your comments in a separate line from the code cause it makes the script not easy to read.

And as far as your every hour scheduling concerns, why don't you put the whole script in a while loop and make it sleep for an hour like this.

The script doesn't cost anything on cpu time or power while it sleeps.

And you can also put the script in the Startup dir so it will run on boot and stay on as a service.(kind of)
Code:
use strict;
use warnings;

# Requests to use the Win32 Mod
use Win32::Process::Info;

# Requests to use the date mod
use Date::EzDate;  
use Date::WeekOfYear;

while(1) {
    # Gets the system process info
    my $printinfo = Win32::Process::Info->new();

    # Stored to an array
    my @info = $printinfo->GetProcInfo();   
    
    # Loads the current Date an Time
    my $currentdate = Date::EzDate->new();
    my $weekNo = WeekOfYear();

    # Names file with week no hour date
    my $file = "$weekNo $currentdate->{'{hour}{_%d_%h}'}.txt";
    
    [red]# Dont forget it is windows and you need to specify              
    # backslases mopst of the times.
    my $logfile = 'c:\perl\ass\'.$file;[/red]
    
    # Open file for writing to
    open(LOG, [red]">[/red]$logfile[red]"[/red]);
    print LOG "$weekNo \n";
    # Prints the current date to *.txt
    print LOG $currentdate->{'{day of month}/{month number base 1}/{year two digits}'}, "\n" , $currentdate->{'clocktime'}, "\n \n";

    # For all information in the array
    foreach my $pid (@info) {
        # Print it to the log file
        print LOG $pid->{"Name"}."\n";
    }
    print "Log Successfully created for: $currentdate\n";
    close LOG;
    
    sleep 3600;
}


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Thanks mate i've been lookin so long how to run hourly and all i needed was

while(1)
{

sleep 3600;
}

thanks !! x x
 
That seems like it would work, but what it would actually do is run the next instance an hour after the current instance ended.

eg., if your process took 1 minute to run, and you started at 8am, it would run at 9:01am, 10:02am, 11:03am, 12:04pm...etc

You could calculate how long the script ran for, and deduct that from the 3600 at runtime, or calculate the time difference between now and the next time you want to run it, and set that in the sleep command.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top