Sub topic 15
Testing and Operating Scheduled Jobs
This chapter assumes you are comfortable with the Linux command line, shell scripts, users and permissions, processes, and reading logs. It also assumes you have already seen cron and systemd timer syntax. The focus here is the operating routine: prove that a job works, inspect what happened, and detect problems before they become incidents.
Test the Job Before Scheduling It
A scheduler adds timing and environment differences. Test the job itself first.
Run the script manually as the same user that will run it. Do not test a backup as root if the service runs as backup. The two users may have different permissions, home directories, PATH values, and access to credentials.
For a systemd service, start the service directly:
sudo systemctl start backup.service
sudo systemctl status backup.service --no-pager
sudo journalctl -u backup.service --since "10 minutes ago" --no-pagerA successful start proves only that this particular run completed. It does not prove that the timer is enabled or that the next activation will happen.
Check the timer separately:
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers backup.timer
systemctl show backup.timer -p ActiveState -p NextElapseUSecRealtime -p LastTriggerUSecThe timer should be active, and NextElapseUSecRealtime should contain a future time. If the timer is inactive, the service will not run on schedule.
For a cron job, copy the command into a shell and run it as the intended account. Use an explicit environment while testing:
sudo -u backup env -i \
HOME=/var/lib/backup \
PATH=/usr/local/bin:/usr/bin:/bin \
/usr/local/bin/backup.shThis catches scripts that accidentally depend on an interactive shell, a user's aliases, or a working directory that cron does not provide.
Use a Repeatable Operating Checklist
After installation and during routine checks, verify each layer.
| Check | What it proves | Useful command |
|---|---|---|
| Script exit status | The job reports success or failure | `echo $?` |
| Recent output | The job left evidence | `journalctl -u backup.service` |
| Scheduler state | The schedule is enabled | `systemctl is-enabled backup.timer` |
| Next activation | The scheduler has a future run | `systemctl list-timers` |
| Result artifact | The job produced a usable result | `test -s /var/backups/db.sql.gz` |
| Resource use | The job is not exhausting the host | `ps`, `df`, `free` |
Test failure paths deliberately. Temporarily use an unwritable output directory, an invalid database name, or a short timeout in a test environment. Confirm that the command returns a nonzero exit status, that temporary files are removed, and that monitoring reports the failure.
Do not test by deleting production backups or changing production data. Use a disposable destination and a test account whenever possible.
Operate the Job After a Change
When changing a script, unit, schedule, user, or environment:
- Validate the schedule or unit syntax.
- Run the job manually with the production-like account.
- Reload systemd after changing unit files.
- Trigger one controlled run.
- Read the logs and inspect the output.
- Confirm the next scheduled activation.
- Keep the old configuration available until the new run succeeds.
Use a small runbook so another administrator can repeat the process. Record the expected runtime, output location, lock behavior, alert destination, and safe rollback steps. Review disk space and log rotation as well. A job can fail because its output disk is full even when the script has not changed.
Exercise
Exercise
You run systemctl list-timers backup.timer and see a future next run, but the backup file is missing. What should you check next?
Show answer
Start with systemctl status backup.service and journalctl -u backup.service. Then run the service manually as the backup user and check its exit status, output path, permissions, disk space, and lock state.