How to create a cron job on Linux

On this page

To create a cron job, run crontab -e, add one line made of five schedule fields and a command, then save the file. Cron installs the change by itself and starts running the line at the next matching minute. The steps are the same on Ubuntu, Debian, Fedora and most other Linux systems.

How do you create a cron job step by step?

  • Run the command by hand first. Use the full path of every program and file, such as /usr/local/bin/backup.sh. If it fails in your terminal it will fail in cron.
  • Make a script executable. chmod +x /usr/local/bin/backup.sh, and start the file with a line such as #!/bin/bash.
  • Open your crontab. crontab -e opens the file in the editor named by VISUAL or EDITOR. The first time, Debian and Ubuntu ask you to pick an editor.
  • Add one line. Five schedule fields, then the command.
  • Save and quit. Cron reads the new file on its own. There is nothing to restart.
  • Read it back. crontab -l prints the file as installed.
crontab -e
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

That line runs backup.sh at 02:30 every day and appends its output to a log file. Cron needs each line to end in a newline, so keep an empty line after the last entry. The five fields are explained in the cron syntax cheat sheet, and the cron expression translator reads any line back in plain English.

How do you test a new cron job?

Do not wait for 02:30 to find a typo. Set the schedule to every minute for the first test, wait for the next minute, then check that the command ran:

crontab -e
* * * * * /bin/date >> /tmp/cron-test.log

When /tmp/cron-test.log gains a line a minute, cron is working and the schedule is read. Swap in the real command and the real schedule, and delete the test line. If nothing appears, go through cron job not running, and see how to check whether a cron job ran.

How do you create a cron job as root or for another user?

GoalCommandRuns as
Your own jobscrontab -eYou
Jobs that need rootsudo crontab -eroot
Another user's jobssudo crontab -u appuser -eappuser
A system job with a user columnA file in /etc/cron.d, or /etc/crontabThe user named in the line

Lines in /etc/crontab and in /etc/cron.d have an extra field between the schedule and the command: the name of the user that runs it. Lines in a personal crontab have no such field. Putting a username in a personal crontab line makes cron try to run it as a command. Where cron jobs are stored lists every place a job can live.

How do you edit, disable and remove a cron job?

  • Edit: crontab -e and change the line.
  • Switch one off: put a # in front of the line. It stays in the file for later.
  • Remove one: delete its line in crontab -e.
  • Remove all of them: crontab -r deletes your whole crontab at once, with no confirmation. crontab -i -r asks first, and crontab -l > backup.txt saves a copy before you start.

What do you add so that a failure does not go unnoticed?

A new cron job gives no sign when it stops. Add a request to a monitoring URL after the command, joined with && so that it is sent only after a successful run, and give it a timeout:

crontab -e
30 2 * * * /usr/local/bin/backup.sh && curl -fsS --max-time 10 --retry 5 -o /dev/null https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10

Create a check with the same schedule and CronMissed alerts you when the request is late. The crontab guide shows the setup, and what is cron job monitoring? explains why a silent job needs one.

Sources

Questions and answers

How do I create a cron job in Ubuntu?
Run crontab -e, choose an editor if asked, add a line such as 30 2 * * * /usr/local/bin/backup.sh, and save. Ubuntu starts the cron service at boot, so nothing else needs enabling.
Do I need to restart cron after editing the crontab?
No. crontab -e installs the file when you quit the editor, and cron picks up the change by itself. Editing a file in /etc/cron.d is also read without a restart.
Why does my cron job work in the terminal but not from cron?
Cron runs with a short PATH and none of your shell profile. Use full paths and set variables in the crontab; cron environment and PATH goes through the causes.
How do I create a cron job that runs every 5 minutes?
Use */5 * * * * as the schedule. Make sure a run can finish within 5 minutes, or two runs will overlap; see overlapping cron runs.
Know the moment a job goes quiet

Create a check, add one curl to the job, and CronMissed alerts you when a ping is late or reports a failure.

Start monitoring