Loading...

Lab 12: Schedule and Inspect Cron Jobs

Schedule a recurring backup task using a user crontab and verify that it is installed correctly. Validate the job with crontab inspection, identify where cron activity is logged, and understand the role of system-wide cron directories.

services troubleshooting core

Scenario

Your team needs regular backups to run automatically. You have been asked to schedule a daily backup job using cron, confirm it is present in the user’s crontab, and identify where you would check for evidence when troubleshooting job execution.

Operator context

Cron failures are often simple: the job was never installed, the schedule was wrong, or logs were never checked. This lab builds the habit of proving each piece with command output.

Objective

  • List the current user’s cron jobs.
  • Open the user’s crontab for editing.
  • Create a daily job to run /usr/local/bin/backup.sh at 02:00.
  • Verify the job is installed using crontab -l.
  • Identify where cron messages are logged on common distros.
  • Explain the purpose of /etc/cron.d/.

Concepts

  • A user crontab is per-account scheduling managed by crontab, not a file you edit directly.
  • Always verify install with crontab -l. If it is not listed, it will not run.
  • Cron runs with a minimal environment. Use full paths and do not assume shell variables are present.
  • Log destinations vary by distro and logging stack. Know where your platform records cron execution.
  • /etc/cron.d/ is for system-managed cron files that include an explicit user field.

Walkthrough

Step 1: List the current user’s cron jobs.
Command
crontab -l

This confirms whether the user already has scheduled jobs. If no crontab exists, you will see a message indicating the crontab is empty or missing.

# Example (no jobs yet):
crontab: no crontab for vonds
Step 2: Open the user’s crontab for editing.
Command
crontab -e

This opens the per-user crontab in an editor. The editor may vary by system configuration, but the goal is the same: install a cron entry under the user’s account.

# Example message:
Editing crontab using nano. Use Ctrl+O to save, Ctrl+X to exit.
Step 3: Add a daily backup job at 02:00.
Command
0 2 * * * /usr/local/bin/backup.sh

Cron format is MIN HOUR DOM MON DOW CMD. This entry runs at minute 0 of hour 2 every day of the month, every month, regardless of day-of-week.

# Confirm the entry is present in the crontab file you saved:
0 2 * * * /usr/local/bin/backup.sh
Step 4: Verify the job is installed.
Command
crontab -l

Always re-list the crontab after editing. This proves your entry was installed and helps catch editing mistakes before you wait for the schedule to trigger.

0 2 * * * /usr/local/bin/backup.sh
Step 5: Identify where cron messages are logged.
Command
# Common locations (distro-dependent):
/var/log/syslog
/var/log/cron.log

Log locations vary by distro and logging configuration. Debian/Ubuntu commonly record cron activity in /var/log/syslog. Some systems log cron to a dedicated /var/log/cron.log or include cron events in a general messages log.

# Example evidence patterns you might see:
CRON[12345]: (vonds) CMD (/usr/local/bin/backup.sh)
Step 6: Understand what /etc/cron.d/ is for.
Command
ls -l /etc/cron.d/

/etc/cron.d/ holds system-wide cron jobs that are managed as separate files. Unlike user crontabs, entries here typically include an explicit user field so a job can run as a chosen account.

# You should see one or more cron definition files (example):
anacron
e2scrub_all
php

Common breakpoints

The job is not listed after editing

The editor may have exited without saving, or the crontab may have failed validation. Re-run crontab -e, save, then confirm with crontab -l.

The job is listed, but it never runs

Cron runs with a minimal environment. Verify the script path is correct, the script is executable, and use absolute paths inside the script. Then check cron logs for execution evidence.

The script runs manually but fails under cron

Environment differences are the usual cause. Missing PATH, missing variables, or relative paths inside the script can break cron execution. Log output to a file to capture errors.

No cron logs appear where you expect

Your system may be using journald-only logging or a different syslog configuration. Search the journal for cron entries or confirm where your distro records scheduler activity.

Cleanup checklist

This lab installs a scheduled job. If you do not want it to persist, remove the entry from the user crontab and verify it is gone.

Commands
crontab -l
crontab -e
Success signal

crontab -l shows the expected schedule (or shows no entry after removal), and cron logs show execution evidence when the job runs.

Reference

  • crontab -l: Lists the current user’s scheduled cron jobs.
  • crontab -e: Edits the current user’s crontab in the configured editor.
  • MIN HOUR DOM MON DOW CMD: Standard cron schedule fields used in user crontabs.
  • /var/log/syslog: Common location for cron messages on Debian/Ubuntu systems.
  • /var/log/cron.log: Common dedicated cron log file on some systems.
  • /etc/cron.d/: Directory for system-wide cron job definitions with an explicit user field.