Running Laravel's scheduler without setting up CRON jobs

Scheduler is a tiny shell script to run Laravel's scheduler every 60 seconds, without having to configure any CRON jobs:


function scheduler () {
    while :; do
        php artisan schedule:run
    	echo "Sleeping 60 seconds..."
        sleep 60

To install, add the function to the end of your ~/.bashrc or ~/.zshrc file and start a new shell session. Run scheduler in your project's root directory to start and press ctrl+c to stop.

But why?

CRON is a great tool to periodically run some commands. For example, in Laravel projects we use the crontab file to execute the framework's scheduler every minute. However, when working on local projects you probably don't want to configure CRON jobs for each and every one of your projects. Keeping track of any number of projects in the crontab file is a pain and on top of that running random CRON jobs in the middle of a big refactor is a recipe for disaster.

Usually you'll end up manually running php artisan schedule:run (or the equivalent in your framework of choice). However, sometimes you just want to use your application without having to manually run the scheduler every 60 seconds. Maybe you're giving a demo or you just want to sit back and look at your fancy realtime dashboard. This is where the scheduler command comes in handy.