Jump to content

Cron jobs.......


unplugged_web

Recommended Posts

I have some software that requires a Cron Job, but my host has just told me they don't support it and that I will have to control it with script. I have two files, one needs to be run every minute while the other needs to be executed every day at midnight. Is it actually possible to do this and how?Thanks

Link to comment
Share on other sites

Not with PHP. PHP scripts only run when you visit a site, if nobody is visiting then it won't work. You'd need to own the server and configure it to do that (seeing as you're on a host they won't let you).The only thing you can really do is leave the page open in your browser all day and have an AJAX script that sends a request based on what time it is.

Link to comment
Share on other sites

Not with PHP. PHP scripts only run when you visit a site, if nobody is visiting then it won't work. You'd need to own the server and configure it to do that (seeing as you're on a host they won't let you).The only thing you can really do is leave the page open in your browser all day and have an AJAX script that sends a request based on what time it is.
Sorry to appear stupid, but how would I do that?
Link to comment
Share on other sites

If the "program" that runs every minute is itself written in PHP and is accessed thanks to your site's visitors, you may add some additional PHP in it that would execute the other PHP file at certain time intervals.The way this works is that with PHP's time() function, you can see the current time as a timestamp, and compare the value there with the value of a certain other file (a text file maybe).To put in in a little code, if your file that's running every single minute contains:

<?php//code that runs every minute?>

You can extend it to include:

<?php/* The interval at which the second script will run in seconds. The value used for this sample is generated by hours (24) multiplied by minutes (60) multiplied by seconds (60), resolving to 24 hours sharp. */$interval = 24 * 60 * 60;//A text file that will hold the timestamp of the last time the job was executed.//NOTE: It will contain that value regardless of whether the job failed or not.$logFile = 'lastWrite.txt';/*If the file doesn't exist (i.e. if it's the first time you run the script, or have deleted the log file for whatever reason), create it with a timestamp as of 24 hours ago, so that you can do the second job at this first run. */if (!is_file($logFile)) {	file_put_contents($logFile, time() - $interval);}//See if it's time to do the jobif (time() - $interval >= (int) file_get_contents($logFile)) {	//Do the job if it is. Because you execute the PHP file over HTTP, this means the actual PHP gets executed.	file_get_contents('http://yoursite.com/path/to/second/php/file.php');	//Put the current time in the log file now that we just did the job.	file_put_contents($logFile, time());}//code that runs every minute?>

Of course this is very flawed, but it's close enough to what you want, it's simple, and it will work (you are on a PHP5 host now, right?).The flaws of this are:1. The check is performed every time the first script is accessed i.e. every minute.2. It requires the first script to be manually visited every minute. If you have any visitors to your site, you shouldn't worry about that.3. It doesn't get executed until the next visit to the first script after the interval has passed. Since this time itself may vary, you may end up in delaying the second job for a few minutes or hours, depending on your site's traffic. The more visitors you have, the more accurate this will be.

Link to comment
Share on other sites

Thanks for the code. I added it to the file and the lastWrite.txt file was generated, but when I loaded the php file I got an error saying:

Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/fhlinux159/d/dateorchat.co.uk/user/htdocs/_cron/hide.php on line 17Warning: file_get_contents(http://www.dateorchat.co.uk/_cron/gold.php) [function.file-get-contents]: failed to open stream: Success in /home/fhlinux159/d/dateorchat.co.uk/user/htdocs/_cron/hide.php on line 17
Is that anything to worry about as the txt file was generated and 24 hours later updated?Thanks
Link to comment
Share on other sites

Thanks for the code. I added it to the file and the lastWrite.txt file was generated, but when I loaded the php file I got an error saying:Is that anything to worry about as the txt file was generated and 24 hours later updated?Thanks
The question is if the job was really performed.As the note says:
//NOTE: It will contain that value regardless of whether the job failed or not.

Oh well... here's a fixed version that will only update the text file if the job is successful. It will otherwise keep doing the job until it gets properly executed.Replace:

	//Do the job if it is. Because you execute the PHP file over HTTP, this means the actual PHP gets executed.	file_get_contents('http://yoursite.com/path/to/second/php/file.php');	//Put the current time in the log file now that we just did the job.	file_put_contents($logFile, time());

with

	//Do the job if it is. Because you execute the PHP file over HTTP, this means the actual PHP gets executed.	if (!is_bool(file_get_contents('http://www.dateorchat.co.uk/_cron/gold.php'))) {		//Put the current time in the log file now that we just did the job.		file_put_contents($logFile, time());	}

but that will be more damaging than what you currently have, because as soon as the cron job starts failing, it will attempt to be redone every minute until it gets done.If the situation starts to get messy, and I believe it will, ask your host if they have compiled PHP with streams support, and if not, ask if you have the cURL extension available. Show them the code, and tell them the basic idea - that you want to initiate a PHP page as if it was executed by a browser accessing the page.

Link to comment
Share on other sites

I checked to see if the host had a cURL extension and they have, it says:I guess that doesn't matter know though because the lastWrite file is being updated.Thanks for your help.
Again, the question is if the second job was executed, not if the file was updated or not.If you're sure it's all OK, let it stay. Otherwise, at least we know you could use cURL to work around it.
Link to comment
Share on other sites

Again, the question is if the second job was executed, not if the file was updated or not.If you're sure it's all OK, let it stay. Otherwise, at least we know you could use cURL to work around it.
I think it's okay. At the moment it's just a working progress site that will hopefully go live next month so I should know then if the code is being executed.Thanks fir your help
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...