crontab script
am 06.12.2007 19:52:16 von Manuel
Hello,
i was trying to create a script that checks if internet connection and
if it is off the perl script tries to connect again and lunch a perl
script.
it opens a txt file, get the timestamp. in $differenza i get the
difference between now and the timestamp in the txt file (in this file
i write with blog.pl the operation time timestamp ). if the difference
is more than 10000 seconds it means the connection is off so it tries
to connect again. and if the difference is more than 500 it start the
blog.pl process again. i did put this file in the crontab list:
*/1 * * * * perl /home/user/perl/crontab.pl
but the blog.pl never stars. Why???
this is the code of crontab.pl:
$ora_aggiornata=time();
open (CHECKBOOK, "timestamp.txt");
$timestamp = ;
$differenza=$ora_aggiornata-$timestamp;
print "$differenza\n";
if ($differenza > 10000){
$ifc=`ifconfig | grep P-t-P`;
if ($ifc eq "") {
`poff dsl-provider`;
`sleep 1`;
`pon dsl-provider`;
}
}
if ($differenza > 500) {
exec("perl blog.pl &");
} else {
print "no not yet\n";
}
Re: crontab script
am 06.12.2007 20:36:42 von kingskippus
On Dec 6, 1:52 pm, Manuel wrote:
> Hello,
> i was trying to create a script that checks if internet connection and
> if it is off the perl script tries to connect again and lunch a perl
> script.
> it opens a txt file, get the timestamp. in $differenza i get the
> difference between now and the timestamp in the txt file (in this file
> i write with blog.pl the operation time timestamp ). if the difference
> is more than 10000 seconds it means the connection is off so it tries
> to connect again. and if the difference is more than 500 it start the
> blog.pl process again. i did put this file in the crontab list:
> */1 * * * * perl /home/user/perl/crontab.pl
> but the blog.pl never stars. Why???
>
> this is the code of crontab.pl:
>
> $ora_aggiornata=time();
> open (CHECKBOOK, "timestamp.txt");
> $timestamp = ;
> $differenza=$ora_aggiornata-$timestamp;
> print "$differenza\n";
> if ($differenza > 10000){
> $ifc=`ifconfig | grep P-t-P`;
> if ($ifc eq "") {
> `poff dsl-provider`;
> `sleep 1`;
> `pon dsl-provider`;
> }}
>
> if ($differenza > 500) {
> exec("perl blog.pl &");} else {
>
> print "no not yet\n";
>
> }
I might be wrong, but I would guess that the crontab.pl process is
dying before the blog.pl process completes.
Have you considered using system() or backticks instead of exec()?
What happens if you put a print "in blog.pl"; statement at the
beginning of blog.pl, does it print?
Re: crontab script
am 06.12.2007 22:14:08 von glex_no-spam
Manuel wrote:
> Hello,
> i was trying to create a script that checks if internet connection and
> if it is off the perl script tries to connect again and lunch a perl
> script.
A perl script for lunch??..
> */1 * * * * perl /home/user/perl/crontab.pl
No need for "*/1".
* * * * * perl /home/user/perl/crontab.pl
Does the script run outside of cron? Debug it outside of cron first.
Do you get any of your print() output?
> $ora_aggiornata=time();
> open (CHECKBOOK, "timestamp.txt");
You're sure it's able to read it?
open (CHECKBOOK, "timestamp.txt") or die "Can't open timestamp.txt: $!";
> $timestamp = ;
> $differenza=$ora_aggiornata-$timestamp;
> print "$differenza\n";
> if ($differenza > 10000){
> $ifc=`ifconfig | grep P-t-P`;
> if ($ifc eq "") {
> `poff dsl-provider`;
> `sleep 1`;
No need to run a shell for sleep.
sleep 1;
> `pon dsl-provider`;
> }
> }
> if ($differenza > 500) {
print "blog.pl exists\n" if -f "blog.pl"; # to ensure it's there.
Really though, if all it does is update timestamp.txt, just
put that code here.
> exec("perl blog.pl &");
> } else {
> print "no not yet\n";
>
> }
Re: crontab script
am 06.12.2007 22:17:21 von Glenn Jackman
At 2007-12-06 01:52PM, "Manuel" wrote:
[...]
> */1 * * * * perl /home/user/perl/crontab.pl
Almost invariably, problems with cron scripts boild down to differences
between cron's environment and your shell. Also, you're doing no error
checking, so cron and perl aren't telling you what the problem is.
> this is the code of crontab.pl:
[...]
> open (CHECKBOOK, "timestamp.txt");
What's the current directory when running this script inside cron?
Does this file exist there?
open (CHECKBOOK, "timestamp.txt") or do {
require Cwd;
my $cwd = cwd();
die "can't read $cwd/timestamp.txt: $!\n";
}
[...]
> `sleep 1`;
Do you know Perl has a function called sleep?
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: crontab script
am 06.12.2007 22:18:55 von Ben Morrow
Quoth Manuel :
> i was trying to create a script that checks if internet connection and
You may find the module LWP::Online useful for this.
> if it is off the perl script tries to connect again and lunch a perl
> script.
> it opens a txt file, get the timestamp. in $differenza i get the
> difference between now and the timestamp in the txt file (in this file
> i write with blog.pl the operation time timestamp ). if the difference
> is more than 10000 seconds it means the connection is off so it tries
> to connect again. and if the difference is more than 500 it start the
> blog.pl process again. i did put this file in the crontab list:
> */1 * * * * perl /home/user/perl/crontab.pl
> but the blog.pl never stars. Why???
>
>
> this is the code of crontab.pl:
>
Where is
#!/usr/bin/perl
use warnings;
use strict;
?
> $ora_aggiornata=time();
> open (CHECKBOOK, "timestamp.txt");
Always check the return value of open.
Use lexical filehandles.
Use three-arg open.
open my $CHECKBOOK, '<', 'timestamp.txt'
or die "can't open timestamp.txt: $!";
> $timestamp = ;
> $differenza=$ora_aggiornata-$timestamp;
> print "$differenza\n";
> if ($differenza > 10000){
> $ifc=`ifconfig | grep P-t-P`;
> if ($ifc eq "") {
> `poff dsl-provider`;
Don't use backticks just to run a command. That's what system is for.
system 'poff dsl-provider';
or, clearer and safer,
system 'poff', 'dsl-provider';
I wouldn't use an external grep at all, just read the whole lot in and
check it in Perl:
local $/ = undef;
if (`ifconfig` =~ /P-t-P/) {
but thats something of a matter of taste in this instance. If ifconfig
were going to produce a large amount of output (several hundred MB or
more) it might be better to filter it with grep first, but that isn't
the case here.
> `sleep 1`;
Perl has a sleep function built in. You don't need an external command.
sleep 1;
> `pon dsl-provider`;
> }
> }
> if ($differenza > 500) {
> exec("perl blog.pl &");
I'm not sure what you intended to acheive with this... Perl is not
shell. This statement will replace your perl process with a shell, which
will run blog.pl in the background and wait for it, then exit. That's a
whole process sitting there doing nothing for no reason.
exec 'perl blog.pl';
or, for the same reasons as above,
exec 'perl', 'blog.pl';
or, more portably,
exec $^X, 'blog.pl';
$^X is a special variable which names the currently running copy of
perl. exec can fail. You need to check for this, particularly as I
suspect your problem is your program ends up running in the wrong
directory, so it can't find blog.pl.
exec $^X, 'blog.pl'
or die "exec or blog.pl failed: $!";
> } else {
> print "no not yet\n";
>
> }
Ben
Re: crontab script
am 06.12.2007 22:38:03 von krahnj
Glenn Jackman wrote:
>
> At 2007-12-06 01:52PM, "Manuel" wrote:
> [...]
> > */1 * * * * perl /home/user/perl/crontab.pl
>
> Almost invariably, problems with cron scripts boild down to differences
> between cron's environment and your shell. Also, you're doing no error
> checking, so cron and perl aren't telling you what the problem is.
>
> > this is the code of crontab.pl:
> [...]
> > open (CHECKBOOK, "timestamp.txt");
>
> What's the current directory when running this script inside cron?
> Does this file exist there?
>
> open (CHECKBOOK, "timestamp.txt") or do {
> require Cwd;
> my $cwd = cwd();
You are using require so that should be:
my $cwd = Cwd::cwd();
John
--
use Perl;
program
fulfillment
Re: crontab script
am 06.12.2007 22:56:54 von krahnj
Manuel wrote:
>
> i was trying to create a script that checks if internet connection and
> if it is off the perl script tries to connect again and lunch a perl
> script.
> it opens a txt file, get the timestamp. in $differenza i get the
> difference between now and the timestamp in the txt file (in this file
> i write with blog.pl the operation time timestamp ). if the difference
> is more than 10000 seconds it means the connection is off so it tries
> to connect again. and if the difference is more than 500 it start the
> blog.pl process again. i did put this file in the crontab list:
> */1 * * * * perl /home/user/perl/crontab.pl
> but the blog.pl never stars. Why???
>
> this is the code of crontab.pl:
use warnings;
use strict;
> $ora_aggiornata=time();
> open (CHECKBOOK, "timestamp.txt");
You should *always* verify that the file opened correctly:
open CHECKBOOK, '<', 'timestamp.txt' or die "Cannot open 'timestamp.txt'
$!";
> $timestamp = ;
chomp( my $timestamp = );
> $differenza=$ora_aggiornata-$timestamp;
> print "$differenza\n";
> if ($differenza > 10000){
> $ifc=`ifconfig | grep P-t-P`;
> if ($ifc eq "") {
Since you don't chomp() the output the variable $ifc will never equal
"".
> `poff dsl-provider`;
> `sleep 1`;
> `pon dsl-provider`;
You should use the complete absolute path to any external programs. You
shouldn't use back-ticks in a void context:
0 == system '/somewhere/bin/poff', 'dsl-provider'
or die "system '/somewhere/bin/poff' failed: $?"
sleep 1;
0 == system '/somewhere/bin/pon', 'dsl-provider'
or die "system '/somewhere/bin/pon' failed: $?"
> }
> }
> if ($differenza > 500) {
> exec("perl blog.pl &");
If you want to run that in the background you can use fork():
defined( $child = fork ) and $child
or exec '/usr/bin/perl', '/somewhere/blog.pl';
> } else {
> print "no not yet\n";
>
> }
John
--
use Perl;
program
fulfillment
Re: crontab script
am 07.12.2007 00:50:26 von Manuel
sorry but this
*/1 * * * * perl /home/user/perl/crontab.pl
means that it starts crontab.pl every minute right?
Re: crontab script
am 07.12.2007 01:36:48 von Keith Keller
On 2007-12-06, Manuel wrote:
> sorry but this
> */1 * * * * perl /home/user/perl/crontab.pl
>
> means that it starts crontab.pl every minute right?
That's a cron question, not a perl question. (You're right, but it's
better as
* * * * * .....
)
--keith
--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
Re: crontab script
am 07.12.2007 04:07:29 von Ben Morrow
Quoth Manuel :
> sorry but this
> */1 * * * * perl /home/user/perl/crontab.pl
>
> means that it starts crontab.pl every minute right?
No idea, read your crontab manual. The syntax is (slightly) different on
different systems. Also, at least on my system, cron only wakes up once
a minute, so it's likely you won't get terribly regular scheduling. It's
better for short waits like this to write a process that runs the whole
time, with something like
while (1) {
sleep 60;
# or something better, that calculates the appropriate wait
# do some stuff
}
around the main work.
Ben