Cron with Lando

jcandan

James Candan

Posted on February 27, 2020

Cron with Lando

Update Feb 28, 2020: It turns out that cron runs within it's own limited environment. See below for more details.

In Laravel, we want the following cron configuration to fire the framework's scheduler. Place this in a file somewhere within your project code base. I went with resources/config/cron.txt.

* * * * * php /app/artisan schedule:run

Note: The cron daemon was designed in such a way that it does NOT execute commands within your normal shell environment. That means your $PATH will not match your container's, and your php.ini memory limit may not be set. That said, we need to supply the full path from within the Lando container to php. Typically, this will be /usr/local/bin/php. In that case, use the following:

* * * * * /usr/local/bin/php -d memory_limit=-1 /app/artisan schedule:run

Next, we need to get cron installed. You'll want to perform this as root during build.

    build_as_root:
      - apt-get update -y
      - apt-get install cron -y

Now, we're ready to fire the cron service. Because cron services all users in a system, it must be started with root priviliges.

    run_as_root:
      - service cron start

Here we are, born to be kings; we're the princes of the the universe.

Oh, sorry, off on a tangent. Okay, we can now store our cron config with crontab.

    run:
      - crontab /app/resources/config/cron.txt

Put this altogether, and you will have arrived.

name: my-project
recipe: laravel
config:
  webroot: public
services:
  appserver:
    build_as_root:
      - apt-get update -y
      - apt-get install cron -y
    run_as_root:
      - service cron start
    run:
      - crontab /app/resources/config/cron.txt

Check that things have gone as planned.

$ lando ssh
www-data@ba70770a45a1:/app$ service cron status
[ ok ] cron is running.
www-data@ba70770a45a1:/app$ crontab -l
* * * * * php /app/artisan schedule:run
www-data@ba70770a45a1:/app$ 

Enjoy.

💖 💪 🙅 🚩
jcandan
James Candan

Posted on February 27, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Cron with Lando
docker Cron with Lando

February 27, 2020