# Laravel Scheduler Cron Not Working

**URL:** <https://community.fly.io/t/laravel-scheduler-cron-not-working/8304>\
**Category:** Questions / Help\
**Created:** [October 29, 2022, 4:12pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304 "2022-10-29T16:12:49Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![josephxanderson](https://avatars.discourse-cdn.com/v4/letter/j/c57346/32.png) [@josephxanderson](https://community.fly.io/u/josephxanderson)\
**Post date:** [October 29, 2022, 4:12pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/1 "2022-10-29T16:12:49Z")

</div>

Hello,

Using the standard template for a new Laravel app, the cron worker is not doing anything.

**fly.toml**

```auto
[processes]
  app = ""
  cron = "cron -f"
  worker = "php artisan queue:work"

```

**Dockerfile** (only section where Cron is set)

```auto
RUN composer install --optimize-autoloader --no-dev \
    && mkdir -p storage/logs \
    && php artisan optimize:clear \
    && chown -R webuser:webgroup /var/www/html \
    && sed -i 's/protected \$proxies/protected \$proxies = "*"/g' app/Http/Middleware/TrustProxies.php \
    && echo "MAILTO=\"\"\n* * * * * webuser /usr/bin/php /var/www/html/artisan schedule:run" > /etc/cron.d/laravel \
    && rm -rf /etc/cont-init.d/* \
    && cp docker/nginx-websockets.conf /etc/nginx/conf.d/websockets.conf \
    && cp docker/entrypoint.sh /entrypoint \
    && chmod +x /entrypoint

```

The logs for the `cron` instance shows `Preparing to run: `/entrypoint cron -f` as root` and then nothing happens. No jobs are ever scheduled. I know @fideloper-fly has worked on some issues with this. Any ideas?

---

<div class="post-metadata">

**Author:** ![fideloper-fly](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/fideloper-fly/32/3834_2.png) [@fideloper-fly](https://community.fly.io/u/fideloper-fly)\
**Post date:** [October 29, 2022, 4:38pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/2 "2022-10-29T16:38:06Z")

</div>

Hi!

What’s your evidence that nothing is happening?

“No jobs are ever scheduled” implied that your cron tasks are creating queue jobs - **is that right?**

1️⃣ You can use `fly ssh console -s` and select an instance to SSH into it. They aren’t necessarily named obviously but in my experience, the 2nd will be `cron`, and the third instance will be `worker`.

You can run commands like `ps aux | grep cron` to see that CRON is a running process or `ps aux | grep artisan` to see if the queue worker is running.

2️⃣ Then perhaps head to `/var/www/html` and run `php artisan schedule:run` manually to see if it works.

3️⃣ Also be sure to check `fly logs` to see if there’s any error output.

❓ Do you have env var setup for your queue connection? The default is `sync`, so they wouldn’t actually get run in a queue but whenever a job is dispatched (during the `schedule:run` command, if my guess is correct about the scheduled jobs firing jobs into the queue).

Be sure to check out the cron/queue docs to see if you missed anything, such as adjusting the `php artisan queue:work` command to work on the queue connection you need it to run on: [Cron and Queues · Fly Docs](https://fly.io/docs/laravel/the-basics/cron-and-queues/)

---

<div class="post-metadata">

**Author:** ![josephxanderson](https://avatars.discourse-cdn.com/v4/letter/j/c57346/32.png) [@josephxanderson](https://community.fly.io/u/josephxanderson)\
**Post date:** [October 29, 2022, 4:47pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/3 "2022-10-29T16:47:25Z")

</div>

Hi - thanks for getting back to me so quickly.

The evidence is that there are no jobs being scheduled.

My queue instance logs sit at `INFO Processing jobs from the [default] queue.` unless I manually use `fly ssh console -s` to SSH into the `cron` instance, go to `/var/www/html` and run `php artisan schedule:run` manually. Then jobs get scheduled and processed, as shown by the queue instance’s logs.

My env var is set properly to `database` which is what I use for my jobs.

I read over the “Cron and Queues” Fly Docs but I don’t see anything that I missed.

---

<div class="post-metadata">

**Author:** ![fideloper-fly](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/fideloper-fly/32/3834_2.png) [@fideloper-fly](https://community.fly.io/u/fideloper-fly)\
**Post date:** [October 29, 2022, 5:17pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/4 "2022-10-29T17:17:43Z")

</div>

Thanks! That all makes sense -

In my own instance just now, I did the following - which may help debug.

1. SSH back into the `cron` instance (`ps aux | grep cron` will show if it’s the right instance, you’ll get a result there for `cron -f`)
2. Edit `/etc/cron.d/laravel` and make it look like this (to add logging):

```auto
MAILTO=""
* * * * * webuser /usr/bin/php /var/www/html/artisan schedule:run >> /var/www/html/storage/logs/cron.log 2>&1

```

Wait a minute or 2, and then check `/var/www/html/storage/logs/cron.log`, you should see some output there. In a fresh Laravel app, I see:

```auto
root@3982c846:~# cat /var/www/html/storage/logs/cron.log

   INFO No scheduled commands are ready to run.

   INFO No scheduled commands are ready to run.

   INFO No scheduled commands are ready to run.

```

🔥 **Quick tip** : Once you SSH in, run `bash` to start `bash` shell, which is a little friendlier/easier than the default `sh`

---

<div class="post-metadata">

**Author:** ![josephxanderson](https://avatars.discourse-cdn.com/v4/letter/j/c57346/32.png) [@josephxanderson](https://community.fly.io/u/josephxanderson)\
**Post date:** [October 30, 2022, 3:32am UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/5 "2022-10-30T03:32:47Z")

</div>

OK, so I followed your steps. (Thanks for the `bash` tip!)

I see that the commands are being run as evidenced by checking `/var/www/html/storage/logs/cron.log`:

```auto
  2022-10-30 03:26:01 Running ['artisan' app:fetchAlerts] in background 0ms DONE
  ⇂ ('/usr/bin/php8.1' 'artisan' app:fetchAlerts > '/dev/null' 2>&1 ; '/usr/bin/php8.1' 'artisan' schedule:finish "framework/schedule-2d925ba0962916a46ccd164ca1b9d78d7c96f7ad" "$?") > '/dev/null' 2>&1 & 

```

Interestingly though, there is nothing being added to the `jobs` table in the database and nothing is being logged by the `worker`, meaning jobs aren’t being dispatched to the right place. Note that this all works fine locally.

Running `php artisan tinker` and then running `env("QUEUE_CONNECTION")` returns `"database"`, which is correct. It seems the `cron` instance is missing something…

---

<div class="post-metadata">

**Author:** ![fideloper-fly](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/fideloper-fly/32/3834_2.png) [@fideloper-fly](https://community.fly.io/u/fideloper-fly)\
**Post date:** [October 30, 2022, 12:32pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/6 "2022-10-30T12:32:52Z")

</div>

I suspect other important env vars or secrets are not being picked up.

Here’s something annoying I haven’t found a great work around for yet:

cron doesn’t let Laravel read the VM environment variables that Fly sets.

However, the setup should be running `artisan config:cache` when the VM boots up, so your config should be in the confit cache.

Is your setup using env() directly in code instead of config()? (it’s okay and expected if config files use env(), but using env() elsewhere can lead to issues).

---

<div class="post-metadata">

**Author:** ![josephxanderson](https://avatars.discourse-cdn.com/v4/letter/j/c57346/32.png) [@josephxanderson](https://community.fly.io/u/josephxanderson)\
**Post date:** [October 30, 2022, 3:03pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/7 "2022-10-30T15:03:49Z")

</div>

As soon as you mentioned `artisan config:cache`…

I SSH’d into the cron instance with `fly ssh console -s`, went to the app directory, and ran `artisan config:cache`. And a minute later my jobs started being scheduled and ran!

For some reason, this was clearly not being run when the VM booted. I’m not sure if the issue was with the `Dockerfile` or the `entrypoint.sh` script in the `docker` directory.

I created a new Laravel Fly app to see what was different, following the tutorial [Fly.io Docs: Run a Laravel App](https://fly.io/docs/laravel/) and noticed the `docker` directory has been changed to `.fly`, and the scripts have been changed.

Long story short, I replaced my `Dockerfile` and `docker` directory with the `Dockerfile` and `.fly` directory from the tutorial and… everything started working!

Thanks a lot for your help @fideloper-fly 🙂

---

<div class="post-metadata">

**Author:** ![Yaeger](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/yaeger/32/44383_2.png) [@Yaeger](https://community.fly.io/u/Yaeger)\
**Post date:** [October 30, 2022, 4:09pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/8 "2022-10-30T16:09:42Z")

</div>

I have gotten around this by running `schedule:work` in another process. It’s under a section called “Running scheduler **locally** ” but it’s working fine so far.

> **[Laravel - The PHP Framework For Web Artisans](https://laravel.com/docs/9.x/scheduling#running-the-scheduler-locally)**
>
> Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.

```auto
[processes]
  app = ''
  worker = 'php artisan queue:work'
  scheduler = 'php artisan schedule:work'

```

---

<div class="post-metadata">

**Author:** ![Yaeger](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/yaeger/32/44383_2.png) [@Yaeger](https://community.fly.io/u/Yaeger)\
**Post date:** [October 30, 2022, 4:10pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/9 "2022-10-30T16:10:42Z")

</div>

> I suspect other important env vars or secrets are not being picked up.

This has caused me a whole lot of headaches so I now try to avoid cron all together 😬

---

<div class="post-metadata">

**Author:** ![fideloper-fly](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/fideloper-fly/32/3834_2.png) [@fideloper-fly](https://community.fly.io/u/fideloper-fly)\
**Post date:** [October 30, 2022, 10:55pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/10 "2022-10-30T22:55:27Z")

</div>

it’s really lame behavior.

a .env file would fix it but creating the .env in fly’s (and heroku’s) model also seems annoying - although doable.

---

<div class="post-metadata">

**Author:** ![Brad](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/brad/32/4429_2.png) [@Brad](https://community.fly.io/u/Brad)\
**Post date:** [November 28, 2022, 8:38pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/11 "2022-11-28T20:38:20Z")

</div>

I wrote a guide at [Crontab with Supercronic · Fly Docs](https://fly.io/docs/app-guides/supercronic/) that shows how to get cron running in a Fly container. I think it’s comprehensive enough where you won’t have to try to avoid it 😄

---

<div class="post-metadata">

**Author:** ![Yaeger](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/yaeger/32/44383_2.png) [@Yaeger](https://community.fly.io/u/Yaeger)\
**Post date:** [November 28, 2022, 10:12pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/12 "2022-11-28T22:12:18Z")

</div>

Hi Brad, that’s great! One question about your post.

You wrote that this should be in your `fly.toml`:

```auto
[processes]
  web = "bin/rails fly:server"
  cron = "cron"

```

Shouldn’t the `cron` entry be something like `supercronic crontab`? As such:

```auto
[processes]
  web = "bin/rails fly:server"
  cron = "supercronic crontab"

```

I tried it with `cron` like you wrote in the article but that didn’t seem to work.

---

<div class="post-metadata">

**Author:** ![fideloper-fly](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/fideloper-fly/32/3834_2.png) [@fideloper-fly](https://community.fly.io/u/fideloper-fly)\
**Post date:** [November 29, 2022, 9:44pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/13 "2022-11-29T21:44:46Z")

</div>

I haven’t played with this yet but if you get that working, send me a ping - I’d love to update the Laravel stuff with that if it solves the env issue.

---

<div class="post-metadata">

**Author:** ![Yaeger](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/yaeger/32/44383_2.png) [@Yaeger](https://community.fly.io/u/Yaeger)\
**Post date:** [November 29, 2022, 9:57pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/14 "2022-11-29T21:57:00Z")

</div>

I actually created a Laravel project with this yesterday, here’s a public repo showing what I did so far:

> **[GitHub - marketphase/laravel-with-supercronic](https://github.com/marketphase/laravel-with-supercronic)**
>
> Contribute to marketphase/laravel-with-supercronic development by creating an account on GitHub.

I didn’t thoroughly test it yet, but it seems to work fine!

EDIT: I just pushed some more changes that were still on my local machine that included running `schedule:run`.

---

<div class="post-metadata">

**Author:** ![Brad](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/brad/32/4429_2.png) [@Brad](https://community.fly.io/u/Brad)\
**Post date:** [December 1, 2022, 5:02pm UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/15 "2022-12-01T17:02:45Z")

</div>

> [@Yaeger](#):
>
> Shouldn’t the `cron` entry be something like `supercronic crontab`? As such:
> 
> ```auto
> [processes]
> web = "bin/rails fly:server"
> cron = "supercronic crontab"
> 
> ```
> 
> I tried it with `cron` like you wrote in the article but that didn’t seem to work.

Yep! You’re right, I think I feel asleep at the wheel. I’m going to update the docs. Thanks for running through them and sorry for sending you down a rabbit hole.

---

<div class="post-metadata">

**Author:** ![Jane\_smith](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@Jane\_smith](https://community.fly.io/u/Jane_smith)\
**Post date:** [June 12, 2024, 11:56am UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/16 "2024-06-12T11:56:36Z")

</div>

To resolve the issue with your Laravel cron job not executing, ensure your setup follows these steps:

**1. Verify SSH Access:** Ensure your hosting provider grants SSH access, as deploying Laravel with tools like FileZilla is not recommended due to dependency management issues.

**2. Correct Cron Configuration:**

- fly.toml: Make sure your cron command is correctly defined.
- Dockerfile: Ensure the cron setup is properly integrated.

Here’s a sample Dockerfile section for setting up cron:

```dockerfile
RUN composer install --optimize-autoloader --no-dev \
    && mkdir -p storage/logs \
    && php artisan optimize:clear \
    && chown -R webuser:webgroup /var/www/html \
    && sed -i 's/protected \$proxies/protected \$proxies = "*"/g' app/Http/Middleware/TrustProxies.php \
    && echo "MAILTO=\"\"\n* * * * * webuser /usr/bin/php /var/www/html/artisan schedule:run" > /etc/cron.d/laravel \
    && chmod 0644 /etc/cron.d/laravel \
    && crontab /etc/cron.d/laravel \
    && cp docker/nginx-websockets.conf /etc/nginx/conf.d/websockets.conf \
    && cp docker/entrypoint.sh /entrypoint \
    && chmod +x /entrypoint

```

**3. Test Cron Manually:**

- Run the cron manually to ensure it’s working: `php artisan schedule:run`.
- Check the logs in `storage/logs/laravel.log` to see if there are any errors.

**4. Common Issues:**

- Ensure the cron service is running: `service cron start`.
- Permissions: Ensure the cron file and directories have the correct permissions.
- Check the crontab syntax and paths.

You can refer to this article for a detailed guide on [Laravel cron jobs](https://devrims.com/blog/laravel-cron-job-task-scheduling/) and troubleshooting.

---

<div class="post-metadata">

**Author:** ![kathrynannetan](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kathrynannetan/32/41714_2.png) [@kathrynannetan](https://community.fly.io/u/kathrynannetan)\
**Post date:** [June 13, 2024, 10:52am UTC](https://community.fly.io/t/laravel-scheduler-cron-not-working/8304/17 "2024-06-13T10:52:28Z")

</div>

Hello @Jane_smith and welcome to [Fly.io](http://Fly.io)!

Thank you for sharing the steps above, they’re very helpful, specially the common issues part. Also! Here at Fly, we already have a cron daemon pre-configured to run the scheduler command, so running the scheduler on a Laravel Fly app is as quick as declaring the cron daemon as a [Fly process](https://fly.io/docs/reference/configuration/#the-processes-section) in its fly.toml file:

```toml
[processes]
  app = ""
  cron = "cron -f"

```

[Here’s the page](https://fly.io/docs/laravel/the-basics/cron-and-queues/) you can read to learn more about the setup.
