Linux: Cron

Linux: Cron cover image

Category: Linux

Posted at: Jul 12, 2025

2 Minutes Read

What is cron?

Cron is a time-based job scheduler used in unix operating systems such as Linux and macOS. it has two main components:


  • Cron job: is a scheduled task that can be configured to run periodically at fixed times, dates, or intervals.
  • Crontab: A configuration file that contains a list of cron jobs along with the schedule for when each command should run.


Where does the name "cron" come from? The word cron comes from the Greek word chronos, which means time.

How to configure a cron job?

Cron jobs have a special format that looks like this:

* * * * * command_to_execute
| | | | |
| | | | └─── Day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | └────── Month (1 - 12) OR jan,feb,mar,apr...
| | └───────── Day of the month (1 - 31)
| └──────────── Hour (0 - 23)
└─────────────── Minute (0 - 59)

And here are some examples:


Runs every minute

* * * * * /home/user/backup.sh

Runs every day at 12:00 AM

0 0 * * * /home/user/backup.sh

Runs every Thursday at 4:00 PM

0 16 * * 4 /home/user/backup.sh


To add, modify or delete a cron job we use the crontab command:

List crontab: crontab -l

Edit crontab: crontab -e

Remove crontab: crontab -r (this deletes all cron jobs)


So, if you want to add a cronjob, you can use the command crontab -e. if it's your first time running this command, it may prompt you to choose your preferred text editor, as shown below:

user@debian:~$ crontab -e
no crontab for user - using an empty one

Select an editor. To change later, run 'select-editor'.
 1. /bin/nano    <---- easiest
 2. /usr/bin/vim.basic
 3. /usr/bin/vim.tiny

Choose 1-3 [1]:

Then, insert your cron job into the file and save it.

Conclusion

Cron jobs are useful for automating tasks like making backups, I use them myself for this website. You can also create scripts to clean up your server or remove unwanted files on a weekly basis.


I hope you found this helpful. Thank you for reading!


Good luck!

Comments

600