How to Create and Manage Cron Jobs on Linux

Cron is one of Linux’s most useful tools and a developer favorite because it allows you to run automated commands at specific periods, dates, and intervals using both general-purpose and task-specific scripts. Given that description, you can imagine how system admins use it to automate backup tasks, directory cleaning, notifications, etc.

Cron jobs run in the background and constantly check the /etc/crontab file, and the /etc/cron.*/ and /var/spool/cron/ directories. The cron files are not supposed to be edited directly and each user has a unique crontab.

How then are you supposed to create and edit cron jobs? With crontab commands. The crontab is the method you use to create, edit, install, uninstall, and list cron jobs.

The command for creating and editing cron jobs is the same and simple. And what’s even cooler is that you don’t need to restart cron after creating new files or editing existing ones.

$ crontab -e

Cron Syntax

Just as it is with any language, working with cron is a lot easier when you understand its syntax and there are 2 formats you should know:

A B C D E USERNAME /path/to/command arg1 arg2
OR
A B C D E USERNAME /root/backup.sh

Explanation of above cron syntax:

  • A: Minutes range: 0 – 59
  • B: Hours range: 0 – 23
  • C: Days range: 0 – 31
  • D: Months range: 0 – 12
  • E: Days of the week range: 0 – 7. Starting from Monday, 0 or 7 represents Sunday
  • USERNAME: replace this with your username
  • /path/to/command – The name of the script or command you want to schedule

That’s not all. Cron uses 3 operator symbols which allow you to specify multiple values in a field:

  1. Asterisk (*): specifies all possible values for a field
  2. The comma (,): specifies a list of values
  3. Dash (-): specifies a range of values
  4. Separator (/): specifies a step value

Now that you know Cron’s syntax and operators, let’s see some cron examples.

Cron Job Examples

The first step to running cron commands is installing your crontab with the command:

# crontab -e

Run /root/backup.sh at 3 am every day:

0 3 * * * /root/backup.sh

Run script.sh at 4:30 pm on the second of every month:

30 16 2 * * /path/to/script.sh

Run /scripts/phpscript.php at 10 pm during the week:

0 22 * * 1-5 /scripts/phpscript.php

Run perlscript.pl at 23 minutes after midnight, 2am and 4am, everyday:

23 0-23/2 * * * /path/to/perlscript.pl

Run Linux command at 04:05 every Sunday:

5 4 * * sun /path/to/linuxcommand

Cron Options

List cron jobs.

# crontab -l
OR
# crontab -u username -l

Delete all crontab jobs.

# crontab -r

Delete Cron job for a specific user.

# crontab -r -u username

Strings in Crontab

Strings are among the developer’s favorite things because they help to save time by eliminating repetitive writing. Cron has specific strings you can use to create commands quicker:

  1. @hourly: Run once every hour i.e. “0 * * * *
  2. @midnight: Run once every day i.e. “0 0 * * *
  3. @daily: same as midnight
  4. @weekly: Run once every week, i.e. “0 0 * * 0
  5. @monthly: Run once every month i.e. “0 0 1 * *
  6. @annually: Run once every year i.e. “0 0 1 1 *
  7. @yearly: same as @annually
  8. @reboot: Run once at every startup

For example, this is how to backup your system every day:

@daily /path/to/backup/script.sh

At this point, you have all you need to create and manage system tasks using Cron. You can now begin to set up and maintain several environments using scheduled commands.

How much of a Cron user are you? And are there any details you can contribute to the article? The discussion box is below.

When you understand enough about how Crontab works you can use these nifty Crontab generator utilities to generate crontab lines for free.

Also, you can read Ubuntu’s article on how to use Cron here. It has resources that you might find useful.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Martins D. Okoi
Martins Divine Okoi is a graduate of Computer Science with a passion for Linux and the Open Source community. He works as a Graphic Designer, Web Developer, and programmer.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

13 thoughts on “How to Create and Manage Cron Jobs on Linux”

  1. 23 0-23/2 * * * /path/to/perlscript.pl
    

    I understand all other example except this one — could you explain more.

    Reply
  2. For this example “Run perlscript.pl at 23 minutes after midnight, 2am and 4am, everyday” I hope exact answer would be below :

    23  0-4/2  * * * /path/to/perlscript.pl
    
    Reply
    • Run perlscript.pl at 23 minutes after midnight, 2am and 4am, everyday” It can be.

      23 0,2,4 * * * /path/to/perlscript.pl
      

      so it runs at 12.23, 2.23, 4.23.

      Reply
  3. There are 2 formats you should know:

    I believe that’s not what it means by the guy who provides you the following 3 lines.

    A B C D E USERNAME /path/to/command arg1 arg2
    OR
    A B C D E USERNAME /root/backup.sh
    

    means the 1st line is the general format and the 3rd line is one of the examples.

    Reply
  4. Excellent article, I am not too clear on Run perlscript.pl at 23 minutes after midnight, 2am and 4am, everyday: so will be checking that format, keep up the great work

    Reply
  5. One thing to remember is that if the cron user is supposed to run a script that assumes that the (normal) user environment exists for the script, then the user is mistaken; the cron function needs to read in the user’s environment via ‘source /home/user/.bashrc‘ before the actual ‘/path/to/script.bash‘ is called.

    So when testing the script, state the full paths to the binaries called inside the script,or set the paths needed inside the script via ‘PATH=${PATH}:/path/to/binary1:/path/to/binary2

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.