Long running Cronjob php script stops without error - php

first i know this looks like a duplicate, but i tried a lot of things suggested in asked in nearly identical questions.
I have a website which i dont host on my own, but by a hoster. I need to update my data regulary. My aproach is to let a cronjob call a php on my webspace which then updates the data which takes around 5-6 minutes at the moment.
When i open the php myself in my browser in works completely fine. (of course blank for the processing time, but it runs fully and updates all data.)
But when i let the cronjob do it (via cron-job.org) it stops after around 2 min 30 seconds without any error given. (i log the process via echo and logging to a file)
I dont have direct access to the php.ini to modify the variables in there so i tried the following:
ignore_user_abort(true);
ignore_user_abort(true);
set_time_limit(0);
ini_set('max_execution_time','600');
ini_set('max_input_time','600');
I know that cron-job.org timeouts after 30 seconds, but i understodd it so that ignore user abort solves that part.
Im absolutely clueless what to do do make this work and i dont see an other way to automate my update process.
Thanks in advance
edit: i dont want to update anything in a database so "update first 100 rows" doesnt really work for me.. im updatign a json.

Related

PHP light alarm clock how do I check my time is equal to a set time?

I am trying to allow my simple website to check the time, so that if the set alarm time is equal to the real time the alarm goes off.
I'm using PHP (and I am pretty sure it must be in PHP due to using LEDs and python). I know this is relatively easy thing to do in js for example.
I have the variables:
$setTime = "$hour$hour2:$minutes$minutes2";
$realTime = date("H:i");
and a if statement:
if ($realTime == $setTime) {
exec("sudo python /home/pi/lighton_1.py");
}
else{
exec("sudo python /home/pi/lightoff_1.py");
}
This all works if when I load my website the real time = the set time however if not it won't. I somehow really want to check the if statement so often somehow. I have tried loops, functions etc and haven't had much success however my coding is a bit basic at the moment. Wondering if anyone knows this solution, (could be very simple?) Need help fast please. Thank you!
You need to understand how your PHP code is triggered. When used to host a website, it is triggered, when someone requests a page. Now, what are the chances than someone will request this page when "$realTime == $setTime". Unless you host a very busy site, the chances are very small. You could have a web page run a JavaScript to continuously refresh the page. Even in this case you may want to say "$realTime > $setTime".
You could alternatively run your PHP from a scheduler like a Unix Cron job, or some kind of PHP scheduler, but then you have to say "$realTime > $setTime", because scheduler may also not run this statement at the exact moment.

PHP script runs to completion, but is timing out

I have a PHP script which contains many database queries, and copies several database tables, and as such, it takes quite a long time to complete. The problem I am getting, is that it is timing out. However, it appears to be completed, which is what is confusing.
The script is suppose to redirect to view once completed. However, even after extending the time limit to 5 minutes, it gives me the timing out error page. However, when I check the database, all of the tables have been copied completely, indicating that the script was completed.
Am I missing something easy here? Is there a general reason it would time out as opposed to redirecting to the view? I would post some of the code, but the entire script is approximately 1000 lines of code, so it seems a bit extensive to show here.
Also, I am using CodeIgniter.
Thanks in advance for your help!
It's possible that the PHP script is not timing out, but the browser you're using has given up waiting for any result. If thats the case you'll need to handle the whole thing differently. For example, run the script in the background and report periodic updates via AJAX or something.
Think of it this way:
Your browser asks your server for a web page and waits for the results.
Your server runs your PHP script, which then asks MySQL to run a query, and waits for results.
MySQL runs the query and returns a result to PHP.
PHP does some more processing and returns a result to the browser.
At step 3, PHP may have timed out and is no longer there. MySQL didn't know that while it was working, it just did its job and then handed a result back to nothing.
At step 4, the browser may have timed out and dropped the connection. PHP wouldn't know that, so it did its job and then returned a result to nothing.
It's two separate timeouts in this example, but your query was completed either way.

PHP: Possible to trickle-output to browser while waiting for database query to execute?

