How to trace a script running successfully or timeout in PHP - php

I have a PHP script which running some crawling job, and which probably require 5 minutes above to complete.
My question as below:
If I try to execute the script via browser request, probably will experience a request timeout after 30 seconds, but is it the script still running on server until completion?
If I execute the script via cron job, how do I trace the running status? How do I know if the script still running or already been kill by server?
Is it possible to increase the maximum execution time via PHP code without touching the php.ini file?
Appreciate for the reply.

If I try to execute the script via browser request, probably will
experience a request timeout after 30 seconds, but is it the script
still running on server until completion?
your script also stop processing on server.
If I execute the script via cron job, how do I trace the running
status? How do I know if the script still running or already been kill
by server?
You can track it by putting log in file at beginning of your script and at the end of your script.
Is it possible to increase the maximum execution time via PHP code
without touching the php.ini file?
You can increase the maximum execution time via PHP code by
ini_set('max_execution_time',300);
but it will only work if your HTTP_CONNECTION variable is set to keep-alive on server.

Related

About max execution timeout

I have some question on server response max execution timeout.
If, I called server API to running something huge and not able to finish within time limit set in server php.ini max_execution_time config, will the process in server still continue to process?
- if so, will it process endless?
- if not, is the process stop immediately or canceling loop one by one and finish all process.
In my experience, when I receive max execution timeout on local hosting, the data is already process.
So I not sure it is because it is stuck on response until timeout or server is continue running after throw max execution timeout exception.
It really depends on what your PHP code is like.
Usually the code execution will halt. You can alter this behaviour using ignore_user_abort().
PHP interpreter runs scripts against php.ini configuration and checks max_execution_time = 500 and max_input_time = 500.
PHP doesn't continue to run the script after the max_execution_time. It's simply "kills" the script.
What can also happen, script starts a database query, normally query will run on database server until finished no-matter what happens to the script. Also you may get a Gateway Timeout coming from the web server, for Apache check httpd.conf and look for the setting Timeout.
If you need to run a script that takes time to execute, a lot more then the rest of your website, you should call a web page, PHP on server, fork a new process as a background executed script (the PHP part that takes lot of time), inform user via async status updates or sending an email that processing ended. You should not extend max_execution_time for all script just for one exception.
It doesn't continue after the exception is thrown. It's simply cut when the time is up.
Anything before the time out is executed. If not designed especially to precent this.
The process won't continue and stop immediately after the time limit set in server php.ini max_executiontime config has been reached then php throw a max execution timeout exception.
Here (How to increase maximum execution time in php) if you want to increase maximum execution time in php file.

Limit of concurrent PHP scripts

I have some PHP scripts that process data
unfortunately I can only run 6 script simultaneously
it seems that there is some sort of limitation in php or apache that makes the 7th script waits until another script ends
the script only processes data, there isn't any kind of connection with the database or any web request
How can I increase this limit?
#Leo - How are you running these scripts simultaneously? Is it web browser calling it (as you have mentioned apache in question)? -- browser has simultaneously connection limit. OR some other scenario.
place this function top of each page.
set_time_limit(0);
more details you can found on
http://php.net/manual/en/function.set-time-limit.php

Long lasting script prevents handling new requests

I have a PHP script on my Apache web server, which starts another several hours running PHP script. Right after the long-lasting script is started no other PHP script requests are handled. The browser just hangs eternally.
The background script crawls other sites and gathers data from ones. Therefore it takes quite long time.
At the same time static pages are got without problems. Also at the same time any PHP script started locally on the server from bash are executed without problems.
CPU and RAM usage are low. In fact it's test server and my requests are only ones being handled.
I tried to decrease Apache processes in order to be able to trace all of them to see where requests are hung. But when I decreased amount of processes to 2 the problem has gone.
I found no errors neither in syslog nor in apache/error.log
What else can I check?
Though I didn't find the reason of Apache hanging I have solved the task in a different way.
I've set a schedule to run a script every 5 minutes. From web script I'm just creating a file with necessary parameters. Script check existence of the file and if it exists it reads its content and deletes to prevent further scheduled start.

how to create continues executing script in php with no execution limit

i have created a script to continuously listen from SMSC to catch the delivered SMS. script looks OK but want to make sure it never stops.
In One post it is stated that server may interfere the
continuous execution and could stop the execution, but could not
find any option in httpd.conf to disable the execution limit.
Secondly is below script is going to take 100% CPU usage? as some
other application are running on the same server.
Third what medium is better? Browser or php CLI?
would appreciate any help or any reading material on stated problem. using window server, XAMPP with apache 2.2, php 5.2.9
set_time_limit(0);
do{
//read incoming sms
$sms=$tx->readSMS();
//check sms data
if($sms && !empty($sms['source_addr']) && !empty($sms['destination_addr']) && !empty($sms['short_message'])){
//send sms for processing in smsadv
$from=$sms['source_addr'];
$to=$sms['destination_addr'];
$message=$sms['short_message'];
// insert in database...
}
//continuously check for SMS
}while(1);
EDIT
As per below response i used PHP script with CLI and without set_time_limit(0) as in script running through CLI has no execution limit.
You should use CLI script because the typical scenario of using PHP on server side to provide dynamic web pages isn't designed to have script running "forever". As advised in comments you might use scheduler script to be requested by browser frequently. If you don't plan to use brwoser at all, use CLI instead.
Regarding CPU load you might consider inserting sleep(1) in your while loop (in case of you're polling for incoming SMS rather than waiting for it as presumed in comments on question.

PHP, Ajax, and the lifespan of the request

I was wondering about the lifespan of a PHP script when called via Ajax. Assume that there is a long-running (i.e. 30 seconds) PHP script on a server and that page is loaded via Ajax. Before the script completes, the user closes the browser. Does the script continue running to completion, is it terminated, or is this a function of the server itself (I'm running Apache fwiw). Thanks for any help.
This may be of interest: ignore_user_abort()
ignore_user_abort — Set whether a client disconnect should abort script execution
However note
PHP will not detect that the user has aborted the connection until an attempt is made to send information to the client.
The script will continue running. Closing the browser on the client does not notify the server to stop processing the request.
If you have a large time consuming script, then I would suggest splitting it up into chunks. Much better that way

Categories