The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 16, 2019, 09:39:54 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: What could be the problem?  (Read 5049 times)
gnarlyhat
Journeyman
***
Offline Offline

Posts: 51



View Profile WWW
« on: November 05, 2007, 02:07:33 AM »

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.

Code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
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...\n";
open(INF,"$pathtocrontxt") or print "couldnt open file";
@commands=<INF>;
close(INF);
foreach $line(@commands){
chomp($line);
if($line == 0){
print "not working\n";
@commands[0]="1\n";
open(OUTF,">$pathtocrontxt");
foreach $command(@commands){
print OUTF "$command";
}
close(OUTF);
}
elsif($line == 1){
print "working";
exit;

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

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

Here's the contents of crontab.txt

Code:
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 Smiley
Logged
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #1 on: November 05, 2007, 01:44:50 PM »

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
Logged
gnarlyhat
Journeyman
***
Offline Offline

Posts: 51



View Profile WWW
« Reply #2 on: November 05, 2007, 06:20:06 PM »

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?
Logged
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #3 on: November 05, 2007, 08:30:25 PM »

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:

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...\n";
open( INF, $pathtocrontxt ) or print "couldnt open file\n";
@commands = <INF>;
close(INF);

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

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

print "\n\nDone!\n";

exit;


Logged
gnarlyhat
Journeyman
***
Offline Offline

Posts: 51



View Profile WWW
« Reply #4 on: November 05, 2007, 11:39:13 PM »

Thank you very much Dirk. I'm testing it now.
Logged
gnarlyhat
Journeyman
***
Offline Offline

Posts: 51



View Profile WWW
« Reply #5 on: November 05, 2007, 11:52:09 PM »

Dirk,

Thank you so much  Praise

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 Smiley

Logged
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #6 on: November 06, 2007, 05:09:40 PM »

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
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!