Skip to content

automation

1 post with the tag “automation”

Automating Homelab Tasks with Cron

In a homelab, there are many tasks that benefit from automation: backups, log rotation, system updates, or even just checking the status of services. cron is the classic Unix utility for scheduling commands to run periodically. This post will guide you through setting up and managing cron jobs.

A cron job entry (a “crontab” entry) consists of five time fields followed by the command to be executed. The fields are:

* * * * * command-to-be-executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

For common schedules, cron offers special strings:

StringDescriptionEquivalent
@rebootRun once after rebootN/A
@yearlyRun once a year0 0 1 1 *
@annuallySame as @yearly0 0 1 1 *
@monthlyRun once a month0 0 1 * *
@weeklyRun once a week0 0 * * 0
@dailyRun once a day0 0 * * *
@midnightSame as @daily0 0 * * *
@hourlyRun once an hour0 * * * *

Each user has their own crontab. To edit it, use:

Terminal window
crontab -e

This will open your crontab in a text editor (usually nano or vim). Add your job entries at the end of the file.

To view your current cron jobs:

Terminal window
crontab -l

To remove all your cron jobs:

Terminal window
crontab -r

Let’s say you have a backup script at /usr/local/bin/backup-homelab.sh. To run it every day at 2:00 AM:

0 2 * * * /usr/local/bin/backup-homelab.sh >> /var/log/homelab-backup.log 2>&1
  • 0: Minute 0
  • 2: Hour 2 (2 AM)
  • *: Every day of the month
  • *: Every month
  • *: Every day of the week
  • >> /var/log/homelab-backup.log 2>&1: Redirects both standard output and standard error to a log file.

To check for system updates every hour:

0 * * * * sudo apt update > /dev/null 2>&1
  • 0: Minute 0
  • *: Every hour
  • > /dev/null 2>&1: Discards all output to prevent excessive emails from cron.

Run a script once after every system reboot:

@reboot /usr/local/bin/start-homelab-services.sh

Cron jobs run with a minimal set of environment variables. If your script relies on specific PATH settings or other variables, you need to define them within the crontab or explicitly in your script.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 3 * * * /usr/local/bin/my-script.sh

Alternatively, specify the full path to all commands in your script.

IssuePossible CauseSolution
Job not runningIncorrect time syntax, script pathCheck crontab -l, verify script path
Job fails silentlyEnvironment variables, permissionsRedirect output to a log file, check script permissions (chmod +x)
No outputOutput redirected to /dev/nullRemove redirection temporarily to debug
Email spamJob produces outputRedirect output to /dev/null or a log file

Cron activity is often logged in /var/log/syslog or /var/log/cron.

Terminal window
sudo grep CRON /var/log/syslog
  1. Use full paths: Always specify the full path to commands and scripts (e.g., /usr/bin/python3 instead of python3).
  2. Log output: Redirect output to a log file (>> /path/to/log.log 2>&1) to capture errors and verify execution.
  3. Test scripts: Run your scripts manually from the command line before adding them to cron.
  4. Minimal privileges: Run cron jobs as the user with the least necessary privileges.
  5. Comments: Add comments to your crontab (# This is a comment) to explain complex entries.

cron is a robust and reliable tool for automating tasks on your homelab. By understanding its syntax and following best practices, you can free yourself from repetitive manual work and ensure your systems are maintained efficiently.

Automate responsibly!