← Other topics

Forge Under the Hood - Scheduler (Cron)

Video Notes

Let’s learn what Forge is doing on your server with its Scheduler feature. You can use this information when troubleshooting or if you decide to cancel your Forge subscription and manage your own servers.

Scheduler = Cron

Behind Forge’s Scheduler feature is Cron, a time-based job scheduling utility on Unix-like operating systems.

Every job you create with Forge’s Scheduler is recorded as an expression/line written in the config file /etc/crontab.

For example, here are the jobs I have listed in Forge’s Scheduler:

And here are those jobs in the Cron config file (/etc/crontab):

Cron syntax

Here’s the breakdown of the syntax for a Cron expression:

Cron time

Every cron expression starts with 5 digits that indicate how often that job should run.

  • Digit 1 represents the minutes (0 - 59)
  • Digit 2 represents the hour (0 - 23)
  • Digit 3 represents the day of month (1 - 31)
  • Digit 4 represents the month month (1 - 12) OR jan,feb,mar,apr ...
  • Digit 5 represents the day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

For example, consider this expression:

16 10 * * * root /usr/local/bin/composer self-update > /home/forge/.forge/scheduled-1010649.log 2>&1

This job should run at 10:16am of every month/day/year because...

  • 16 indicates at 16 minutes
  • 10 indicates at hour 10
  • * indicates every month
  • * indicates every day
  • * indicates every year

To make sure you’re correctly understanding cron times, plug them into a tool like this....

To easily generate cron times, I just ask ChatGPT, e.g. In cron, how can I indicate that the job should run on the first and 15th of every month at 1pm?

Laravel Task Scheduling

If you’re working in a Laravel application, you should use their Task Scheduling feature. With Task Scheduling, you’ll have just a single cron job that runs every minute and invokes the command php artisan schedule:run. This command will then invoke any commands you have defined in /app/Console/Kernel.php, for example:

class Kernel extends ConsoleKernel
{
    /**
     * Define the application’s command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('send-video-reminders')->dailyAt('6:00');
        $schedule->command('send-digests')->dailyAt('6:30');
        $schedule->command('s3-backup')->hourly();
        $schedule->command('email-cron-log')->dailyAt('7:00');
        $schedule->exec('node /home/forge/alert.js')->daily(); 
    }

The idea here is to centralize your jobs within your Laravel application where they can be tracked in version control with the rest of your code, and you have access to the user-friendly syntax offered by Laravel’s Schedule class.

Working with the cron daemon

Here are some useful commands to be aware of when working with cron:

Check the status of the cron daemon:

> sudo systemctl status cron

Stop the cron daemon:

> sudo service cron stop

Start the cron daemon:

> sudo service cron start

Note that you do not have to restart the cron daemon when editing your crontab file. The cron daemon automatically detects changes in the crontab file and applies them without requiring a restart.

Resources

If this info helped you out you can say thanks and support future content by clicking my Amazon affiliate link: https://amzn.to/3UtgnYk. If you make a purchase on Amazon within 24 hours of clicking the link I may receive a micro-commission and it costs you nothing extra. Any income from these sales goes directly to supporting me in making new videos and guides. Thank you for your support!

← Other topics