Max execution time

Also known as: max_execution_time

On this page 7

Max execution time is a configured upper limit on how long a single script or request may run before the interpreter or server terminates it. In PHP the setting is called max_execution_time, and similar ceilings exist in web servers, application servers, FastCGI process managers and reverse proxies. Within the management of server resources, it acts as a safety valve that keeps one slow or looping request from holding a worker, CPU time and memory indefinitely.

The limit matters most on shared and multi-tenant servers, where a single runaway script can starve other sites of workers. It is closely related to the PHP memory limit and to process limits, but it measures time rather than space, and its exact meaning depends on which layer enforces it.

How max execution time stops a running script

Max execution time works by starting a timer when a request begins and aborting the script once the timer passes the configured threshold. In PHP the interpreter checks the timer periodically and raises a fatal error with the message “Maximum execution time of N seconds exceeded”, after which shutdown functions run and the worker returns to the pool.

A detail that surprises many administrators is what the PHP timer actually counts. On Linux and other Unix-like systems, max_execution_time measures CPU time consumed by the script itself, not wall-clock time. Time spent waiting on database queries, sleep() calls, file reads or outbound HTTP requests is usually not counted. On Windows the same setting measures wall-clock time, so identical code can behave differently across platforms.

Because of this, a script stuck waiting on a slow external API may run far longer than max_execution_time suggests. Other layers then become the effective limit: the FastCGI request timeout, the web server proxy timeout or the load balancer idle timeout. The following table shows the main enforcement points and what each one measures.

LayerTypical settingWhat it measuresCommon default
PHP interpretermax_execution_timeCPU time on Unix, wall time on Windows30 seconds (0 on the command line)
PHP-FPM poolrequest_terminate_timeoutWall-clock time per request0 (disabled)
Nginx proxyfastcgi_read_timeoutWait between reads from the backend60 seconds
ApacheTimeout and ProxyTimeoutWait for network activity60 seconds
Load balancerIdle timeoutInactivity on the connectionAbout 60 seconds on many platforms
Where execution time limits are enforced

In practice the shortest limit in the chain wins. If PHP allows 300 seconds but the proxy closes the connection after 60, the visitor sees a 504 Gateway Timeout while the script may keep running in the background until its own limit expires.

Typical values and how they are configured

Appropriate values for max execution time depend on the type of workload, not on a single universal number. Interactive web pages should finish in well under a second, so a ceiling of 30 to 60 seconds already leaves a wide margin, while import jobs, report generation or backups may legitimately need several minutes.

WorkloadTypical limitReason
Public web pages30 secondsSlow pages indicate a fault, not normal work
Administrative dashboards60 to 120 secondsOccasional heavy queries and exports
Data imports and exports300 to 900 secondsLarge files processed in one request
Command-line jobs0 (unlimited)Supervised by cron or a job runner instead
API endpoints10 to 30 secondsClients usually time out earlier
Common max execution time ranges by workload

The value can be set in several places. Server-wide defaults live in php.ini, per-pool values in the PHP-FPM pool file, per-directory values in .htaccess or .user.ini files, and per-script values through set_time_limit() or ini_set(). Note that `set_time_limit()` restarts the timer from zero rather than adding to elapsed time, and hosting providers often lock the setting so that customers cannot raise it beyond the plan ceiling.

How it relates to other server resource limits

Max execution time is one of several complementary guardrails that together keep a server stable. Each limit protects a different resource, and a well-tuned configuration aligns them so that the intended limit triggers first and produces a clear error.

LimitResource protectedTypical symptom when hit
Max execution timeWorker time and CPUFatal error about exceeded execution time
PHP memory limitMemory per scriptFatal error about exhausted memory
Process limitNumber of concurrent processesRequests queue or are refused
Entry processesConcurrent connections per accountError 508 on CloudLinux hosts
CPU throttlingCPU share per accountSlower responses, no hard error
Max execution time compared with neighboring limits

These limits interact. A script that runs for a long time also occupies an entry process and a PHP-FPM worker, so a handful of slow requests can exhaust the process pool long before CPU utilization looks alarming. Conversely, CPU throttling on a shared host makes scripts slower, which can push borderline jobs over their execution time ceiling.

Choosing and tuning the limit

The right approach is to set max execution time as tight as normal traffic allows and to move genuinely long work out of the web request. Raising the limit to hide a slow script treats the symptom and leaves the worker pool exposed to the next spike.

SituationRecommended actionWhy
Page occasionally exceeds 30 secondsProfile queries and add indexesThe cause is usually inefficient code
Large import in the browserSplit into batches or a background jobAvoids proxy and browser timeouts
Nightly maintenance taskRun from the command line with cronCLI has no default time ceiling
Slow third-party APISet explicit HTTP client timeoutsPHP CPU timer may not count waiting
Proxy returns 504 firstAlign proxy and FPM timeoutsThe shortest limit hides the real error
Decisions when scripts approach the limit

A sound rule is to keep the layers ordered from inner to outer: PHP limit shortest, then the PHP-FPM request_terminate_timeout, then the proxy timeout, then the load balancer. With this ordering the application produces a logged, explainable error instead of an anonymous gateway timeout.

Limitations and common mistakes

Max execution time is a coarse instrument and has several well-known blind spots that administrators should account for.

  • Waiting time is often invisible: on Unix the PHP timer ignores time spent in database calls and network I/O, so it does not protect against slow dependencies.
  • Setting the value to 0 removes the ceiling entirely and lets a stuck request hold a worker until a restart.
  • Abrupt termination can leave partial writes, half-processed batches or stale locks unless the code uses transactions and shutdown handlers.
  • Raising the limit globally for one slow plugin weakens protection for every site on the server.
  • Ignoring outer timeouts leads to scripts that keep running after the visitor has already received an error.

Monitoring helps close these gaps. Tracking slow request logs in PHP-FPM, response time percentiles and the count of timeout errors shows whether the limit is protecting the server or merely masking a deeper performance problem.

A non-technical view

Think of a server as a restaurant kitchen with a fixed number of cooks. Each visitor request is an order, and each cook can handle only one order at a time. Max execution time is a rule that says no single order may occupy a cook for longer than a set time. If a dish takes too long, the cook stops and moves on.

This rule protects everyone else waiting for food. Without it, one complicated or broken order could tie up a cook all evening. The key point is that the limit protects shared capacity, so very long tasks are better prepared separately, outside the normal rush.

A concrete example

An online shop on a managed PHP host lets staff upload product catalogs of about 40,000 rows through an administration page. With max_execution_time set to 60 seconds and an Nginx fastcgi_read_timeout of 60 seconds, uploads regularly fail halfway, leaving some products updated and others not.

The administrator first considers raising both limits to 600 seconds, but that would let any slow page hold a worker for ten minutes. Instead, the upload page now stores the file and queues a background job processed by a command-line worker in batches of 500 rows, each inside a database transaction.

The web limits stay at 60 seconds for every page, imports finish in about four minutes without errors, and the shop no longer suffers from exhausted PHP-FPM workers during catalog updates.