<p>To reiterate what has been said in the comments above and to present an answer, the best way to ensure that queue jobs are completed even without the CMS being logged into or the site being visited is using a scheduled cron job.</p>

<p>The following will run all jobs in the queue every 10 minutes:</p>

<pre><code>*/10 * * * * /usr/bin/php /path/to/craft queue/run
</code></pre>

CC BY-SA 4.0
2021-03-03T02:25:49.810Z
Is this still a valid solution?  I have scoured the "Robust queue job handling in Craft CMS" article at https://nystudio107.com/ and there are a number of somewhat complex solutions to this.  But if I can just manage this with a simple cron, that would be ideal.
gjhead
CC BY-SA 4.0
2021-03-03T07:41:02.633Z
Yes, it should still be valid.
Ben Croker
CC BY-SA 4.0
2019-07-01T15:33:14.513Z
2019-07-01T15:33:14.513Z
Ben Croker
<p>I've noticed that tasks only run when logged into the admin panel, is there anyway to avoid this? </p>

<p>For some context: I have jobs scheduled on a cron to run every 30 minutes, when I log in it seems that all of the items get queued up from the last time I logged in and proceed to run, this isn't terrible - but this app won't be seeing much login activity down the road.</p>

<pre><code>`public function execute($queue)
{
    $section = Craft::$app-&gt;sections-&gt;getSectionByHandle('orders');
    $entryTypes = $section-&gt;getEntryTypes();
    $entryType = reset($entryTypes);

// Create an entry
    $entry = new Entry([
        'sectionId' =&gt; $section-&gt;id,
        'typeId' =&gt; $entryType-&gt;id,
        'fieldLayoutId' =&gt; $entryType-&gt;fieldLayoutId,
        'authorId' =&gt; 1,
        'title' =&gt; 'Order Number: ' . $this-&gt;orderId,
        'slug' =&gt; $this-&gt;orderId,
        'postDate' =&gt; new DateTime(),
    ]);


    $entry-&gt;setFieldValues([
        'orderId' =&gt; $this-&gt;orderId,
        'lineItemMatrix' =&gt; $this-&gt;lineItemMatrix,
        'couponMatrix' =&gt; $this-&gt;couponMatrix,
        'amountPayable' =&gt; $this-&gt;amountPayable,
        'orderTotal' =&gt; $this-&gt;orderTotal,
        'tax' =&gt; $this-&gt;tax,
        'sellerDiscount' =&gt; $this-&gt;sellerDiscount,
        'shipping' =&gt; $this-&gt;shipping
    ]);
    //check to see if entry exists.
    $existing = Entry::find()-&gt;section('orders')-&gt;slug($entry-&gt;slug)-&gt;one();

    if (is_null($existing)){
        return Craft::$app-&gt;elements-&gt;saveElement($entry);
    } else {
       // don't do anything;
        return false;
    }
}`
</code></pre>

CC BY-SA 4.0
2018-11-30T17:13:59.840Z
Tasks should run even when not logged in, would you be able to post the code of the task you have written?
tekstrand
CC BY-SA 4.0
2018-11-30T17:55:53.400Z
@TheEks - edited to question to have the code for the task.
Brett Moore
CC BY-SA 4.0
2018-11-30T21:19:49.083Z
A good way to do this is to just disable the [runQueueAutomatically](https://docs.craftcms.com/v3/config/config-settings.html#runqueueautomatically) and from the CLI start up `./craft queue/listen` which starts a process that polls the queue, and runs things as needed; see [yiisoft/yii2-queue](https://github.com/yiisoft/yii2-queue).
andrew.welch
CC BY-SA 4.0
2018-12-11T22:09:39.340Z
It sounds like you're comfortable with cron tasks already—you can also run a similar command to the one @andrew.welch recommended (`./craft queue/run`) from a cron task. The `listen` method is appropriate when you're OK daemonizing it (or, as on Heroku, you have a "worker" dyno), but if the setup is daunting, cron would be totally suitable.
August Miller
CC BY-SA 4.0
2018-12-11T22:12:31.250Z
Additionally, as @TheEks said: you should be getting a snippet of JS that sends an XHR request to the queue controller, in any page (just before the closing `body` tag), if there are pending jobs in the queue…
August Miller
<php><queue>
How can I get tasks to complete when not logged in to the CP?
# How can I get tasks to complete when not logged in to the CP?

(Asked by: username_0 on 2018-11-30)

I've noticed that tasks only run when logged into the admin panel, is there anyway to avoid this? 
For some context: I have jobs scheduled on a cron to run every 30 minutes, when I log in it seems that all of the items get queued up from the last time I logged in and proceed to run, this isn't terrible - but this app won't be seeing much login activity down the road.
`public function execute($queue)
{
    $section = Craft::$app->sections->getSectionByHandle('orders');
    $entryTypes = $section->getEntryTypes();
    $entryType = reset($entryTypes);

// Create an entry
    $entry = new Entry([
        'sectionId' => $section->id,
        'typeId' => $entryType->id,
        'fieldLayoutId' => $entryType->fieldLayoutId,
        'authorId' => 1,
        'title' => 'Order Number: ' . $this->orderId,
        'slug' => $this->orderId,
        'postDate' => new DateTime(),
    ]);


    $entry->setFieldValues([
        'orderId' => $this->orderId,
        'lineItemMatrix' => $this->lineItemMatrix,
        'couponMatrix' => $this->couponMatrix,
        'amountPayable' => $this->amountPayable,
        'orderTotal' => $this->orderTotal,
        'tax' => $this->tax,
        'sellerDiscount' => $this->sellerDiscount,
        'shipping' => $this->shipping
    ]);
    //check to see if entry exists.
    $existing = Entry::find()->section('orders')->slug($entry->slug)->one();

    if (is_null($existing)){
        return Craft::$app->elements->saveElement($entry);
    } else {
       // don't do anything;
        return false;
    }
}`

**Tags:** php, queue

> **Comments:**
> **username_1**: Tasks should run even when not logged in, would you be able to post the code of the task you have written?  
> **username_0**: @TheEks - edited to question to have the code for the task.  
> **username_2**: A good way to do this is to just disable the [runQueueAutomatically](https://docs.craftcms.com/v3/config/config-settings.html#runqueueautomatically) and from the CLI start up `./craft queue/listen` which starts a process that polls the queue, and runs things as needed; see [yiisoft/yii2-queue](https://github.com/yiisoft/yii2-queue).  
> **username_3**: It sounds like you're comfortable with cron tasks already—you can also run a similar command to the one @username_2 recommended (`./craft queue/run`) from a cron task. The `listen` method is appropriate when you're OK daemonizing it (or, as on Heroku, you have a "worker" dyno), but if the setup is daunting, cron would be totally suitable.  
> **username_3**: Additionally, as @TheEks said: you should be getting a snippet of JS that sends an XHR request to the queue controller, in any page (just before the closing `body` tag), if there are pending jobs in the queue…  

---

## 1 answers

### Answer by username_4 on 2019-07-01. Score: 5 | Accepted answer

To reiterate what has been said in the comments above and to present an answer, the best way to ensure that queue jobs are completed even without the CMS being logged into or the site being visited is using a scheduled cron job.
The following will run all jobs in the queue every 10 minutes:
*/10 * * * * /usr/bin/php /path/to/craft queue/run

> **Comments:**
> **username_5**: Is this still a valid solution?  I have scoured the "Robust queue job handling in Craft CMS" article at https://nystudio107.com/ and there are a number of somewhat complex solutions to this.  But if I can just manage this with a simple cron, that would be ideal.  
> **username_4**: Yes, it should still be valid.  

---

