How to use cron and scheduled tasks

Topic: Servers linux

Summary

Schedule recurring commands with cron: edit crontab -e, use the five fields (minute hour day month weekday), and log output to a file or to journal. Use systemd timers as an alternative for services. Verify the job runs and check logs when it fails.

Intent: How-to

Quick answer

  • crontab -e to edit user crontab; format: minute hour day month weekday command; use absolute paths and redirect output (>> /var/log/cron-job.log 2>&1).
  • System cron: put scripts in /etc/cron.d or add to /etc/crontab; ensure executable and owner correct; cron runs with minimal env so set PATH in script or crontab.
  • Verify: wait for the schedule or run the command by hand; check log file or journal (CRON in syslog); fix env (PATH, HOME) if command works in shell but not in cron.

Prerequisites

Steps

  1. Edit user crontab

    crontab -e; add line: 0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1 (daily at 02:00); save and exit; crontab -l to list.

  2. Use correct syntax and paths

    Five fields: 0-59 0-23 1-31 1-12 0-6 (Sun=0) or names; * means every; use /full/path/to/script and set PATH= at top of crontab if needed.

  3. System-wide cron

    Script in /etc/cron.d/ with user field; or script in /etc/cron.daily/ (run by run-parts daily); chmod +x script; ensure shebang and paths.

  4. Verify and debug

    Run the command manually as the cron user; check mail or log file; ensure no interactive prompt; use flock to avoid overlapping runs if needed.

Summary

You will schedule recurring tasks with cron (crontab or /etc/cron.d), use correct syntax and paths, and confirm jobs run and log correctly. Use this for backups, cleanup, and any periodic script.

Prerequisites

  • User or root access; the command or script to run and the desired schedule.

Steps

Step 1: Edit user crontab

crontab -e
# Add:
# 0 2 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1
crontab -l

Step 2: Use correct syntax and paths

  • Minute (0-59), hour (0-23), day of month (1-31), month (1-12), day of week (0-7, 0 and 7 = Sunday).
  • Use full paths for the command and any files it reads/writes. Set PATH=... or SHELL=/bin/bash at the top of the crontab if the command needs it.

Step 3: System-wide cron

  • /etc/cron.d/script: add line with user field, e.g. 0 3 * * * appuser /opt/app/cleanup.sh.
  • /etc/cron.daily/: drop an executable script; run-parts runs them daily (time depends on distro).

Step 4: Verify and debug

Run the script by hand as the cron user (e.g. su - user -c “/opt/scripts/backup.sh”). Check the log file or syslog for CRON entries. Use flock to prevent overlapping runs: flock -n /tmp/backup.lock /opt/scripts/backup.sh.

Verification

  • crontab -l shows the line; after the scheduled time the log has new output or the effect is visible; no errors in syslog for that cron job.

Troubleshooting

Job does not run — Cron may be stopped (systemctl status cron); check crontab syntax (no blank lines with wrong format); ensure script is executable and path exists.

Command works in shell but not in cron — Cron has a minimal env; set PATH, HOME, or source profile in the script; avoid interactive prompts.

Next steps

Continue to