Skip to main content

Linux: Crontab Tutorial with Examples

What is Crontab?

Crontab (CRON TABle) is a Linux (Unix) program that manipulates the CRON daemon, making it easy for users to schedule task and run programs/commands at pre determined periods of time. Crontab can also be considered a file witch contains commands that will be run by the system as the user that owns the crontab file.

What is the purpose of Crontab?

Cron is designed to maintain a list of commands that the system needs to run at a given time interval.
For example if you have a script that generates statistics and needs to be run every couple of hours or everyday cron can do it for you. Or for example if you have a script that sends a newsletter every month you can use cron to run the script that sends the newsletter every month or week.

Crontab commands

When your logged in to your server you can use program cron using the following commands:

  • crontab -l
    • Lists the current cron jobs
  • crontab -e
    • Edit your current crontab file and ad/remove/edit crontab tasks.
  • crontab -r
    • Remove the crontab file.
  • crontab -v
    • Displays the last time you edited your crontab file.


The crontab file – components of crontab

When you enter the edit mode (crontab -e) and start adding tasks to your cron file you need to consider the following syntax:





The asterisk (*) symbolizes that every instance of that field (i.e. every minute, hour, day, month, weekday) will be used in the command.

Example on how to setup your first crontab

Lets say you have a script named run-me.sh located in /home/your_username you want to run every day at 13:30. You will need to login your server console and input the following commands:

crontab -e
This will open the crontab file and let you edit it. By default this file will be opened with the VI editor and you will need to press the “Insert” key (i) on your keyboard to be able to write in that file.
 

30 13 * * * /home/your_username/run-me.sh >/dev/null 2>&1
 

  • The first character you see is “30” this means that crontab will run the script every time the clock hits the 30 minutes mark.
  • Next “13” this means that crontab will run the script when the clock hits 13.
  • The next three * tell crontab to run the script every day, of every month of every weekday.

Combining these fields crontab will run the script every day at exactly 13:30. You may notice that we added the “>/dev/null 2>&1” string at the end of the command. The default cron job will always send and e-mail to the root account when ever a command is executed. Now you don't want to be notified every day that your crontab job has been executed.

If you don't want to receive e-mails every day notifying you about your job's execution place this “>/dev/null 2>&1” at the end of every instance of every crontab command.


When you are finished adding your commands to the crontab file you need to save and exit. If you are using VI as your editor you need to issue the following commands:
 

  1. Press the Esc (Escape key) on your keyboard to enter the command mode of VI
  2. After you pressed Escape then type the following characters :wq! and press Enter. Remember you have to type this characters (without the quotes): “:wq!”.

Now to list your crontab job just issue the following command: crontab -l

crontab -l

If you need to ad another crontab job or even more all you need to do is follow the same steps as above and just ad another line to the file.
 

REMEMBER: Every crontab job needs to be placed on its own line in the file and after the last line you need to insert a non-braking character (press Enter).

The crontab syntax goes a little beyond its boundaries and has more advance meaning for some users.
For example if you wish to use more then one instance of one column you will separate those instances by a comma “,” or if you wish to use a time period lets say for example from Sunday till Tuesday you will use dash “-”.


10,20,30 13 * * 0-2 /home/your_username/run-me.sh >/dev/null 2>&1


This crontab job will run your scrip “run-me.sh” on from Sunday until Tuesday (Sunday, Monday, Tuesday) at 13:10, 13:20 and 13:30. Remember there are no spaces between values separated by commas “,” and neither in dashes “-”. There is also an operator that some versions of cron support called the slash operator “/” that can be used to skip a given number of values in your jobs.

Crontab examples - cron examples

The syntax of crontab is not very easy to understand from the start and the best way of understanding is from examples:

Run the script every day at 12:00.


0 12 * * * /home/your_username/run-me.sh >/dev/null 2>&1

 

Run the script every day at 12:00, 14:00 and 16:00.


0 12,14,16 * * * /home/your_username/run-me.sh >/dev/null 2>&1

 

Run the script every Sunday at 13:30.


30 13 * * 0 /home/your_username/run-me.sh >/dev/null 2>&1

 

Run the script every Saturday at 12:00, 14:00 and 16:00.


0 12,14,16 * * 6 /home/your_username/run-me.sh >/dev/null 2>&1

 

Run the script on the first, fifteenth and twentieth of every month.


0 0 1,15,20 * * /home/your_username/run-me.sh >/dev/null 2>&1

 

Run the script every day from 3 to 3 hours: 00:00, 03:00, 06:00 etc.


0 */3 * * * /home/your_username/run-me.sh >/dev/null 2>&1


 


Popular Webmaster Tutorials

WEBSITE TEMPLATE: SLIDING DOORS - Use it as a website template for intros with animation and landing page forms

Greetings fellow webmasters!   Here is a new template for you to use freely on your web development projects. It's called Sliding Doors. This template uses just a little bit of Javascript for its awesome reveal animation effect. Check it out, it's really cool! Check it out here, live preview: https://sliding-doors.webmasterbits.com/ Download it on github, here's the link: https://github.com/webmasterbits/sliding-doors As always, you can check out all of our free website templates here: https://www.webmasterbits.com/p/templates.html Let us know what you think of this new template in the comments bellow. You are welcome to include links to projects you've used this template for.

DAY 1: Become a Webmaster Tutorial - BUILD A WEBSITE IN 10 MINUTES and host it on Github for Free

  Greetings fellow webmasters and soon to become webmasters!   I released my first video in the Become a Webmaster series. With this video I am also updating the Become a Webmaster page with the tutorial for Day 1. Watch the Day 1 video on youtube here: https://youtu.be/kJYONpeeDj4   Read the detailed tutorial on Day 1 here: https://www.webmasterbits.com/p/become-webmaster.html Day 1 is the most important day in your journey of becoming a webmaster. Here's what I explain in the video and tutorial: How to gather content for your new website How to create a Protonmail account How to create a Github account How to create a repository for your new site How to create the index.html file How to add headings Read the tutorial and then watch the video to follow along.  

How to do research for a website

  Being a Webmaster is a lot of work and research is a big part of it. Like most professions, you need to stay on top of your game. Whether you're building a new website or taking one over, you will need a plan. And a solid plan requires research. If you haven't already, read What is a Webmaster first . Below we have compiled a list of areas of research that you as a Webmaster shoud be very familiar with and actively pursuing. Remember to write down everything you research and the results of your research.   Tools & Service Providers Research Having the right tools for the job is key. Knowing what to use and when will give you the confidence to move forward faster and get things done. As a Webmaster you'll always be asked: where do I register a domain? what's a reliable hosting company? what's your recommendation? Being able to provide knowledgeable advice is your most valuable asset. It's your most valuable asset because it's a source of passive income.