How to Create a Cron Job in Linux

If you manage a Linux server, you cannot rely on human memory to perform critical maintenance tasks. If a database needs to be backed up at 3:00 AM every single Sunday, or a temporary cache folder needs to be purged every fifteen minutes, you must automate the process. The tool that handles this background automation in Linux is a legendary software daemon called cron.

You interact with the cron daemon by writing rules into a specific configuration file called a crontab (cron table). The syntax of a crontab file is notoriously unforgiving; if you put a single space in the wrong place, the job will silently fail.

Step 1: Open Your Crontab File

Every single user on a Linux system (from the root administrator down to a restricted guest) has their own private crontab file. You do not edit these files directly using standard text editors because the daemon needs to validate the syntax upon saving.

  1. Open your terminal or SSH into your server.
  2. To edit your personal crontab, type the following command:

crontab -e

(Note: If you need a script to run with absolute administrative privileges, you must edit the root crontab instead by typing sudo crontab -e).

  1. If this is the absolute first time you have ever run this command, the terminal will ask you to choose a default text editor. Press 1 to select nano (the easiest editor for beginners) and press Enter.

The nano text editor will open, displaying a large block of blue commented text explaining how cron works. Use your arrow keys to scroll down to the very bottom of the file to create a blank new line.

Step 2: Understand the Cron Syntax

A cron job consists of exactly six columns, separated by spaces. The first five columns represent the Time and Date, and the final column represents the Command to execute.

The five time columns are (in this exact order):

  1. Minute (0-59)
  2. Hour (0-23, military time)
  3. Day of the Month (1-31)
  4. Month (1-12)
  5. Day of the Week (0-7, where both 0 and 7 represent Sunday)

You use a literal asterisk (*) to tell cron “every.” If you put an asterisk in the Month column, the job runs every month.

Step 3: Write the Job (Examples)

Let’s write a few practical examples.

Example 1: Run a Backup Script Every Day at 2:30 AM

30 2 * * * /home/user/backup.sh

  • Minute: 30
  • Hour: 2
  • Day of Month: * (Every day)
  • Month: * (Every month)
  • Day of Week: * (Every day)

Example 2: Run a Script Every 15 Minutes

If you want a job to run repeatedly at a specific interval, you use the slash (/) modifier.

*/15 * * * * /home/user/clear_cache.sh

This translates to “Every minute that is divisible by 15.”

Example 3: Run a Script Every Monday at Noon

0 12 * * 1 /home/user/weekly_report.sh

Step 4: Save and Install the Job

Once you have typed your cron job at the bottom of the file:

  1. Press Ctrl + O (the letter O) to save the file.
  2. Press Enter to confirm the filename.
  3. Press Ctrl + X to exit the nano editor.

The exact second you exit nano, the terminal will print crontab: installing new crontab. The daemon has successfully swallowed your new rule, and the job is now actively running in the background.

Crucial Troubleshooting Tip: Absolute Paths

The single most common reason a cron job fails is because the user relied on relative paths. When cron executes a script at 3:00 AM, it does not load your user profile, it does not load your environmental variables, and it has no concept of a “current working directory.”

If your script tries to write data to a file called log.txt, cron will fail because it doesn’t know where to put it. You must write every single file path in your scripts as a strict, absolute path (e.g., /home/user/scripts/log.txt). You must also use absolute paths for system commands (e.g., use /usr/bin/python3 instead of just typing python3).

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.