Skip to main content

How to set-up a crontab file

In Linux, Cron is a daemon/service that executes shell commands periodically on a given schedule. Cron is driven by a crontab, a configuration file that holds details of what commands are to be run along with a timetable of when to run them. Knowing how to use Cron is key to mastering automation with Linux.

Creating a crontab file

You can create a crontab file by entering the following terminal command:

crontab -e

Entering the above command will open a terminal editor with a new blank crontab file, or it will open an existing crontab if you already have one. You can now enter the commands to be executed, see syntax below, before saving the file and exiting the editor. As long as your entries were entered correctly your commands should now be executed at the times/dates you specified. You can see a list of active crontab entries by entering the following terminal command:

crontab -l

Crontab syntax

A crontab file has six fields for specifying minute, hour, day of month, month, day of week and the command to be run at that interval. See below:

*     *     *     *     *  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Crontab examples

Writing a crontab file can be a somewhat confusing for first time users, therefore I have listed below some crontab examples:

* * * * *  command #Runs every minute
30 * * * * command #Runs at 30 minutes past the hour
45 6 * * * command #Runs at 6:45 am every day
45 18 * * * command #Runs at 6:45 pm every day
00 1 * * 0 command #Runs at 1:00 am every Sunday
00 1 * * 7 command #Runs at 1:00 am every Sunday
00 1 * * Sun command #Runs at 1:00 am every Sunday
30 8 1 * * command #Runs at 8:30 am on the first day of every month
00 0-23/2 02 07 * command #Runs every other hour on the 2nd of July

As well as the above there are also special strings that can be used:

@reboot command #Runs at boot
@yearly command #Runs once a year [0 0 1 1 *]
@annually command #Runs once a year [0 0 1 1 *]
@monthly command #Runs once a month [0 0 1 * *]
@weekly command #Runs once a week [0 0 * * 0]
@daily command #Runs once a day [0 0 * * *]
@midnight command #Runs once a day [0 0 * * *]
@hourly command #Runs once an hour [0 * * * *]

Multiple commands

A double ampersand && can be used to run multiple commands consecutively. The following example would run command_01 and then command_02 once a day:

@daily command_01 && command_02

Disabling email notifications

By default a cron job will send an email to the user account executing the cronjob. If this is not needed put the following command at the end of the cron job line:

>/dev/null 2>&1

Or, you can disable all email notifications by adding the following to the top of the crontab file:

MAILTO=""

Specifying a crontab file to use

As mentioned at the top of this post, you can create a new crontab file with the “crontab -e” command. However, you may already have a crontab file, if you do you can set it to be used with the following command:

crontab -u 

Therefore the following command…

crontab -u tux ~/crontab

…would set Tux’s crontab file to that of the file named “crontab” residing in Tux’s home directory.

Removing a crontab file

To remove your crontab file simply enter the following terminal command:

crontab -r

Further information

Refer to the man page for further information about crontab. Enter the terminal command:

man crontab

External links

Some external links for your browsing pleasure:

I think that pretty much covers the subject of cron jobs and crontab. Please feel free to comment if I have missed anything or made any obvious mistakes.

View as: JSON Markdown

If you enjoyed this post or found it useful, you can subscribe to my RSS feed.

Similar posts

  1. Upgrading from Fedora 41 to Fedora 42

    If someone were to ask me which Linux distro has provided the best desktop experience, I wouldn't hesitate to answer: Fedora Workstation 41. So of course I upgraded to Fedora 42.

    fedora linux
  2. How to install PHP extension for Microsoft SQL Server under Fedora

    I found myself needing to connect to a Microsoft SQL Server via a PHP application running under Fedora. Finding concise details about installing the necessary drivers and extensions was not easy, so here is a blog post detailing how I did it.

    php microsoft fedora mssql sql linux
  3. GNOME menu entries for Visual Studio Code projects

    I work on a large number of code projects and I wanted a quick way to open any of my projects in Visual Studio Code, my preferred code editor. I figured the quickest way to do this under GNOME would be to create a .desktop file for each project directory.

    gnome vscode linux
  4. My Debian 12 (bookworm) server set-up

    I've been running Debian on my servers for years. It's dependable. I guess my server set-up is pretty common, consisting of Apache, PHP and MariaDB, but I figure it is still worth sharing details of how I provision my servers.

    php composer mariadb apache debian linux node fish
  5. My Debian 12 (bookworm) desktop set-up

    Creating a good Debian desktop experience is not too difficult, thanks to the excellent work of the Debian developers, but I thought it might be interesting to share how I set-up my Debian systems.

    debian linux
  6. Upgrading from Fedora 40 to Fedora 41

    A post describing my first experience of upgrading a Fedora installation. TLDR: The upgrade went smoothly and Fedora continues to impress me.

    fedora linux
  7. Desktop Linux and compiling from source

    If anyone is in any doubt as to whether you need to compile any software from source in order to use desktop Linux, you really don't.

    linux
  8. Switching desktop Linux from Debian to Fedora

    Last week I switched the operating system on my daily driver (Lenovo ThinkPad T14s) from Debian 12 to Fedora 40. In this post I write a little about why I switched and how the switch went.

    debian linux fedora
  9. How to create Bash aliases in Fedora

    Creating your own Bash aliases is a relatively easy process. That said, I recently switched my desktop linux distribution from Debian to Fedora and there are subtle differences.

    linux fedora debian bash