Code Icon

What the heck is a cron job?

Published on

Have you ever worked on a web project that would require a script to run at a certain time every day? Or perhaps, multiple times per day? Well, that’s exactly what cron jobs are for.

A “cron” is a Linux utility which schedules a command or script on your server to run automatically. A “cron job” is the actually scheduled task itself.

So, a cron job is an individual command that tells your server to run something at a specified time or interval. The time can be anything. Could be every minute. Could be every three months.

So let’s see what one of these things looks like, using my website as an example.

A screenshot of 3 active cron jobs within cpanel

That’s a lot of weird shit, right?

Let’s break it down into parts, starting with the timing mechanism.

Cron Job Timing

You’ll see columns that break down into the following categories: Minute, Hour, Day, Month, and Weekday. These correspond to…you guessed it..minutes, hours, days, months, and weekdays. The syntax is just a little weird.


For minutes, a “0” indicates that it will be at the beginning of an hour. Alternatively, you can add the precise minute of the hour you would like, such as “37”. You can also do tricky things, such as “*/2”, which means once every two minutes.


For hours, it’s pretty much the same. “0” corresponds to midnight, and you can input the number you wish based off military time (“13” is 1 pm). You can also do the crazy stuff like “*/3” which is every third hour.


Days are a lot simpler. You can specify the number of the day, or, more commonly, do “*”, which means every day. You can also do something like “*/2” which means every other day.


Months are simple like days. The months have a corresponding number, so January is “1”. Just like the others, you can do wild things like “*/2” for every other month.


Weekday is a little weird. If you have something running daily or something to that effect, this is where you can specify the day of the week you would like to limit it to. For instance, if I have a daily job that I only want to happen every Monday, Wednesday, and Friday, I would put “*” in the day field, but put “1,3,5” in weekday. Or, for something ran once a week on Sunday, you would just put “0” in weekday.

All that makes sense? No? Well, let’s look at some examples.

Case: We want a script to run every Sunday morning at 3:24 am.
Solution: 24 3 * * 0

Case: We want a script to run every day at midnight.
Solution: 0 0 * * *

Case: We want a script to run twice every hour, but only Monday through Friday
Solution: 0,30 * * * 1-5

Is it all starting to come together now?

Good, let’s move on to something simpler. The actual command to be executed.

Cron Job Commands

For this, it’s good to have a good knowledge of your server’s file structure. For instance, I rent a server, so I FTP using Filezilla and can see my directory structure on the right side.

A screenshot of FileZilla showing the file structure

Now, there are quite a few different ways to go about doing this, but I’m a simple person so I’m going to keep it simple. My example is going to be a PHP script.

So how do we do that?

See the Pen Example Cron Job PHP Command by Kyle Hawk (@Krazyhawk) on CodePen.

You’ll see the beginning part “php”. This is telling the server to use the PHP command-line interpreter. Basically, you’re telling the server, “Hey! This is a PHP script, yo!”

After that, comes the path to the script you want to execute. It starts like “/home/username/”, where username is whatever user you are on the server or the provided name from your rented server.

After that, we are in the root directory for our user. If your script is in the public_html directory of your server, then your path would be “/home/username/public_html/”.

For example, “/home/username/public_html/scripts/myphpscript.php”.

This is telling the cron job that the php script it needs to execute is in the username user, under public_html, under scripts, and the file name is myphpscript.php.

For me, my usual cron jobs are stuff that I don’t necessarily want public facing, just for an extra security measure. So, I like to create a directory outside of public_html. So my path could look something like “/home/username/cron-jobs/cron.php”. Notice that it is not inside the public_html directory.

That’s About It

Well, hopefully this has been a good introductory to the world of cron jobs and you have enough knowledge to create your own!

Trust me, it’s not as scary as it seems. If you aren’t sure about something, test it!

There are also plenty of tools out there to help with the timing stuffs, just give it a Google. For example, Cron Tab Guru is pretty awesome. Alternatively, your server’s cpanel sometimes has presets to help you out.

Good luck!