>_ IzzyLab

Hands-on task

This task is step 4 of the course “Cron: scheduled jobs”

Cron output to a log, appending

medium~7 min

Add a job to the root crontab that runs /usr/local/bin/collect.sh every minute, sending its output (stdout and stderr) to the log /var/log/collect.log in append mode, so earlier entries are not overwritten. The cron daemon is already running — after the edit, wait for the first run.

What you'll practise:
  • cron
  • every-minute run
  • appending >>
  • 2>&1
  • logging

Topic breakdown

cron doesn't write job output anywhere by itself (by default it mails it to the owner), so logging is set up right in the schedule line with a redirection. >> appends to the end of the file, keeping the history, while > would overwrite it on every run. The 2>&1 construct sends the error stream (fd 2) to wherever stdout (fd 1) is already going — important so that the script's errors end up in the log too. Asterisks in all five time fields mean a run every minute.

What's checked

  1. runs every minute
  2. stdout and stderr are appended to the log
  3. cron has already written the job's output to the log

What it looks like