crontab script

M

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 = <CHECKBOOK>;
$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";

}
 
T

TonyV

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 = <CHECKBOOK>;
$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?
 
J

J. Gleixner

Manuel said:
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 = <CHECKBOOK>;
$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.
 
B

Ben Morrow

Quoth Manuel said:
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 = <CHECKBOOK>;
$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
 
J

John W. Krahn

Glenn said:
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
 
J

John W. Krahn

Manuel said:
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 = <CHECKBOOK>;

$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
 
M

Manuel

sorry but this
*/1 * * * * perl /home/user/perl/crontab.pl

means that it starts crontab.pl every minute right?
 
B

Ben Morrow

Quoth Manuel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top