Adding Crontab to mac for testing scheduled jobs in Laravel

Jan 2024

I needed to test some scheduled jobs for a Laravel application, but I struggled to set up crontab on my local machine. In order to get the scheduler to run, you have to manually use the operating system's crontab feature.

This is what worked for me:

Getting set up with Crontab

In a terminal window, run crontab -e to enter a vim editor.

I believe crontab runs out of the root folder of the operating system, so everything needs to be either an absolute path, or relative to the root folder.

Setting up the command

The exact command I used was this... * * * * * /opt/homebrew/bin/php /Users/stephenbateman/web/yli/registrations-new/artisan schedule:run >> /dev/null 2>&1

To break that down, it looks like this: * * * * * PATH_TO_PHP ABSOLUTE_PATH_TO_ARTISAN schedule:run >> /dev/null 2>&1

In order to get the PHP path, I typed in which php and copied that value.

It's important to note that the second path is to the project's artisan folder, not the root of the project itself. There's probably a lot of ways to do this, but this is what worked for me.

Closing VIM

In case VIM is not your daily driver, type i to enter insert mode, paste your command, press escape to exit insert mode, and the :x to save and close.

I hope this helps!

Turning off crontab

Once I was done, I did crontab -e again and added a # before the command to turn it off.