Re: Using perl as a cron script?
by d4vis (Chaplain) on Jun 28, 2002 at 18:54 UTC
|
| [reply] |
Re: Using perl as a cron script?
by grinder (Bishop) on Jun 28, 2002 at 18:59 UTC
|
You could just sleep for 10800 seconds at the end of an infinite loop. If demands are placed on the system while your script is sleeping, it will be paged out, so no harm there. You should just take care to close all your file or socket descriptors before sleeping.
Also, you could set it up to listen for SIGINT signals, and exit gracefully upon reception of such a signal.
print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
| [reply] |
|
Remember to take into account the execution time of the code though...I don't know how heavy the functionality is, but if it takes a while to execute; your start times will gradually become offset. For example, if your processing takes 2 minutes and you sleep for 3 hours starting at noon...
12:00 execute 1
15:02 execute 2
18:04 execute 3
etc...
This may be acceptable but if your caveat is that your script starts every three hours this method is probably not what you're looking for. Perhaps a shell script Dmn wrapper that executes your script in the background without waiting for it to complete to start the sleep cycle would be more effective. -Jason | [reply] [d/l] |
|
if it takes a while to execute; your start times will gradually become offset. For example, if your processing takes 2 minutes and you sleep for 3 hours starting at noon...
If start time drift is a problem, you can stay in sync by simply reading the current time and then calculating how long you want to sleep, in order to wake up at the desired time.
The following snippet does just that. My internal mail server (running Bloatus Notes) has to be shut down for backups. Rather than sending mail (created by other night-time batch jobs) to the external server, I just sleep until a point in time when I know it will be available.
use Time::Local;
# sleep a while until the mail server comes back up
my @later_bits = (localtime)[0..5];
@later_bits[2,1] = (8, 15); # 08:15
my $later = timelocal( @later_bits );
my $duration = $later > $now ? $later - $now : 0;
sleep $duration;
Modifying this to allow for sleeping up until midday modulo 3 is left as an exercise to the reader. Note that this technique lets you do cool stuff like "run every 8 minutes", which is not possible with standard cron.
update: while I do use Paul Vixie's cron when I can, it's not always present. On Solaris, for instance, you can only specify comma-separated values (at least on the O/S version I use at the moment).
I would be hesitant of of using */8 in a crontab entry, because 60 is not evenly divisible by 8. I've never bothered to see what it would do, but I have a sneaking suspicion that it would fire off your script at 0, 8, 16 ... 48 and 56 minutes of the hour. Thus, at the end of the hour the script will have two runs 4 minutes apart.
print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u' | [reply] [d/l] |
|
|
Re: Using perl as a cron script?
by jsegal (Friar) on Jun 28, 2002 at 18:56 UTC
|
You could always use sleep to simulate what you need. E.G.
while (1) {
# do stuff
sleep(60*60*3); # sleep for three hours
}
This does use up memory (and any open files, etc) while the script is sleeping, but doesn't use up CPU. You could easily have the "do stuff" section simply call another script, so you have less to worry about in terms of keeping resources booked in your main script.
--JAS | [reply] [d/l] |
Re: Using perl as a cron script?
by sparkyichi (Deacon) on Jun 28, 2002 at 18:51 UTC
|
I had asked Merlyn about that a few days ago and he had made an off hand suggestion that you change the shell for cron from vi to Perl. I could not do it in my case but you might be able to do something like that.
As far as adding and removing entries you will need to have access (A login) to the cron you want to edit. Generally every login account has a crontab that can be listed by crontab -l or edited by crontab -e. There is a way for the administrator (root) to add the crontabs to a group that you are a member of then you could use Perl to edit the flat files and then the admin would need to do a crontab -e then a write to update the crontab. There is no easy way to do it. I am currently developing a Tk interface that will allow you to edit any cron but you will still need to know the password for the account you want to work with.
Sparky
FMTEYEWTK | [reply] [d/l] [select] |
|
Sorry, I did not really answer the question. I would suggest that you set you script in a crontab. You could also have your program run and wait for localtime to equal the time you are waiting for. Another way if it does not need to be precise is you can do sleeps and check for the time if it is in your window run task if not go back to sleep.
Sparky
FMTEYEWTK
| [reply] |
Re: Using perl as a cron script?
by DamnDirtyApe (Curate) on Jun 28, 2002 at 21:05 UTC
|
If Schedule::Cron won't do what you're looking for (I suspect it probably will, but I've never used it), maybe you could use something like this:
#! /usr/bin/perl
use strict;
use warnings ;
use diagnostics ;
sub schedule (&@)
{
my ( $condition, $exec_stmt ) = @_ ;
if ( &{$condition} )
{
my $pid = fork() ;
if ( !$pid )
{
exec( $exec_stmt ) ;
}
}
}
while ( 1 )
{
my ( $min, $hour, $mday, $mon, $year, $wday ) = ( localtime )[1..6
+] ;
$year += 1900 ;
# Run everyday at 12:00
schedule { $hour == 12 && $min == 00 } '/usr/bin/some/prog' ;
# Run every three hours
schedule { $hour % 3 == 0 } '/usr/sbin/some/other/prog' ;
# Every Tuesday, plus the 1st & 15th of every month at midnight.
schedule {
( $hour == 0 && $min == 0 ) &&
( ( $wday == 2 ) || ( $mday == 1 || $mday == 15 ) )
} '/usr/local/bin/yet/another/prog' ;
sleep 60 ;
}
_______________
D
a
m
n
D
i
r
t
y
A
p
e
Home Node
|
Email
| [reply] [d/l] |
|
Thanks DamnDirtyApe, very useful script. I have a question, if the program sleep for 60 seconds, what prevents to run the same progran more than one time (since "hour%3 ==0" conditions is testing every 60 seconds ) ?
| [reply] |
|
This post is almost 20 years old and DamnDirtyApe hasn't been here for 12 years.
But you are right, you need to check the minute too.
And you should be aware that this is a hack, rounding errors could easily lead to skipped jobs.
| [reply] |
Re: Using perl as a cron script?
by robobunny (Friar) on Jun 28, 2002 at 18:58 UTC
|
you could probably make use of the Schedule::Cron module on CPAN. | [reply] |
Re: Using perl as a cron script?
by belg4mit (Prior) on Jun 29, 2002 at 19:15 UTC
|
a cron; in perl
This is somebody else's machine,
they might not like persistent processes.
On same machines it is possible to RLIMIT
the total CPU time a process may take...
--
perl -pew "s/\b;([mnst])/'$1/g"
| [reply] |
Re: Using perl as a cron script?
by mt2k (Hermit) on Jul 06, 2002 at 18:59 UTC
|
There is perhaps just one slight problem with using Perl for this at all. If you do not have access to cron, I take it that you do not have root access. In this case, I will also assume that you have an account on someone else's remote machine.
With these assumptions, this means that you would have to keep a telnet or ssh session open to the remote server in order to keep the Perl script running. As many people will find, telnet and/or ssh are not the best ways to keep a persistent connection: especially telnet sessions have a tendency to hang at unexpected moments. In this case, if it happens to hang while the script is running, the execution of the script would be terminated, resulting in no crontab running.
Just my 10 cents (yes, the price of opinions has been increased by 8 cents) | [reply] |
|
| [reply] [d/l] |