gnarlyhat

I know it's not polite to ask for support on a script which is not supported but I would really appreciate it if any of you gurus can help me sort out the problem. Here is a script written by Eli ... yeah bluehat.

This is a cron script for which you throw a bunch of crontab lines and it makes sure that they won't overlap and put strain on the server. Here's the code.

#!/usr/bin/

perl

 
print "Content-type: text/html ";
use CGI qw/:cgi-lib/;
use CGI::Carp qw(fatalsToBrowser);
%FORM=Vars();

#Instructions
##################
#Set this to the server path to the crons.txt
$pathtocrontxt="/home/username/public_html/cgi-bin/crons.txt";
# In the crons.txt put the first line as 0 and then put each cronjob on a seperate line below
# set the chmod permissions to crons.txt to 777 and cron.cgi to 755
#####################################
print "Running Crons... ";
open(INF,"$pathtocrontxt"Applause or print "couldnt open file";
@commands=<INF>;
close(INF);
foreach $line(@commands){
chomp($line);
if($line == 0){
print "not working ";
@commands[0]="1 ";
open(OUTF,">$pathtocrontxt");
foreach $command(@commands){
print OUTF "$command";
}
close(OUTF);
}
elsif($line == 1){
print "working";
exit;

}
else {
print "Executing: $line ";
$output=system("$line");
print "$output ";
}

}
@commands[0]="0 ";
open(OUTF,">$pathtocrontxt");
foreach $command(@commands){
print OUTF "$command";
}
close(OUTF);
print " Done! ";


Here's the contents of crontab.txt

0
ping www.google.com
ping www.google.com.hk
ping www.google.com.au
ping www.google.com.sg
ping www.google.com.my


and here's the error I got from crontab email.

quote
Content-type: text/html

Running Crons...
not working
not working


Done!


From my limited knowledge, I can tell that it checks for 0 on the crons.txt first line and if true print "Not working" and change it to 1 and prints out the commands and executes it. May I know why it's not working? TIA Applause

dirk

Just had a look at the script.

It's a CGI script: So isn't the input from the GUI missing if you execute it
on the command line?

The hash %FORM which is defined here is never used:
%FORM=Vars();

Dirk

gnarlyhat

I would have not idea as the script is supposed to be called by crontab and it processes all the commands in crons.txt

Would it help if I were to remove it?

dirk

I have tweaked the script a little bit. It's running now.

The crucial modification was:

    if ( $line == 0 ) {
->
    if ( $line eq '0' ) {

The first condition was true for every line.

Here was another bug:

        @commands[0] = "1";
->
        $commands[0] = "1";

And I have changed it to:

        $commands[0] = 1;

It makes no sense to quote and interpolate 1.

Here is the modified code:


#!/usr/bin/

perl

 

# Instructions
##################
#Set this to the server path to the crons.txt
$pathtocrontxt = '/home/username/public_html/cgi-bin/crons.txt';

# In the crons.txt put the first line as 0 and then put each cronjob on a seperate line below
# set the chmod permissions to crons.txt to 777 and cron.cgi to 755
#####################################

print "Running Crons... ";
open( INF, $pathtocrontxt ) or print "couldnt open file ";
@commands = <INF>;
close(INF);

foreach $line (@commands) {
    chomp $line;
    if ( $line eq '0' ) {
        print "not working ";
        $commands[0] = 1;
        open( OUTF, ">$pathtocrontxt" );
        foreach $command (@commands) {
            print OUTF "$command ";
        }
        close(OUTF);
    }
    elsif ( $line eq '1' ) {
        print "working ";
        exit;
    }
    else {
        print "Executing: $line ";
        $output = system $line;
        print "$output ";
    }
}

$commands[0] = 0;
open( OUTF, ">$pathtocrontxt" );
foreach $command (@commands) {
    print OUTF "$command ";
}
close(OUTF);

print " Done! ";

exit;



gnarlyhat

Thank you very much Dirk. I'm testing it now.

gnarlyhat

Dirk,

Thank you so much  Applause

I was thinking it didn't work earlier but then realized that ping www.google.com will just keep on pinging non stop. I changed the commands to ping www.google.com -c 5 and it spat out the results right to my email. Thanks once again. This will save me a lot of time adding crons Applause

dirk

Glad to hear it's working on your system also.

Yes, pinging on

Linux

  never stops...

I have tested it on my local Windows PC where
it stops after 3 pings. Hence I haven't noticed
that this generates another problem if you run
the script on a

Linux

  system.

Dirk


Perkiset's Place Home   Politics @ Perkiset's