Skip to content

Instantly share code, notes, and snippets.

@mauris
Created April 28, 2014 15:44
Show Gist options
  • Save mauris/11375869 to your computer and use it in GitHub Desktop.
Save mauris/11375869 to your computer and use it in GitHub Desktop.
Laravel Artisan Queue Ensurer - Set a cron job to run this file periodically to ensure that Laravel's queue is processing all the time. If the queue listener stopped, restart it!
<?php
function runCommand ()
{
$command = 'php artisan queue:listen > /dev/null & echo $!';
$number = exec($command);
file_put_contents(__DIR__ . '/queue.pid', $number);
}
if (file_exists(__DIR__ . '/queue.pid')) {
$pid = file_get_contents(__DIR__ . '/queue.pid');
$result = exec('ps | grep ' . $pid);
if ($result == '') {
runCommand();
}
} else {
runCommand();
}
@henkiws
Copy link

henkiws commented Feb 21, 2023

@alvaro-canepa thanks this work me

@CodeAndWeb
Copy link

I also need the shell-script variant... pgrep allows to use it without the pid file.
However you can only run one instance of php artisan queue:work

I use pgrep to only search for the artisan queue:work since my provider uses an alias for php.

#!/bin/bash

# Check if "artisan queue:work" is running
if pgrep -f "artisan queue:work" > /dev/null
then
    echo "queue:work is already running."
    exit 1
else
    echo "Starting queue:work..."
    nohup php artisan queue:work > /dev/null 2>&1 &
    echo "queue:work started."
fi

@alvaro-canepa
Copy link

@CodeAndWeb looks nice, more clean than my code!

Thanks to share

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment