Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel 5 Database Queue - Shared Hosting
/usr/bin/php /path/to/artisan schedule:run >/dev/null 2>&1
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$path = base_path();
$schedule->call(function() use($path) {
if (file_exists($path . '/queue.pid')) {
$pid = file_get_contents($path . '/queue.pid');
$result = exec("ps -p $pid --no-heading | awk '{print $1}'");
$run = $result == '' ? true : false;
} else {
$run = true;
}
if($run) {
$command = '/usr/bin/php -c ' . $path .'/php.ini ' . $path . '/artisan queue:listen --tries=3 > /dev/null & echo $!';
$number = exec($command);
file_put_contents($path . '/queue.pid', $number);
}
})->name('monitor_queue_listener')->everyFiveMinutes();
}
}
register_argc_argv=On
@andreich1980

This comment has been minimized.

Copy link

commented Mar 28, 2019

It's a nice approach. But you tell me why do we need register_argc_argv=On in this case?
I have read this https://www.php.net/manual/en/ini.core.php#ini.register-argc-argv but still don't understand.

@generahben

This comment has been minimized.

Copy link

commented Apr 17, 2019

I am having issue setting up queue on shared host. Though i setup cron to run queue:work once every minute, this is causing 503 error over time. I need a better approach, that is why i want to try your solution.

I copied your code into my laravel 5.7 app/console/kernel.php but the queue didnt run. I tried to manually run schedule:run on localhost but i got "No scheduled commands are ready to run.". Is there any thing i have done wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.