Fatal error: Maximum execution time of 300 seconds exceeded

Fatal error: Maximum execution time of 300 seconds exceeded

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fatal error: Maximum execution time of 300 seconds exceeded

You are running a script via the command line, expecting it to execute indefinitely or for a very long duration, but PHP is halting execution after exactly 300 seconds (5 minutes) with a Fatal error. You have already attempted the standard troubleshooting steps: modifying max_execution_time and max_input_time in your php.ini file to values like 0, -1, or 4000. Despite these changes, the error persists. This discrepancy suggests that the configuration you are editing is not the one actually being used by the CLI process, or the environment overrides your local settings.

The core issue lies in understanding how PHP loads configuration files differently for the web server versus the command line interface. When you run phpinfo() from a browser, it displays the settings active for that specific request handler (often Apache or Nginx). However, when you invoke php directly from your terminal, it reads a completely different set of directives.

If you have not explicitly specified an ini-file path during the command line invocation, PHP looks for default locations. These defaults are often system-wide files located in /etc/php/.../cli/php.ini, distinct from the file you might be editing in your project's root directory or the one active for your web server. Even if you edit the php.ini associated with your web stack, that configuration is irrelevant to a standalone CLI execution unless explicitly told otherwise.

To verify which configuration is actually active, you must check the specific error reporting output. The error message itself usually contains a path indicator. If you see something like Maximum execution time of 300 seconds exceeded, it implies the limit is hardcoded to 300. This value appears in the default php.ini distribution provided by many Linux package managers if no custom file is specified or loaded.

You can confirm the active configuration path and its values by running:

php -i | grep max_execution_time
php -i | grep "Loaded Configuration File"

If the output for Loaded Configuration File points to a system default (e.g., /etc/php/8.x/cli/php.ini) rather than your project's config, you have two viable solutions. The first is to pass the configuration file directly via the command line flag:

php -d max_execution_time=0 -d max_input_time=0 /path/to/your/script.php

This approach overrides the system defaults without requiring permission changes to global configuration files. Setting these values to 0 tells PHP to run until the process is explicitly killed by an operating system signal (like SIGTERM or a timeout set by your container orchestrator) rather than hitting an internal ceiling.

However, there is a critical pitfall when working with containerized environments, such as Docker. Many modern PHP distributions for containers include the cli binary but do not automatically load a custom php.ini. In these scenarios, the default system configuration applies unless you mount your own ini-file into the container or use the -d flag at runtime. If you are using a framework like Laravel, ensure that any overrides in config/app.php or environment-specific settings aren't inadvertently resetting these values upon deployment.

Another potential cause is the web server's timeout mechanism interfering with CLI scripts if they are being executed through a specific proxy or gateway that enforces its own limits. While less common for pure CLI execution, some hosting environments apply global timeouts at the system level (via ulimit or systemd timer configurations) that sit above PHP's internal limits.

If you need to run long-running batch jobs, relying solely on increasing PHP's limit might not be the most robust solution. Instead, consider breaking large tasks into smaller chunks that complete within safe time windows, allowing for better error recovery and resource management. This architectural shift prevents a single runaway process from locking up your server indefinitely.

Finally, if you are deploying to a managed hosting environment via https://laravelcompany.com or similar services, verify their specific documentation regarding CLI execution limits. Some hosts disable infinite execution times for security reasons and enforce strict timeouts regardless of php.ini settings. In such cases, the only recourse is to optimize your script's performance or adjust the job queue strategy to respect these imposed boundaries.

Our network