>_ IzzyLab

Hands-on task

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

The crontab environment: PATH and mail

medium~7 min

The reporting tools live in /opt/tools/bin, and rotate-reports calls its neighbour report-gen without a path — in cron's stripped-down environment it won't be found. In root's crontab set the variables MAILTO=cron-alerts@example.org and a PATH that includes /opt/tools/bin as well as the system /usr/bin and /bin. Below them add a job that runs rotate-reports every day at 02:30 — write the command without a path and let cron find it through PATH.

What you'll practise:
  • cron
  • crontab
  • environment variables
  • PATH
  • MAILTO

Topic breakdown

cron jobs don't run from your session but with a minimal environment: PATH is usually limited to /usr/bin:/bin, so commands from /usr/local/bin or /opt/…/bin, and scripts that call them by name, fail in cron with "command not found". The environment is set with NAME=value lines right in the crontab — without export, and they apply to all jobs further down the file. MAILTO gives the address cron sends job output to; an empty value turns the mail off. To check whether a command will be found in a given environment, use env -i PATH=… sh -c 'command -v name'.

What's checked

  1. MAILTO=cron-alerts@example.org is set
  2. the crontab PATH finds the reporting tools
  3. rotate-reports runs at 02:30
  4. PATH is set above the job

What it looks like