Hulp nodig? Wij helpen graag

How can I set up cron jobs?

Whenever you have a website or webshop, there are always a series of tasks you need to do to keep your website/shop up-to-date. These tasks can be performed manually, but it's more efficient to do these tasks automatically by the server. This is why cronjobs came into life. A cronjob will perform these tasks for you.

In this article we’ll explain how cronjobs work and how to set them up.

How does a cron job work?

A cronjob can always be seen as two different parts

  • When does the cronjob run (time)

  • What needs to be done (task)

Let’s take a look at a example cronjob:

12 6 * * * php /home/domein1q/public_html/cron.php

With this example we’ll explain how the two parts of the cronjob work together.

When does the cron job run

You can use the following table to check how the timestamp works:

m

minutes (0 -59)

h

hour (0-23)

dom

day of the week (1 -31)

mon

month (1-12)

dow

day of the week (mon,tue,wed,thu,fri,sat,sun)

 If we take a second look at the example, we can read it like this:

m = 12

h = 6

dom = *

mon *

dow = *

This means that the cronjob will run on 6:12 AM every day.

What needs to be done

This part (task) can also be divided into two parts:

  • Program to use

  • Path of the file that needs to be executed

In the example we saw that we use PHP (Program) to execute the file cron.php in the path /home/domein1q/public_html/ . This means that the cronjob wild run on 6:12 AM every day and use PHP to execute the file cron.php that’s located in /home/domein1q/public_html/ .

NB! If you want to use php in your task as program you need to have the following line of code in your crontab:

PATH=/usr/local/bin/:/usr/bin:/bin

The cron job will be like this then:

PATH=/usr/local/bin/:/usr/bin:/bin

12 6 * * * php /home/domein1q/public_html/cron.php

If you don’t have this, you’ll have to replace php with on of the following:

- /usr/local/bin/php (this sues the default PHP version of the server)

- /opt/cpanel/ea-php70/root/usr/bin/php (If you’d like to use PHP 7)

- /opt/cpanel/ea-php56/root/usr/bin/php (If you’d like to use PHP 5.6)

Extra options

There are also some options you can give the cronjob that will alter the way it works. You can use the following options (there are way more but we only cover these two):

Mail the output of the cronjob to an email addres

If you add the following line of code above the cron task you’ll get an email with the result of the cron job: MAILTO= iets@domein.nl.

The cron job will be like this:

MAILTO= iets@domein.nl

12 6 * * * php /home/domein1q/public_html/cron.php

Write the output to /dev/null

If you don’t want any messages about your cronjobs you can write the output to dev/null with this: >> /dev/null 2>&1

The cronjob will be like this:

12 6 * * * php /home/domein1q/public_html/cron.php >> /dev/null 2>&1

How do I setup a cronjob?

Setting up a cron job can be done on two ways:

  • Through an option in cPanel

  • Use the crontab command in SSH

We’ll cover both methods.

Setup a cron job through cPanel

In cPanel you can use the option Cron jobs (under the Advanced category)

Click Cron jobs

You get the following page:

Cron setup page

You can do the following things on this page:

Cron Email: You can fill in the email address where you wish to email the output of your cron job to.

Add New Cron Job: You can add new cron jobs here. You can specify the timestamp and the command that needs to be executed. You can use the Common Settings to use a couple of predetermined timestamps, so you don’t have to fill it in yourself.

Current Cron Jobs: You can see your current cron jobs here and edit or delete them

Setup a cron job through SSH

If you’re using SSH you can use the crontab -e command to edit your crontab and add your cron jobs there.

Frequently asked questions

Do I need to use Wget or PHP for my cronjob?

Both programs can be used! We recommend using the one you prefer and works with the cron job you’re trying to add.


How can I setup my WordPress cron?

Check out article How do I manually set up the WordPress cronjob?


How can I setup my Magento cron?

Check out article How do I manually set up the Magento cronjob?


Why doesn’t my cron job work?

Most of the times this has to do with the task part of your command not properly figured. Contact support and we’ll see if we can help you out with your cron job.


Why are the tasks I set up in my CMS not executed through the cron job?

Unfortunately, we only can check out if the cron job had run or not. We can’t see why the setup tasks don’t work and that’s something you’ll have to check out for yourself. You could try flushing your cache or use a plugin to check if the tasks have been executed or not.

 

This knowledgebase article was last updated on: 14 December 2018

Heeft dit artikel je geholpen?

Status

Ga naar onze statuspagina voor een overzicht van recente storingen en onderhoud.

Openingstijden

Maandag — vrijdag 9:00 — 17:00