Terminal and shell

Schedule a job with cron

Make a command run by itself on a schedule, like every night at 2 am.

Fill these in once
  1. Add the job
    Terminal
    (crontab -l 2>/dev/null; echo "0 2 * * * /Users/me/backup.sh") | crontab -

    0 2 * * * means 2:00 every day. The five parts are minute, hour, day, month and weekday.

Check it worked

  1. List your jobs
    Terminal
    crontab -l
Undo it
  1. Remove the job
    Terminal
    crontab -l | grep -vF "/Users/me/backup.sh" | crontab -
  1. Add the job
    Terminal · Ubuntu / Debian
    (crontab -l 2>/dev/null; echo "0 2 * * * /Users/me/backup.sh") | crontab -

    0 2 * * * means 2:00 every day. The five parts are minute, hour, day, month and weekday.

Check it worked

  1. List your jobs
    Terminal · Ubuntu / Debian
    crontab -l
Undo it
  1. Remove the job
    Terminal · Ubuntu / Debian
    crontab -l | grep -vF "/Users/me/backup.sh" | crontab -
  1. Add a daily task at 2:00
    PowerShell
    schtasks /Create /SC DAILY /ST 02:00 /TN "Nightly backup" /TR "/Users/me/backup.sh"

    Windows uses Task Scheduler instead of cron.

Check it worked

  1. Check it's there
    PowerShell
    schtasks /Query /TN "Nightly backup"
Undo it
  1. Remove the task
    PowerShell
    schtasks /Delete /TN "Nightly backup" /F

New to cron jobs? I explain it in plain English on Layman Terms.