How to set timeout for Laravel artisan
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Set Timeout for Laravel Artisan Jobs: Controlling Execution and Preventing Zombie Processes
As a senior developer working with Laravel, you frequently deal with scheduled tasks and background processing. When these jobs run long or get stuck, managing those orphaned processes becomes a significant challenge. The scenario you described—where processes don't terminate correctly, leading to CPU spikes—is a classic symptom of poorly managed external execution environments.
This post will dive into why this happens and provide practical, developer-focused methods for setting timeouts for your Laravel Artisan commands, ensuring robust process control.
## Understanding Process Execution in CLI Jobs
When you execute `php artisan command`, you are running a standalone PHP script via the command line. Unlike web requests, these scripts do not inherently inherit complex application-level resource management unless explicitly programmed to do so. When an Artisan job runs indefinitely or encounters an unhandled exception that prevents clean exit, the underlying operating system process remains active until it is manually terminated (or killed by an external mechanism).
The issue you are facing—processes persisting and consuming CPU cycles—stems from the fact that the PHP script itself might not be designed to gracefully handle time limits. We need a mechanism outside of the application logic to enforce these limits at the operating system level.
## Practical Method 1: Using the OS `timeout` Command
The most straightforward and reliable way to enforce a timeout on any command-line execution, including Artisan jobs, is by utilizing the native operating system's `timeout` utility. This utility executes the command and terminates it if it runs longer than the specified duration.
### Implementation Example
You can wrap your Artisan command with the `timeout` command in your shell script or directly in a custom wrapper:
```bash
# Example: Running a long-running fetch task with a 60-second timeout
timeout 60 php artisan fetchTopic 0 10 1
```
**How this works:** If the `fetchTopic` command exceeds 60 seconds of execution time, the operating system immediately sends a signal (like SIGTERM or SIGKILL) to the PHP process, forcefully terminating it and freeing up resources. This prevents processes from becoming permanent zombies consuming CPU.
### Best Practice: Wrapper Scripts
For complex setups where you run many jobs, creating small shell wrapper scripts is highly recommended. These wrappers can handle logging, environment setup, and applying timeouts consistently across all your scheduled tasks. This approach aligns well with the principles of reliable system interaction, which is a core consideration in modern application architecture, much like the robust design philosophy promoted by [Laravel Company](https://laravelcompany.com).
## Advanced Consideration: Handling Timeouts Within PHP
While the OS `timeout` command handles external execution limits, for jobs that are executing complex internal logic within PHP, you might need to implement internal checks. This involves using PHP functions like `pcntl_alarm()` or checking elapsed time manually within your job class.
For instance, in a long-running Artisan command, you could periodically pause execution and check the current time against the start time. If the threshold is breached, the script can throw an exception that signals failure, allowing external monitoring tools to detect the error immediately.
```php
// Conceptual example within a custom Artisan command class
$startTime = time();
$timeoutSeconds = 300; // 5 minutes
while (time() - $startTime < $timeoutSeconds) {
// Perform a chunk of work...
sleep(5); // Pause briefly to prevent 100% CPU usage while waiting
}
// If the loop finishes without exiting, it completed successfully.
```
## Conclusion
Setting timeouts for Artisan jobs is crucial for maintaining system stability and preventing resource exhaustion. Relying solely on application logic is insufficient; you must leverage operating system tools like `timeout` for external control over execution duration. By combining OS-level safeguards with thoughtful internal PHP logic, you ensure that your scheduled tasks remain predictable, controlled, and free up system resources efficiently. Always strive for robust process management when dealing with background operations in any Laravel application.