Gossamer Forum
Home : General : Perl Programming :

Updating a URL without using a Cron Job

Quote Reply
Updating a URL without using a Cron Job
Hi,

I want to know if anyone has knows of a CGI script or a web bot that will update a URL link every 10 mins or so.

Martin
Quote Reply
Re: Updating a URL without using a Cron Job In reply to
The only way that you're going to be able to do this is with a cron job unless you can set up another CGI that you're running (like a counter or a search program) to "trip" your URL and update it.
Quote Reply
Re: Updating a URL without using a Cron Job In reply to
Any ideas of how this can be done?
Quote Reply
Re: Updating a URL without using a Cron Job In reply to
Not really, as you are keeping the process in memory 24 hours a day, 7 days a week. Your ISP is sure to remove the program (any decent ISP should have some sort of process management that will kill processes that last too long, especially a webhost!).

Also, you will be out of luck if the server reboots. You won't know it, and your program won't be running.

I'd ask your ISP for cron access, or if they can put this one program in cron, otherwise switch ISP's if you really need it.

Cheers,

Alex
Quote Reply
Re: Updating a URL without using a Cron Job In reply to
Martin,

There is a way to do this, but you might face the risk of your server administrator shutting down this program, and you might find it hard to shut down this program once you started it. For the purpose of this example, I will use the term "mini-cron" to relate to the program that will emulate the cron job, it is not actually related to cron in any way.

You will need basically four programs to do this, two will allow you to start and stop the "mini-cron", one will be the "mini-cron" itself, and the third will be the program you want "mini-cron" to run.

In the first program, we will call it cronstart:
Code:
#!/usr/bin/perl
my $job = "/path/to/mini-cron";

print "Content-type:text/html\n\n";
print "<h1>Starting your job</h1>";
print `$job`;
Note on print `$job`; we use left quote, which are typically under the ~ symbol on a PC keyboard.

As for the kill procedure, this will vary ALOT depending on your OS. I won't go into the kill procedure at this time, so if you think you will need this, let me know and I will post it. Next, "mini-cron":

Code:
#!/usr/bin/perl
# Welcome to mini-cron
my $job = "/path/to/program_for_cron";
my $delay = 600; # sleep delay in seconds 600 = 10 minutes
my $log = 0; # Turn logging of output on (1) or off (0)
my $log_file = "log.txt";

while (1) {
my $error = `$job`; # Error is actually whatever is outputted by the program
&log_errors($error) if ($log_on); # IF you want to
sleep $delay;
}

sub log_errors {
my $error = @_;
open (FILE, ">> $log_file);
print FILE "$error";
close FILE;
}

You could also change this code so that instead of while (1) you used a conditional statement to run a specific number of times, or however you wanted to do this. As you can see, this program runs an infinite loop. This can be very dangerous, and most likely, the server admin will shut it down if it sees this process running continually. As a suggestion, you might try something a bit different:

Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
print `man crontab;crontab -l`;
What this will do is see if crontab is installed on your system, and show you how to use it. If it is installed, I would strongly suggest manipulating the shell though a perl or csh cgi rather than using the methods described above. Its much less overhead for everyone involved. I can post methods to handle this if you wish.

Hope this helps,


------------------
Fred Hirsch
Web Consultant & Programmer
Quote Reply
Re: Updating a URL without using a Cron Job In reply to
Hiya fhirsch! If I just wanted to do this at 12:00 am everyday would it bother the server that much? Because I don't have cron Frown and i tried that list bit of code at the bottom and it didn't work. Let me know if my case is reasonable enough Smile



------------------
Joseph Gruber
http://linksgalore.virtualave.net

~To be or not to be...which is better?~
Quote Reply
Re: Updating a URL without using a Cron Job In reply to
Thanks Alex for that info...i don't really want to do it then if it will burden my server. But I have a question then. Is it possible that I can start this program up at say 10:00 PM EST by calling the program myself and then having the program kill itself (using the other code fhirsch said he had) right around 12:30 am EST. This would allow me to put in a time of 2hrs to update and it would update right at midnight and then would kill itself so it doesn't burden the server. If this is possible let me know how to implement it! Thanks Smile
Quote Reply
Re: Updating a URL without using a Cron Job In reply to
Joseph,

If you are going to run the program manually, don't bother with this code example, simply find the program you want to run, and run it. Doesn't get any easier than that. I too would not suggest using a program like the "mini-cron" I described. Most likely your server has a process running which checks for stuck or idling jobs and kills them automatically. This defeats the purpose of having such a task.

My suggestion for you would be to write a crontab, upload it to your server and request the server admin enable your crontab. If you don't have support for your hosting, maybe you should look elsewhere... many servers for hosting are very affordable and allow use of the cron. It simply avoids users trying to circumvent it.

Fred

------------------
Fred Hirsch
Web Consultant & Programmer