Run something on a schedule#
You will end up with a script the router runs on an interval or at the times you choose, and that survives an image upgrade. The router's own task scheduler does the running; System → Settings tab → Scheduled tasks tells it what to run and when.
Before you start#
- The Operator role, and a licence: scheduled tasks are part of the router's configuration.
- The script, and a shell on the router to put it there: the web interface does not create or edit files on the router. Turn SSH on if you have not.
- Somewhere for it to live. Put it in
/config/scripts./configis the persistent partition, so anything there survives an image upgrade;/usrand/optcome from the image and are replaced.
Step 1 — Put the script on the router#
Over SSH, or at the console:
sudo mkdir -p /config/scripts
sudoedit /config/scripts/nightly-check.sh
sudo chmod 755 /config/scripts/nightly-check.sh#!/bin/sh
set -eu
used=$(df -P /config | awk 'NR == 2 { print $5 }')
logger -t nightly-check "/config is $used full"logger puts the line in the router's log, which is where you will look for it —
the Logs page reads the same log.
Step 2 — Schedule it#
Scheduled tasks → Add task.
| Field | Takes |
|---|---|
| Name | Letters, digits, dots, hyphens and underscores, beginning with a letter or a digit, such as nightly-check. |
| Schedule | Repeat at an interval, or Cron schedule. |
| Interval | "Minutes, or a number with m, h or d." — 30, 30m, 6h, 1d. At most 60 minutes, 24 hours or 31 days. |
| Cron schedule | "Minute, hour, day of month, month and day of week." Five fields, such as 15 3 * * *. |
| Program | The full path of the script, such as /config/scripts/nightly-check.sh. "It must exist on the router and be executable." |
| Arguments | Anything the program takes, such as --verbose /config/scripts/input. |
Add task adds "Add scheduled task “nightly-check”" to the pending changes, with the schedule and the program beneath it: "Every 1h, runs /config/scripts/nightly-check.sh".
Step 3 — Apply and wait#
Apply it from the bar at the bottom of the page with Apply changes, like any other change. If the script is missing or not executable, applying fails and the bar says why.
A task does not run when it is applied: it first runs when its schedule next comes round,
so a task every 1d tells you nothing until tomorrow. Test with a short interval and
lengthen it afterwards.
Check it worked#
Scheduled tasks lists the task, its schedule — "Every 1h", or the cron fields — and the program with its arguments.
Logs shows whether it ran: type the tag you gave logger into Filter lines…. The
page reads the most recent lines of the log; on a busy router, choose more lines from the
list beside the filter.
Things that will bite#
The script runs as root. Everything in /config/scripts should be readable only by root
if it holds anything sensitive, and chmod 700 is the safe default for one that carries a
credential.
Output goes nowhere unless you send it somewhere. A task that prints to standard output
prints to nothing. Use logger, or redirect to a file under /config.
A failing task is silent. Nothing alerts on a non-zero exit. If the task matters, make it log its own failure, and watch for that line — Ship logs off the box is how you get it off the router.
/tmp is not persistent. Fine for scratch, no use for state.
What this is not for#
- Backups. The Backup page downloads a complete, encrypted backup and keeps daily snapshots on the router, and a copy kept only on the disk you are protecting is not a backup — Take a backup that is actually complete.
- Applying configuration on a timer. That is the reconcile loop, which applies with auto-revert and records what it does in the audit log.
Undoing it#
Click the task, choose Delete task — "Remove scheduled task “nightly-check”" — and apply.
The script file stays where it is; delete it at a shell if you want it gone.
In Advanced mode#
With Advanced mode on (Account → Preferences), Review changes lists these changes as the
router's commands, under system task-scheduler task.