schedule-monitor:sync

php artisan schedule-monitor:sync synchronizes the schedule defined in your code with the monitored_scheduled_tasks table and, when configured, with the cron checks of an Oh Dear monitor. It is the command that decides which tasks are monitored, and it must run after every change to the schedule, typically on every deployment. This page describes exactly what it creates, updates, and deletes in both modes.

Property Value
Signature schedule-monitor:sync {--keep-old}
Description Sync the schedule of the app with the schedule monitor
Options --keep-old: non-destructive sync
Writes monitored_scheduled_tasks (and, through cascading deletes, monitored_scheduled_task_log_items); Oh Dear cron checks

Usage

# Full sync: database and Oh Dear mirror the current schedule
php artisan schedule-monitor:sync

# Non-destructive: only add and update, never remove
php artisan schedule-monitor:sync --keep-old

Step 1: Syncing with the Database

  1. The schedule is loaded and filtered to tasks that run in the current environment (app.env).
  2. Tasks marked doNotMonitor() and tasks without a name are dropped. Of tasks sharing a name, only the first is kept.
  3. For each remaining task, a row is created or updated by name with these values:

    Column Value
    type command, job, closure, or shell
    cron_expression The event's cron expression, such as 0 2 * * *
    timezone The event's timezone, such as UTC or Europe/Brussels
    grace_time_in_minutes graceTimeInMinutes() or the configured default
    ping_url Set to null for tasks marked doNotMonitorAtOhDear(); otherwise left unchanged in this step
  4. Without --keep-old, every row whose task was not part of this sync is deleted. The foreign key on the log items cascades, so their history is deleted too. This covers tasks that were removed from the schedule, renamed, marked doNotMonitor(), or restricted to another environment.

The last_*_at timestamps of existing rows are untouched, so a schedule change (for example from hourly to daily) keeps the task's history.

Step 2: Syncing with Oh Dear

If schedule-monitor.oh_dear.monitor_id is empty, this step is skipped with a yellow notice:

Not syncing schedule with oh dear because not monitor_id is not set in the oh-dear config file.
Learn how to set this up at https://ohdear.app/docs/general/cron-job-monitoring/php#cron-monitoring-in-laravel-php.

Otherwise, the tasks to send are the synced tasks that are not marked doNotMonitorAtOhDear(). Each is described to Oh Dear with:

Field Value
name The task name
type cron
cron_expression The task's cron expression
grace_time_in_minutes The task's grace time
server_timezone The task's timezone
description Empty string

Full Sync (Default)

All tasks are sent in one request to Oh Dear's sync endpoint (POST monitors/{monitor_id}/cron-checks/sync). Oh Dear makes the monitor's cron checks match this list: checks are created and updated, and every other cron check on that monitor is removed, including checks you created by hand or from another application.

--keep-old Sync

Only tasks whose row has no registered_on_oh_dear_at yet are sent, each in its own request (POST monitors/{monitor_id}/cron-checks). Nothing is removed at Oh Dear, and existing checks are not updated: if you change the cron expression or grace time of a task that is already registered, Oh Dear keeps the old values. Use --keep-old when the Oh Dear monitor also holds cron checks for jobs outside this Laravel application.

After Oh Dear Responds

For each cron check returned by Oh Dear, the package finds the row with the same name and:

  • stores the check's ping URL in ping_url. If endpoint_url is configured, the ping URL is built as that endpoint plus / plus the check's UUID instead;
  • sets registered_on_oh_dear_at to the current time, only if it was not set before.

From then on, every finish and failure of the task is pinged to its ping_url (details).

If the Oh Dear API call fails, the command stops with an exception such as Failed to POST monitors/12345/cron-checks/sync: .... The database step has already completed at that point, so re-running the command after fixing the problem is safe.

Output

A successful full sync with Oh Dear prints output similar to:

Start syncing schedule...
Start syncing schedule with database...
Start syncing schedule with Oh Dear...
Successfully synced schedule with Oh Dear!
All done! Now monitoring 12 scheduled tasks.
Run php artisan schedule-monitor:list to see which jobs are now monitored.

The count is the total number of rows in monitored_scheduled_tasks after the sync. With --keep-old it includes rows that are no longer in the schedule.

Full Sync vs. --keep-old

Behavior Full sync --keep-old
New tasks get a row Yes Yes
Existing rows get new cron expression, timezone, grace time Yes Yes
Rows of removed or renamed tasks (and their log items) Deleted Kept
New tasks registered at Oh Dear Yes Yes
Changed tasks updated at Oh Dear Yes No
Oh Dear checks not in this schedule Removed Kept
Ping URLs refreshed for all tasks Yes Only for newly registered tasks

Rows kept by --keep-old still receive log items if a task with that name runs (the listeners only look for a row by name), and they still have their ping_url, so they can still ping Oh Dear.

Running the Sync on Deploy

php artisan migrate --force
php artisan schedule-monitor:sync

Run it after the new code is in place and the configuration is cached (if you cache it), so it sees the new schedule and the right environment. Running it on every deploy is cheap: it is one query per task plus one Oh Dear request.

Environments and Shared Oh Dear Monitors

The sync only sees the tasks of the current environment, and a full sync makes Oh Dear match exactly those tasks. Two consequences:

  • Do not point several environments at the same Oh Dear monitor. A full sync from staging would replace production's cron checks with staging's. Use a separate monitor per environment, or leave OH_DEAR_MONITOR_ID empty outside production.
  • Do not share the monitoring tables between environments. Each environment's sync deletes the rows of tasks it does not run.

Stopping Oh Dear Monitoring

Removing OH_DEAR_MONITOR_ID makes future syncs skip Oh Dear, but it does not clear the ping_url already stored on each row, and pings do not need the API token. Tasks keep pinging their existing checks. To stop the pings, either mark the tasks with doNotMonitorAtOhDear() and sync (which sets ping_url to null), or clear the column yourself:

use Spatie\ScheduleMonitor\Models\MonitoredScheduledTask;

MonitoredScheduledTask::query()->update(['ping_url' => null, 'registered_on_oh_dear_at' => null]);

See Also