.local-bin/docs/setup_crontab/Usage.md

1.5 KiB
Raw Permalink Blame History

setup_crontab

Sets up automated cron jobs from a configuration file.

Description

This script manages cron jobs by reading from mycronjobs.txt and merging them into the system crontab. It intelligently avoids duplicate entries and ensures the cron service is running.

Usage

./setup_crontab.sh

Configuration File

The script expects a cron job definition file at: ~/.local/bin/mycronjobs.txt

Example mycronjobs.txt:

# Daily backup at 2 AM
0 2 * * * /home/user/.local/bin/backup.sh

# Weekly cleanup on Sundays
0 0 * * 0 /home/user/.local/bin/cleanup.sh

Features

  • Smart Merging: Only adds new jobs, skips existing duplicates
  • Comments Support: Lines starting with # are ignored
  • Service Management: Automatically starts cron service on Linux
  • Cross-Platform: Works on Linux (systemd) and macOS (launchd)

Platform Behavior

Linux

  • Starts cron via systemctl if not running
  • Uses standard crontab

macOS

  • Uses launchd (cron runs automatically)
  • No additional service management needed

Example Output

✅  Added 2 new cron job(s):
   + 0 2 * * * /home/user/.local/bin/backup.sh
   + 0 0 * * 0 /home/user/.local/bin/cleanup.sh

Or if jobs already exist:

  No new cron jobs to add; all jobs already present.

Notes

  • The script uses set -euo pipefail for strict error handling
  • Existing crontab entries are preserved
  • Empty lines are ignored during processing