Mautic Cron Jobs – The Full Guide for 2020

Mautic Cron Jobs

Mautic cron jobs automate several repetitive tasks that Mautic needs to perform in order to run properly. Mautic Cron jobs also allow you to control the timings on each of those processes.

This is the complete and most updated guide on Mautic Cron jobs, now revisited in 2020 so you can take Mautic Cron Jobs to the top-level and automate Mautic tasks with Crontab.

This is a 3-level series about Mautic Cron Jobs, each guide is suited for users with different levels of skills: Newbie, Rookie or PRO.

Cron jobs are about the smartest and simplest way to automate script execution on a Linux server, however, they were designed by the great and deep minds that created Linux itself and are deeply rooted in the darkest corners of the Linux kernel, so they don’t usually present themselves as simple or obvious to most of us.
This is probably why so many people have issues with Mautic cron jobs.

This is a 3 level guide, choose the right level for your own skillset.
If you start at the Newbie lvl, you will be able to also complete the other 2 lvls without any issue, later on.

Lvl 1 – Mautic Cron Jobs for Newbies:

An introductory guide to cronjobs and how to set them for Mautic. If you are new to Cron Jobs and/or to Mautic, you might want to start here.
If you are installing Mautic cron jobs for the first time or for some reason your mautic cron jobs are not working, the lvl1 guide will help you get on your feet.

Lvl 2 – Mautic Cron Jobs: Which ones to use:

Once you know the basics of how to setup Cron Jobs for Mautic, the next step is to learn about the different Mautic Cron Jobs you can run, which ones you’ll be needing and what each cron job can do for you.

Lvl3 – Mautic Cron Jobs: Fine-tunning the timings.

After you have decided which Cron Jobs to use it is time to fine-tune the timings, here are some ideas based on my experience.

Lvl 1 – Mautic Cron Jobs for Newbies:


What are Mautic cron jobs?

Technically there aren’t any Mautic cron jobs per se, what we all use are Mautic commands that we happen to trigger using a crontab.

A Mautic command is an action or series of actions found within the Mautic code, these commands can be triggered from any line of code within Mautic or via your own code, also directly on the CLI console or, with cron jobs of course.

Cron jobs are a way to schedule tasks in Linux, it’s basically a list of tasks to be executed at certain times, so yes it’s kind of an alarm clock which instead of ringing, runs a command in the server when the time is right.

What we call Mautic cron jobs are in fact Mautic commands run by the cron scheduler at predefined intervals.

In order to be able to properly set the Mautic cron jobs for your specific server/hosting, there are 3 variables that you need to solve first, we will divide the problem into these 3 variables and in the end, we will wrap it all, let’s get started!

1st variable. The Mautic command:

There are many Mautic commands we can run as a cron job, this is maybe the simplest variable to solve, in the next lvl you will find the full list of Mautic Cron jobs you can use, for now let’s focus on the 3 mandatory and most used ones cron jobs for Mautic:

  • mautic:segments:update
  • mautic:campaigns:update
  • mautic:campaigns:trigger

For now you only need to know they exist…

2nd variable. The path:

Now, to be able to run these commands you need to tell ¨cron¨where to find the scripts you want to run.
The Mautic commands we are going to use as cron jobs are actually found within your Mautic directory, the problem is, the exact placement of these files is completely arbitrary… Each OS and each Linux Distribution uses different default places or paths to store your Mautic files, on top of that, every service provider and hosting company will also add some extra folders (or not) and if you are using any sort of panel or installation script, these also use different folder structures of their own.

So basically, the right path for your Cron Jobs is the path of your Mautic directory + app/console.

If you are having a hard time finding out the right path for your Mautic installation, you can run the following command on your Linux server:

For servers running Debian, Ubuntu and Derivatives use:

grep -i 'DocumentRoot' /etc/apache2/sites-available/000-default.conf

For servers running CentOSRHEL and Fedora Linux distributions, run:

$ grep -i 'DocumentRoot' /etc/httpd/conf/httpd.conf

This command will return something like:

DocumentRoot /var/www/html/mautic

But again, it could be anything… Take note of the output, you’re gonna need it later.

3rd variable. The timing:

The timing is the last variable we are going to work with, there are different ways to set this up but for simplicity sake, we’ll just use  * * * * *

I’m not going to discuss the details about cron job timings in this part of the guide, this will be analized in detail in the Lvl3 section. For now, you just need to know that * * * * * means every minute.
 

Let’s put it all together!

We’ve done the work, and now it’s time to reap the rewards…
Every cron job is basically built with the same parts, your cron jobs will have 4 parts as shown below:

The time the cron job will run at goes first, use * * * * *
Then add the text “php” to tell the server which interpreter it has to use.
Then add the “path/toyour/files” we found in step 2, our DocumentRoot.
And finally add the Mautic command you want cron to run for you.

So, let’s say your path (DocumentRoot) is “/var/www/html/” 

Then your cron jobs would be like this:

* * * * * php /var/www/html/app/console mautic:segments:update
* * * * * php /var/www/html/app/console mautic:campaigns:update
* * * * * php /var/www/html/app/console mautic:campaigns:trigger

Notice there are spaces before and after PHP and one more after console, you need to respect every space or you will get in trouble.
 

Install your cron jobs!

If you run Mautic on a VPS and logically have access to your server via ssh, you now can add these commands to your crontab, for that just log into your server via SSH and type:

sudo crontab -e

If this is the first time you edit your crontab it will ask you to choose an editor, (choose “nano” it’s probably the simplest one to get started with).
You can now write your commands on the editor, it doesn’t really matter where but you usually add new stuff at the end of the file, one command per line.

If you are using some shared hosting or a control panel, you might not have direct access to your crontab, I can’t help you in this situation cause each provider and each panel has its own way of doing things, if that is the case, you will have to ask your service provider for instructions.

Hope this was instructive and remember you can ask questions in the comments here below if any of the steps don’t work as expected on your own setup.