17

I want my laravel queue:work to keep running on a shared hosting, this is a shared hosting (am not on a VPS) I can't install anything because almost all online sources when I was searching for this suggest installing supervisor. So basically I thought I could just create a cron job that checks if queue is not running and starts it, any help on how to go about this because am kinda stuck, thanks.

P.S. Am on a Linux server and Laravel 5.3

9 Answers 9

23

Laravel >= 5.7

Adding this line to the scheduler in app\Console\Kernel.php and then setting up a cron job to run the scheduler, ensures that the queue gets serviced every minute.

$schedule->command('queue:work --stop-when-empty')
    ->everyMinute()
    ->withoutOverlapping();

For Cpanel servers, the Cron statement might look like:

* * * * * /usr/local/bin/php /home/{account_name}/live/artisan schedule:run

where {account_name} is the user account that cpanel is running under and live is the folder of the laravel application

Source: https://talltips.novate.co.uk/laravel/using-queues-on-shared-hosting-with-laravel

Laravel <= 5.6

--once To force worker to exit after running one job

--tries The maximum number of times a job may be attempted

--timeout The maximum number of seconds that jobs can run may be specified

withoutOverlapping() By default, scheduled tasks will be run even if the previous instance of the task is still running. To prevent this, you may use the withoutOverlapping method

sendOutputTo() Send the output to a file for later inspection

$schedule->command('queue:work --timeout=60 --tries=1 --once')
    ->everyMinute()
    ->withoutOverlapping()
    ->sendOutputTo(storage_path() . '/logs/queue-jobs.log');

Laravel All version

If you don't want to use the `--once' or '--max-time' options, it's better to use the restart command to avoiding memory leak and make apply code changes.

$schedule->command('queue:restart')
    ->everyFiveMinutes();

Laravel <= 5.2

Not recommended:

The --daemon options continued to run, which you can run once from the command line if you have ssh or cpanel 'command' access.

1
20

As you mentioned you are using shared hosting, You follow the below steps.

Step 1. you need to setup your queue driver as database

Step 2 you need to setup a cron job with with the following command php /path/to/application/artisan queue:work --queue=high,default.

You can give a try. I hope it will work.

10
  • ok first am using sqs but that doesn't matter i guess, second how often should the cron job execute because if its once, should i be worried that the server might be temporary down and the process be terminated?
    – lulliezy
    Commented Sep 10, 2017 at 14:26
  • cron needs to be triggered every min, then only u will get the maximum accuracy of job Commented Sep 10, 2017 at 14:33
  • will the next command replace the previous one or will there be a case of multiple commands executing parallelly
    – lulliezy
    Commented Sep 10, 2017 at 14:41
  • 2
    But this approach is consuming a lot of RAM, what to do to avoid so?
    – Ishaan
    Commented Aug 5, 2019 at 11:12
  • 2
    @Ishaan You may need to add a flag like this php artisan queue:work --queue=high,default --stop-when-empty Commented Jul 20, 2021 at 15:18
7

the best way is to set followin command on your tasks of panel (I'm using plesk control panel, it makes me able to set task there)

php artisan queue:work --once

Note: in my shared host, I must set following values because of their server configuration:

  1. php: /opt/plesk/php/7.2/bin/php -q
  2. artisan: /var/www/vhosts/t4f.ir/httpdocs/artisan
  3. my command: then I should write the command

so, the result would be like this:

/opt/plesk/php/7.2/bin/php -q /var/www/vhosts/t4f.ir/httpdocs/artisan queue:work --once

there is another option for runtime which I set to Cron type with value of: * * * * * which means, every minute this code will be executed. as I used --once in end of my commad, once it execute the command and job has been finished, it will be terminated. regarding to concurrent execution, I'm not worried about beacuase it's handling in queueing system and it's responsibility of this system.

2
  • Is the -q for making it quiet? Commented Sep 2, 2020 at 8:03
  • @MichaelSørensen to be honest, I don't know :) I'm using it based on my last searches, but glad to hear from you, if you will find it. I will learn. Commented Sep 4, 2020 at 10:46
7

With Laravel 8 you have the option --max-time for queue:work that allows it to stop job processing after some amount of time.

php artisan queue:work --max-time=300

You can setup a cron job, for example, every 5 minutes to run job processing and set the --max-time option to 5 minutes (300 seconds). In that case, cron will run job processing, after 300 seconds queue:work will exit, and, in a couple of seconds, cron will run job processing again, and so on.

4

This is the solution that worked for me after searching for days.

flock -n /tmp/latavel_queues.lockfile /usr/bin/php /path/to/laravel/artisan queue:listen

See https://laracasts.com/discuss/channels/servers/database-queue-on-shared-hosting

0
2

One more solution (I solved same problem in this way). You can make script like this:

# getting running processes with name "queue:work"
QUEUE_STATUS=$(ps aux | grep "queue:work")

# check is queue:work started, if no, start it
if $( echo $QUEUE_STATUS | grep --quiet 'artisan queue:work')
then
    exit;
else
    php ~/public_html/artisan queue:work
fi

and run it in CRON. I run every 10 min.

1
  • the cron detect that it has the queue work but when i check it on shell, it doesnt have any process about queue
    – Akbar Noto
    Commented Aug 5, 2019 at 19:11
2

Try adding two cron jobs:

  1. To clear cache:
/usr/local/bin/php /home/<path-to-project>/artisan cache:clear
  1. To run the scheduler
/usr/local/bin/php /home/<path-to-project>/artisan schedule:run

Also, make sure that your app\Console\Kernel.php is having something like

protected function schedule(Schedule $schedule)
    {
        $schedule->command('queue:work --tries=3')
            ->cron('* * * * *')
            ->withoutOverlapping(5);
    }

Remove the first job (the one to remove cache) if the queue starts working

1
  • I have done exactly the above and nothing runs
    – Jeche
    Commented Apr 7, 2021 at 9:57
0

I figured a hack to accomplish this

On file Illuminate\Queue\Worker.php my current laravel version (5.3) is on line 151; on function runNextJob($connectionName, $queue, WorkerOptions $options) add else as below

if ($job) {
    return $this->process(
        $connectionName, $job, $options
    );
 } else {
    $this->stop();
}

Now create cron job that will run the number of times you like with command php artisan queue:work the moment the queue is exhausted, it will exit (but should be frequent as the process exists)

UPDATE: Using task schedular with withoutOverlapping() prevents further calls of the command if its already running, so this is a better option considering the previous one is a change you have to make everytime you composer install or composer update

0

// step 1: To run the scheduler

/usr/local/bin/php /home/USERNAME/public_html/PROJECT_FOLDER/artisan schedule:run >> /dev/null 2>&1

// step 2 in kernel.php

protected function schedule(Schedule $schedule)
    {
        $schedule->command('queue:work', [
        '--max-time' => 300
        ])->withoutOverlapping();

// step 3 test it. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.