Guide to Linux Cron Jobs

Deepak NemadeDeepak Nemade
3 min read

Have you ever wanted your computer to automatically do things for you, like save files or run scripts at specific times? On Linux, something called cron jobs performs this magic. Let’s dive into what cron jobs are, how you can configure them, and the basic commands you need to know.


What is a Cron Job?

Think of it as a cron job that can force your computer to perform a task on a system, such as setting an alarm clock. These tasks are managed by an external process called the cron daemon (crond), which ensures that your scheduled tasks run at the right time.


What is Crontab?

A crontab (short for "cron table") is a special file to list all the tasks you want to schedule. On Linux systems, each user can have their own crontab file, which means you can customize your system without affecting others.


How to Read a Crontab File

* * * * * command_to_be_executed
- - - - -
| | | | |
| | | | +----- Day of the week (0 - 7) (Sunday is both 0 and 7)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)

Each field can take different values:

  • * (asterisk): This means "every" (e.g., every minute or every day).

  • , (comma): Used to separate multiple values (e.g., 1,15 means both the 1st and the 15th).

  • - (hyphen): Used to specify ranges (e.g., 1-5 means from 1 to 5).

  • / (slash): Used for intervals (e.g., */2 means every 2 units, like every 2 minutes).


Basic Crontab Commands

List Your Cron Jobs

crontab -l

This command shows all the cron jobs you’ve set up.


Edit Your Crontab

crontab -e

This opens your crontab file in a text editor so you can add or change jobs.


Remove Your Crontab

crontab -u username -l

Use this to see the cron jobs of a different user (you'll need the appropriate permissions).


Examples of Cron Jobs

  • Run a Script Every Day at Midnight
0 0 * * * /path/to/script.sh
  • Run a Command Every 5 Minutes
*/5 * * * * /path/to/command
  • Run a Backup Script Every Sunday at 2 AM
0 2 * * 0 /path/to/backup.sh
  • Run a Cleanup Script on the First Day of Every Month at 3 AM
0 3 1 * * /path/to/cleanup.sh

Extra Tips for Using Cron Jobs

  1. Use Full Paths: Always use the full path for commands and files to avoid any issues with paths.

  2. Check Logs: If a cron job isn’t running as expected, check the logs in /var/log/syslog or /var/log/cron.

  3. Test Your Commands: Before adding a command to your crontab, run it manually to make sure it works as expected.


Cron jobs are a powerful way to automate tasks on Linux, saving you time and ensuring important processes run smoothly. By mastering cron jobs, you can efficiently schedule backups, run maintenance scripts, and perform regular system checks with ease. Remember to test your commands, check logs, and use full paths for reliability. Embrace cron jobs, and transform your Linux workflow! Happy scheduling!

0
Subscribe to my newsletter

Read articles from Deepak Nemade directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Deepak Nemade
Deepak Nemade