A Step-by-Step Guide to Running Cron Jobs locally for Laravel on macOS
Cron jobs are scheduled tasks that run at specified intervals, and they’re incredibly useful for automating repetitive tasks. Whether you’re a developer, system administrator, or just a macOS user looking to streamline some processes, understanding how to set up and manage cron jobs can save you a lot of time. In this article, we’ll walk through the steps to run cron jobs on macOS.
Step 1: Open Terminal
First, you need to open Terminal. You can find it by searching for “Terminal” in Spotlight (Cmd + Space) or navigating to Applications > Utilities > Terminal.
Step 2: Open the Crontab File
The crontab file is where you define your cron jobs. To edit your crontab file, use the following command:
crontab -e
If this is your first time editing the crontab file, you’ll be prompted to choose an editor. You can select nano for simplicity.
Step 3: Understand Cron Job Syntax
Cron jobs are defined using a specific syntax:
* * * * * script to run
- - - - -
| | | | |
| | | | ----- Day of the week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
Each field specifies the time and frequency of the job.
Step 4: Add Your Cron Job
To add a cron job, simply add a new line to the crontab file with the appropriate syntax. For example, to run a backup script every day at midnight, you would add:
Edit your crontab by running then add the line in the file
crontab -e
* * * * * cd "/path/to/your/laravel-project" && "/path/to/your/php" artisan schedule:run >> /dev/null 2>&1
Then save the crontab entry by typing:
:wq
And hit enter.
Step 5: Verify Your Cron Job
crontab -l
It should display your crontab entry.
Step 6: Removing / Pausing cron entry
Start by editing the crontab again (crontab -e)
#* * * * * cd "/path/to/your/laravel-project" && "/path/to/your/php" artisan schedule:run >> /dev/null 2>&1
Just add a # to comment-out an entry :)
Enjoy!