tl;dr: Is it possible to feed some "dummy spaces" periodically back to the browser while waiting for a SQL query to execute? This to not have the browser hang up on me while nothing returns.
Longer story:
I've made a small "web tool" against a database (MS SQL, using their PDO driver).
Sometimes, the queries that I run take a long time.
After about 100 seconds, the browser just stops "rotating". I don't know yet what causes this, but it is the same with Firefox and Chrome. The stack is PHP 5.3, IIS 6, FastCGI. It is not PHP nor DB/SQLSRV timeout, as I've increased both of those - and other queries I have take a longer time to feed back all the result. (I can reproduce the problem by writing some header, chilling for 110 seconds, and then write the footer. Only the header-part is then shown.)
The problem with the present query, is that it doesn't feed back anything for about 200 seconds, then the whole thing comes. But this doesn't help when something along that stack have stopped listening/receiving/transmitting after about 100 seconds.
Thus, the question: Is it possible to trickle-feed the browser some dummy spaces while the script is waiting for the SQL to return? In my native tounge of Java, this would be trivial, but in PHP, one is AFAIK utterly single threaded (actually, "single process'd"). I know that this trickling would work, as I have other scripts that in total takes much longer, but which continually sends small pieces of the result back to the browser - this renders just fine.
Not if you only intend to run one query. However, depending on the nature of your query, you can probably just split it up into multiple smaller queries, and then loop through those.
Contrary to your other answers and comments, you CAN "trickle-feed" data to the browser, if your split your calls up. You're looking for the flush() function.
DEMO
<?php
for ($i = 0; $i <= 200; $i++)
{
sleep(1);
echo ' ';
flush();
}
echo 'It worked!';
?>
Try running this. It should take 200 seconds. However, because flush() is there, it'll send data to the browser after each iteration of the loop, and hopefully not time out! My boss's web host times out after 30 seconds of inactivity (Rackspace, grrrr!) so I've had to use this very same trick countless times.
PHP does not send output to the browser as you echo it. It writes the contents to a buffer, and sends the entire contents to the browser at once. So, no, you cannot trickle output to the browser.
flush doesn't work very well there is a better way - you can pipe the output to a file and then use a independent php script to only ever rip the last line, then use ajax on the client to poll that independent script every 200ms the last line. this gives the effect you want I am working on it now and will get the code here ready when I do
EDIT1: HAHa Endre it's you! <3 from England you little athiest you x
EDIT2: the timeout comes from a plethora of subtle params in and out of PHP.ini, mostly undocumented in the sense they seem to be unrelated, but also do NOT underestimate the browser being lame - in my experience I was only ever able to irradiate timeouts utterly once and only then on firefox but I was able to run/poll a 3 hour long script this way (sadly it was a AD migration script, at my old job and I don't have the code)
EDIT3: you can "thread" in PHP by using the PHP CLI and then the process ID or by using curl both are fairly ewww but you CAN do it

Keep alive solution for cron job

I have a hosted websolution and a script that update information backend for the site.
My webhost allows cron and I have made a script that wget's to every hour.
The script checks the db-table with posts to update for the oldest updated post and update that. In the end of the script I use javascript refresh to do the same procedure again this time with the second oldest updated post and so on until all posts are run through. If I open the script in my browser it works fine but when my host sets up the cron the script does not continue after the javascrpt refresh.
How can I solve this with another refresh solution that will work until the my statement stops it just by letting cron start the script?
(I changed to this solution from one where all posts were updated in one pageload but since it started to time out I went with this)
script.php
$limit=3600;
//Select the oldest updated post
if($last_update<$update_to_limit){ //check if the post was updated during this run
// Script that update the post and below the java refresh that repeats the script.
?>
<script type="text/javascript">
<!--
window.location.href = "http://www.site.se/script.php"
//-->
</script>
<?php
}else{
echo 'OK : All posts updated within the last : '.$limit.' s';
}
Wget will not run Javascript, if you open your code from the browser, it will of course run it, that is the reason of the difference.
I don't really suggest the method you're trying to use. If you really want, you can call wget again from PHP, use cURL or I guess even header('Location..');. But it would be much nicer to solve this problem in one turn.
If your code times out, I'd recheck the way that PHP code is written, and try to find a better solution, that is not so time-consuming. Afterwards: is the DB good enough, are the indexes set, etc. Or, if you cannot optimize it (or you do not want to), you can use set_time_limit().
(One more thing: Java!=Javascript. If you want to shorten it, write js instead of java)

AJAX (prototype/php) getting partial status updates during script execution

I have a process I want to run in the background of a page. The process will take a while to run, lets say a few minutes.
I have it set up so that from the page I can click on a button to start the process off, wait a bit and then it will finish and I can have the page update notifying me that the process was run successfully. I'm doing this with prototype and php.
What I want to do is have status updates while the process is running. So it could update a the page letting me know how many records have been processed so far or update a status bar or something like that.
Is this possible? I'm just not a huge JS guru and I can do the basic functionality I've already described but was wondering if it was possible to extend it to do this little extra bit as I haven't had any luck googling or looking through the docs.
With just PHP you'll pretty much end up doing polling. You'll need to spawn your long-running process in the background, and for example write to a database of file what it's progress is.
The browser can then call a different php script every x seconds, which reports back with this status.
Polling sucks though, but that's what you're stuck with, with PHP.

Categories