MENU

Ideas
Contact Me
Blogs
YouTube
Portfolio

© 2024 Saviourdev. All rights reserved.

29 Apr 20264 min read17 views

Run Your Laravel Queue workers on shared hosting with using cron jobs.

Tired of using cron jobs to run your to run your jobs in queue in laravel , try this , a single command that will change your game .

Adarsh VermaPublished under /run-your-laravel-queue-workers-on-shared-hosting-with-using-cron-jobs

Topics

LaravelQueueJobsEmailsphpshared hsotingcloud hostingsaviourdev

Keep Your Laravel Queue Running on Shared Hosting (Because Supervisors are for Quitters)

Your Laravel app needs to send emails, process images, sync data — all the boring stuff that'll freeze your request if you do it synchronously. Enter queues. But there's a catch: shared hosting doesn't have supervisor (cry). So you'll use nohup — the Unix equivalent of telling a process, "keep running, I'm leaving now."


The 30-Second Setup

Set your queue driver in .env to QUEUE_CONNECTION=database (simplest, works everywhere). If you haven't created the jobs table yet, run php artisan queue:table && php artisan migrate.

SSH into your server and run this one-liner:

nohup php artisan queue:work --timeout=60 --sleep=3 >> storage/logs/queue-worker.log 2>&1 &

Done. Your queue is now running in the background. The process will keep working even after you disconnect.


What Did I Just Do?

  • nohup = "don't kill this when I log out"
  • --timeout=60 = kill jobs that take longer than 60 seconds (prevent zombies)
  • --sleep=3 = check for new jobs every 3 seconds (balance CPU vs. responsiveness)
  • >> storage/logs/queue-worker.log 2>&1 = dump all output to a log file
  • & = run in background

It'll print your process ID (PID). Save it somewhere safe — you'll need it to kill the worker later.


Is It Actually Running?

Check if it's alive:

pgrep -f "queue:work"

Or stalk the logs in real time:

tail -f storage/logs/queue-worker.log

If you see jobs being processed, congrats — you're a queue wizard now.


The Crash Problem (aka Auto-Restart)

Here's the thing: your queue worker might crash. Database locks, memory leaks, cosmic rays — who knows. On shared hosting, there's no supervisor to restart it automatically. So use a cron job watchdog.

Create a file called check-queue.sh in your project root:

#!/bin/bash if ! pgrep -f "php artisan queue:work" > /dev/null; then cd /full/path/to/your/laravel/project nohup php artisan queue:work --timeout=60 >> storage/logs/queue-worker.log 2>&1 & fi

Make it executable:

chmod +x check-queue.sh

Then add it to crontab (runs every 5 minutes):

crontab -e

Add this line:

*/5 * * * * /full/path/to/your/laravel/project/check-queue.sh

Now if your worker dies, it automatically restarts within 5 minutes. You're basically letting a robot babysit your queue.

Pro tip: Use absolute paths everywhere. Relative paths in cron are a special kind of nightmare.

How to Kill It (When Things Go Wrong)

Find the PID:

pgrep -f "queue:work"

Kill it gracefully:

kill <PID>

Or nuke it from orbit (if graceful doesn't work):

kill -9 <PID>

Then restart:

nohup php artisan queue:work --timeout=60 >> storage/logs/queue-worker.log 2>&1 &

Common Disasters & How to Fix Them

❌ "Command not found: php"

Use the full path: nohup /usr/bin/php artisan queue:work .... Find it with which php.

❌ Worker stops after one job

Check the logs: tail -50 storage/logs/queue-worker.log. Probably a database connection timeout or missing env vars.

❌ Too many queue processes running

You started it three times by accident. Kill all PHP processes (carefully): killall php. Or kill specific PIDs one by one.

❌ Server rebooted, queue is gone

You need to manually restart it or add a cron job that runs hourly to ensure it stays alive.

Real talk: If your shared host reboots frequently, consider upgrading to a VPS where you can use supervisor. It's worth the extra $5/month.

The Cheat Sheet

# Start it nohup php artisan queue:work --timeout=60 >> storage/logs/queue-worker.log 2>&1 & # Is it alive? pgrep -f "queue:work" # Watch the logs tail -f storage/logs/queue-worker.log # Kill it kill <PID> # Force kill kill -9 <PID>

That's it. Your Laravel queue is now running 24/7 on budget shared hosting. No fancy services, no Supervisor, just good old Unix magic and a cron job babysitter. Sleep soundly.

Discussion

Comments

0 comments

Sign in with Google to join the discussion.

Loading comments...

Stay Connected

Follow and Explore More

Watch more on YouTube, explore the rest of the brand, and jump back to the homepage to discover ideas, projects, and more.