How to increase execution time of shell commands? [duplicate] - php

This question already exists:
Python execution from php
Closed 6 years ago.
I am using exec() to run python script from cakephp, upto 30 seconds it is working fine after that php is aborting but still in background python is running to complete the process.
I am using ajax post method and running python and waiting for response. In this workflow if execution of python is crossing 30 seconds the php is not waiting for the response. I am using nginx server. I tried to modify php.ini file to increase max_execution_time=600. But still the issue remains same.

Use set_time_limit() for example:
<?php
set_time_limit(40);//set script's execution time limit to 40 seconds
in large backstage task, I usually apply set_time_limit(0); which means no time limit is imposed.

Related

PHP does not respect the maximum execution time set [duplicate]

This question already has answers here:
Does sleep time count for execution time limit?
(5 answers)
Closed last year.
I am running scripts that last longer than the limit allowed by the server and the server does not terminate them.
The phpinfo() showed me that the max_execution_time is set to 30.
Using the ini_get('max_execution_time') feature the value 30 is displayed, but I put the code with sleep(45) and it runs until the end.
I also tried decreasing the time with ini_set('max_execution_time', 15), but still the code runs normally with sleep(45).
I've used sleep for testing purposes, but this is with functions that use cURL or even foreach and while that are used to create files for users.
What could be changing the server's maximum execution time?
"The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running."
Copied from: sleep().
In other words: Sleep() was a bad choice for testing maximum execution time.

How to trace a script running successfully or timeout in 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.

PHP Exec PHP script from command line on Windows in the background

I have a PHP page that I am executing from another PHP page like this - exec('c:\php\php.exe processing.php > NUL ');
The processing page has a SQL stored procedure that runs and takes awhile to complete after the stored procedure is complete the page then creates a .csv from the results and drops it into a directory.
This entire process works fine as long as it does not take too long to execute. If I am running it on a few thousand records it only takes like 3-5 minutes and completes fine. If I run it with like 20K records the process takes over 10 minutes to complete but is getting killed before it's done.
The speed is not the issue, we are fine with it taking awhile to run but somehow the script is getting killed. While it's running I can see the CLI process from the task manager in Windows but it seems like if it takes more than 10 minutes Windows is killing it or something.
I am just trying to figure out how I can run this without the process getting killed for taking too long.
Any help on this would be greatly appreciated.
Thanks!
Try using set_time_limit(0) (see documentation: http://php.net/manual/en/function.set-time-limit.php).
Setting time limit to 0 will remove any time limit.
Also, make sure you're not having any memory issue. If your script tries to allocate too much memory it will be killed anyway. You can check memory usage of your script with memory_get_usage() (see documentation: http://php.net/manual/en/function.memory-get-usage.php)

IIS 7 long timeout on individual PHP script

I know there was some trick with the metabase in IIS 6 to do this, but 7 is different.
Basically, I have a long-running PHP script that handles its own timeout in the regular PHP way, that keeps getting killed by the server before it's done. I need the IIS 7 procedure for giving it a long or no timeout on just that one script and no others.
check set_time_limit function.
set_time_limit(0); //will remove execution time limit
http://php.net/manual/en/function.set-time-limit.php

PHP with lengthy loop creating erratic server error

I have a PHP script that pulls down a bunch of RSS feeds. To prevent overloading the publishers' servers, I use the PHP Sleep function to slow things down.
The entire script could last for a couple of hours.
If I run this from a Cron job on GoDaddy, it will happily work for 5 - 10 minutes and then return a server error. I checked and the PHP maximum execution time is 30 seconds, so I'm not sure if this is the cause of the problem.
If I run the job on my Mac, my local PHP also has a default maximum execution time of 30 seconds, but this script does work if I run it from the terminal, but I don't understand why.
How do I loop a script that will exceed 30 seconds without running into unreliability problems?
Help appreciated.
Short answer is use set_time_limit(0) to allow for a long-running script. Your terminal (CLI) PHP probably has it set to 0. You could also be running out of memory, especially on PHP 5.2 or older. Log all errors to a file, and inspect it.
You could rewrite your program to be able to work on a subset of the data during one run. The benefit of that approach is you could use it to run 24/7 or to run every five minutes, depending on what the PHP environment supports. You could also run multiple instances at a time, each working on their own data.

Categories