Skip to main content

Command Palette

Search for a command to run...

How to Run php artisan queue:work Continuously on cPanel for Laravel Projects

Updated
3 min read

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

  1. 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.
  2. Run the Queue Worker in the Background:

    • Navigate to the directory where your Laravel project is located, e.g.:

        cd /home/username/public_html
      
    • Then, use the nohup command to run the worker in the background:

        nohup php artisan queue:work --daemon &
      
    • The --daemon flag makes the queue worker run continuously without stopping. The nohup command prevents the process from being terminated when you log out.

    • The & puts the process in the background.

  3. Monitor the process:

    • After running the command, check if the process is running:

        ps aux | grep queue:work
      
  4. Ensure it restarts automatically:

    • The nohup command 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.

Method 2: Using cron to Keep the Worker Alive

  1. 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:work process is already running (ps aux | grep 'queue:work'). If not, it starts the process (nohup php artisan queue:work --daemon &).
  1. 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:

  1. Install supervisord (if allowed):

    • In some shared hosting environments, this may not be possible, but if you have a VPS or dedicated server, supervisord is a great tool for managing background processes.
  2. Configure supervisord to 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
      
  3. Start and monitor with supervisord:

    • After configuring, start the process with supervisord.

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!

45 views