How to Run php artisan queue:work Continuously on cPanel for Laravel Projects
To have the php artisan queue:work command always running on cPanel, you’ll need to set it up using a background process or a cron job that runs indefinitely. However, cron jobs typically run for a specified time and terminate, so you can't rely on them for a truly persistent background task.
Here's how you can set it up with Process Management or using supervisord (if available) or through a long-running cron job loop:
Method 1: Using nohup for a Long-Running Process
SSH into your cPanel account:
- You’ll need SSH access to your cPanel account to run this command in the terminal. Ensure SSH is enabled for your account.
Run the Queue Worker in the Background:
Navigate to the directory where your Laravel project is located, e.g.:
cd /home/username/public_htmlThen, use the
nohupcommand to run the worker in the background:nohup php artisan queue:work --daemon &The
--daemonflag makes the queue worker run continuously without stopping. Thenohupcommand prevents the process from being terminated when you log out.The
&puts the process in the background.
Monitor the process:
After running the command, check if the process is running:
ps aux | grep queue:work
Ensure it restarts automatically:
- The
nohupcommand ensures it will keep running after you disconnect, but to ensure it restarts automatically in case of failure, you can set up a cron job that checks the process every minute and restarts it if it’s not running.
- The
Method 2: Using cron to Keep the Worker Alive
Create a Persistent Cron Job:
In cPanel, go to Cron Jobs under Advanced.
Set up a cron job that runs every minute to check if the worker is already running, and if not, restart it. Use this cron job:
* * * * * ps aux | grep 'queue:work' | grep -v grep || nohup php /home/username/public_html/artisan queue:work --daemon &
- This command checks if the
queue:workprocess is already running (ps aux | grep 'queue:work'). If not, it starts the process (nohup php artisan queue:work --daemon &).
Save and test the cron job:
- After setting up the cron job, it will ensure the worker is always running, even if it crashes or stops for any reason.
Method 3: Using supervisord (if allowed on your server)
If you have access to a process manager like supervisord (it may need to be installed and configured by your hosting provider), you can configure it to keep the queue worker running:
Install
supervisord(if allowed):- In some shared hosting environments, this may not be possible, but if you have a VPS or dedicated server,
supervisordis a great tool for managing background processes.
- In some shared hosting environments, this may not be possible, but if you have a VPS or dedicated server,
Configure
supervisordto manage the worker:Add a new configuration to supervise the Laravel queue worker.
Example configuration:
[program:laravel-queue-worker] command=php /home/username/public_html/artisan queue:work --daemon autostart=true autorestart=true stderr_logfile=/home/username/laravel-queue-error.log stdout_logfile=/home/username/laravel-queue-output.log
Start and monitor with
supervisord:- After configuring, start the process with
supervisord.
- After configuring, start the process with
Note: Using nohup or the cron job method is the simplest and most effective way to run php artisan queue:work continuously on cPanel since shared hosting typically restricts tools like supervisord.
Let me know if you need further clarification or assistance!
Comments
Join the discussionNo comments yet. Be the first to comment.


