1 month ago

Laravel queue worker stops after 15min if started via task schedule but keeps running if started from CLI

Posted 1 month ago by bramcou

Hi all,

I've been running a Laravel website on shared hosting for a couple of years now with a email queue setup. In the process of switching to a new (shared) host and I'm running into trouble with the queue worker.

On the previous host the queue worker kept running for weeks, never needed a restart (only when I forced one after updating the website).

Since I'm on a shared hosting environment I can't install supervisor (do have SSH access) so I use the task scheduler to verify whether the queue worker is still running and if it isn't, the task scheduler restarts the queue worker.

Code to verify if the queue is running:

$path = $this->getPath();

        if (file_exists($path . '/')) {
            $pid = file_get_contents($path . '/');
            $result = exec("ps -p $pid --no-heading | awk '{print }'");
            return $result == '' ? false : true;

        return false;

And the code to restart the queue worker, if it has stopped:

$command = 'php /laravel/artisan queue:work --delay=10 --timeout=30 --sleep=10 --tries=3 > /dev/null & echo $!';
        $number = exec($command);
        file_put_contents($path . '/', $number);

The task scheduler runs this task every minute. After 15 minutes the queue worker stops and the task scheduler restarts it. When I start the queue worker from the CLI it keeps running for hours, no sweat, as long as I keep my CLI terminal open.

When I close my CLI terminal and then restart my CLI and run 'ps aux' the process is still running however (logically) no longer owned by my CLI. In that case, the queue worker also stops after 15 minutes.

No errors in my laravel.log or send to sentry.

  1. Could this be anything else then my hosting environment auto killing unowned processes after they've run for 15 minutes?

  2. Would it be troublesome to work in this configuration with my task scheduler checking the queue worker every minute and then auto restarting the worker every 15 minutes?

  3. sub question: on this new host I have access to a 128mb redis. Used to use the database driver for the queue and now wondering if I should switch to redis. From what I've found on the web it would appear redis would only be preferential when running heavy queue's. Our queue handles, tops, some 100-200 emails a hour, usually no more than 10-20 a hour.

I've figured that I could ask the host if they can allow me to 'whitelist' my queue worker but they pretty clearly state that running deamons isn't allowed in the first place so I think that's probably only gonna get me in trouble..

Always easier to say sorry afterwards then to ask permission beforehand (-:


Please sign in or create an account to participate in this conversation.