Skip to content
Lab 4 of 4

Scheduled Jobs: Rotate Kestrel's Log with cron

Kestrel's request log has never been rotated, and the last time a disk filled, orders failed for 40 minutes. Write a rotation script, schedule it nightly in /etc/cron.d as the service account, prove it under cron's own bare environment, and make sure two runs can never overlap.
  • Intermediate
  • 44 min
  • Bash
  • cron
  • Automation
  • Kestrel

Overview

In November 2025 the receipts disk filled and Kestrel took no orders for 40 minutes — the Diwali outage. Simran points at the next disk to fill: /var/log/kestrel/orders.log, which the API appends to on every request and nobody has ever rotated.

You will write /usr/local/bin/kestrel-rotate-logs, which keeps five old copies, and schedule it every night at 03:15 as the kestrel service account, in /etc/cron.d. Then comes the part most people skip: proving the job works the way cron will run it — with almost no environment, no terminal, and output that vanishes unless you send it somewhere — without waiting until 03:15 to find out. Finally you make sure that if one run is slow, the next one cannot start on top of it.

The cron daemon is already running on this machine.

What you will learn

  • Rotate a log by renaming, and know when renaming is safe
  • Write an /etc/cron.d entry with a schedule, a user and a command
  • Know the rules that make cron silently ignore a file
  • Reproduce cron's environment to test a job before it is scheduled to run
  • Prevent overlapping runs with flock -n

Before you start

  • Bash That Fails Loudly first, or comfort with set -euo pipefail and quoting
  • You can edit files with sudo nano

Tasks

  1. 01Rotate by renaming
  2. 02Schedule it in /etc/cron.d
  3. 03Prove it under cron's environment
  4. 04Never two at once: flock