Back to lessons

Linux Survival Basics

Turn Cron Into a Readable Table

Raw crontab lines are hard to scan during an incident, especially with long commands.

Command

crontab -l | awk 'NF && $1 !~ /^#/ {printf "%-16s %s\n", $1" "$2" "$3" "$4" "$5, substr($0,index($0,$6))}'

What changed

Nothing changes. awk formats the five schedule fields separately from the command.

Danger

safe

When to use it

Use when reviewing several cron jobs and you need a quick schedule-to-command map.

When not to use it

Do not use it for /etc/cron.d files without adapting for the user field after the schedule.

Undo or recovery

No undo needed because the command is read-only.

Expected output

Aligned schedule expressions next to their command strings.

demo script

Disposable terminal steps

  1. crontab -l
  2. crontab -l | awk 'NF && $1 !~ /^#/ {printf "%-16s %s\n", $1" "$2" "$3" "$4" "$5, substr($0,index($0,$6))}'

simulated output

What it looks like

disposable vessel
::fixture-ready::
$ crontab -l
# user crontab for demo
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
5 * * * * /usr/local/bin/check-disk >> /var/log/cron-disk.log 2>&1
17 2 * * * /usr/local/bin/backup-db
0 4 * * 0 /usr/local/bin/report-weekly | /usr/bin/mail -s report ops@example.invalid
::exit-code::0
$ crontab -l | awk 'NF && $1 !~ /^#/ {printf "%-16s %s\n", $1" "$2" "$3" "$4" "$5, substr($0,index($0,$6))}'
SHELL=/bin/bash     SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin     PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
5 * * * *        /usr/local/bin/check-disk >> /var/log/cron-disk.log 2>&1
17 2 * * *       /usr/local/bin/backup-db
0 4 * * 0        /usr/local/bin/report-weekly | /usr/bin/mail -s report ops@example.invalid
::exit-code::0

YouTube Short

Make cron readable.

Cron fields are compact until you are tired. Split the schedule from the command and the review gets faster.

LinkedIn hook

Cron is easier to debug when the schedule and command stop blending together.

Question: Do you mentally parse cron fields, or do you format them before reviewing?

experiments

A/B tests to run

Metric: average_view_duration

A: Cron is dense. Format it first.

B: Make cron readable before debugging it.