Laravel - Execute queued job automatically
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Laravel: Executing Queued Jobs Automatically Without Manual Intervention
As a senior developer working with Laravel, you often find yourself in scenarios where asynchronous processing is crucial—sending bulk emails, processing large file uploads, or handling complex background calculations. You dispatch a job, record it in your database, and expect it to run later. However, the transition from development (where you manually run `php artisan queue:work`) to production (where reliability and uptime matter most) presents a challenge: how do we ensure these jobs execute automatically and continuously without relying on manual intervention?
The short answer is that you need to move beyond simply running a single command; you need to implement a robust, persistent queuing system managed by an external process supervisor.
## Understanding the Queue Dispatch vs. Execution Cycle
When you call `dispatch(new SendStartPatEmail($data));` in your controller, Laravel serializes this job and pushes it onto a designated queue (e.g., Redis, database). At this point, the message is safely stored, and the HTTP request completes successfully. The actual execution is deferred.
The problem arises when you rely on manually running `php artisan queue:work`. This command starts a process that continuously polls the queue for new jobs. In a local development environment, this works perfectly. However, in a production server environment (like a VPS or Docker container), if you simply run this command once, the process will terminate as soon as it finishes its initial batch, leaving no mechanism to keep it alive 24/7.
## The Solution: Persistent Queue Workers and Process Managers
To achieve automatic, continuous execution, we need to treat the queue worker not as a one-off script but as a vital, long-running service. This is achieved by deploying the worker process under a system service manager.
### 1. Choosing the Right Driver
First, ensure you have configured your queuing system correctly in `config/queue.php`. For high-volume applications, using drivers that interface with dedicated services like Redis or Amazon SQS is highly recommended over the default database driver for better performance and scalability. As we explore robust application architecture, understanding these underlying mechanisms is key to building scalable solutions, much like the principles discussed on [laravelcompany.com](https://laravelcompany.com).
### 2. Implementing Process Management (The Automation Layer)
The crucial step for automation is using a process manager. A process manager ensures that if the worker process crashes or stops, it is automatically restarted, guaranteeing continuous job processing. The two most common tools for this are **Supervisor** (for Linux environments) or container orchestration systems like Kubernetes.
#### Example using Supervisor (Linux/VPS):
If you are deploying on a standard Linux server, `Supervisor` is the go-to tool. You define a configuration file that tells the operating system to monitor and manage your PHP worker process.
1. **Define the Worker Command:** You specify exactly what command needs to run—in this case, the Laravel queue worker.
2. **Configure Supervision:** Supervisor monitors the script. If the `queue:work` process exits for any reason (error, crash), Supervisor immediately restarts it.
A typical Supervisor configuration snippet might look something like this (this is illustrative; actual paths depend on your setup):
```ini
[program:laravel-worker]
command=/usr/bin/php /var/www/html/artisan queue:work --sleep=3 --tries=3 --queue=default
process_name=%(program_name)s
numprocs=4 ; Run 4 parallel workers for better throughput
autostart=true
autorestart=true
stopwaitsecs=3600
user=www-data
```
By setting `autorestart=true`, you delegate the responsibility of keeping the queue processing alive to the operating system itself. This eliminates the need for manual login and execution every time.
## Best Practices for Production Queuing
* **Parallel Workers:** Instead of running a single worker, deploy multiple instances (e.g., using `numprocs=4` in Supervisor). This allows your application to process jobs much faster by utilizing multiple CPU cores concurrently.
* **Monitoring:** Implement logging and monitoring tools. If jobs are failing repeatedly, you need alerts. Tools like Sentry or dedicated log aggregators help you catch errors immediately rather than discovering them hours later when a manual check is performed.
* **Queue Drivers:** Use a reliable queue driver (like Redis) deployed alongside your application. This separates the job storage from the application logic, making the queuing system highly resilient.
## Conclusion
Transitioning from manually invoked jobs to fully automated background processing is the hallmark of building scalable, professional applications with Laravel. By correctly configuring your queues and employing robust process management tools like Supervisor, you transform a simple dispatch command into a persistent, self-healing background service. This shift ensures that your application can reliably handle heavy loads without requiring constant human oversight, allowing you to focus on feature development rather than queue administration.