In this categoryAutomation ยท 38
AutomationIntermediate

How to Automate a Daily Task with a Cron Script

Wrap a repeatable job in a small script and schedule it with cron so it runs on its own every day and logs what it did.

9 minIntermediate

Any task you do at the same time every day is a candidate for cron, the scheduler built into every Unix-like system. Wrap the task in a script, schedule it once, and it runs untouched. This guide builds a small backup script and schedules it with a log you can check.

What you need

  • A Mac or Linux machine that stays on at the scheduled time
  • A task you can express as shell commands
  • About 10 minutes

Step 1: Put the task in a script

Write the job as a script with a shebang line, and have it echo a timestamp so the log shows when it ran. Make it executable.

~/bin/daily-backup.sh
#!/usr/bin/env bash
set -euo pipefail
stamp="$(date '+%Y-%m-%d %H:%M:%S')"
echo "[$stamp] starting backup"
tar -czf "$HOME/backups/notes-$(date +%F).tgz" "$HOME/notes"
echo "[$stamp] backup done"
terminal
chmod +x ~/bin/daily-backup.sh
~/bin/daily-backup.sh   # run once by hand to confirm it works

Step 2: Add a cron entry

Open your crontab and add one line. The five fields are minute, hour, day of month, month, and day of week. Below runs the script at 07:30 every day and appends output to a log.

zsh - crontab
$crontab -e
add this line, then save and quit
30 7 * * * $HOME/bin/daily-backup.sh >> $HOME/backups/backup.log 2>&1
$

Step 3: Confirm it is scheduled

List the crontab to verify the line is registered. After the first scheduled run, read the log to confirm it fired.

Terminal - verify
$ crontab -l
30 7 * * * /Users/you/bin/daily-backup.sh >> ...
$ tail -n 2 ~/backups/backup.log
[2026-06-25 07:30:01] starting backup
[2026-06-25 07:30:03] backup done
The job is registered and the log shows a clean run.
Cron has a bare environment
Cron does not load your shell profile, so PATH is minimal. Use full paths to binaries, or set PATH at the top of the script, or commands like node and git will be not found.

Result: a task that now runs itself on schedule and writes a log you can glance at, freeing you from remembering to do it by hand.

Watch related tutorials

Free weekly email

New guides in your inbox

Fresh step-by-step how-to guides as we publish them. One email a week, no more.

Tags
#automation#cron#scripts#scheduling#shell