Mautic Cron Jobs for dummies & marketers too

Mautic cron jobs

Cron jobs are (supposed to be) the smartest and simplest way to automate things on a Linux server, and they are. However, it's design, deeply rooted on the darkest corners of the linux kernel, designed by the deep minds that created Linux itself, doesn't always present itself as obvious to us, marketers (or to any other kind of Linux Dummies), that's probably why soooo many people have issues with cron jobs. Yes, maybe this is something that should be solved within the Mautic interface, maybe someday... in the meantime, let's try to tackle this problem down, in a way even a marketer can do it :-)

 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 cron command:

This is the easiest one to solve, cause you can find the right answer everywhere, these are the basic commands that are needed to run Mautic:

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

2nd variable. The path:

Now, to be able to run these commands you need to know where the scripts are located in your server, here the thing gets more complicated, cause every OS has its own default path, every service provider will also add some extra folders (or not) and if you are using any sort of panel, well, no comment. But fear not!

Cause my good friend Robert Went gave me an idea and we've both worked tirelessly to give you all a universal solution to this problem. To successfully complete this project we had to work for literally thousands of... well, milliseconds. Here's the code and when you run it it will tell you your path and will give you an example that is very likely going to be your full cron command:

<?php
 echo 'Your Path is: ';echo $_SERVER['DOCUMENT_ROOT']; echo '/app/console';
 echo '   and your cron jobs will PROBABLY be: * * * * * php ';echo $_SERVER['DOCUMENT_ROOT']; echo '/app/console mautic:segments:update';
?>

To use it you first need to create a new empty file, call the file "path.php" and copy these 4 lines into it. Then save it and upload to your Mautic folder (using ftp for example).

Now open your browser and point it to your mautic installation, the proper url to run this script is almost the same one you use to access Mautic, but you will need to delete anything that you find after the domain name (probably "s/dashboard" or maybe "s/login") and add "rootpath.php" after that.

It should look something like this http://mauticsubdomain.yourdomain.com/rootpath.php (if you installed your Mautic copy on a subdomain)

or like this  http://www.yourdomain.com/mauticfolder/rootpath.php (if you installed mautic on a folder)

Take note of the path showing in your browser and then delete this file you've just created, we don't want anyone else knowing about your server's path.

3rd variable. The timing:

The timing is the last variable, or should I say the first? There are different ways to set this up but for simplicity sake we'll just use  * * * * *

I'm not going to discuss the details in this post, I'll write a new post soon explaining how to properly "time" your crons, for now you just need to know that * * * * * means every minute and that you need to add "PHP " (notice the extra space) after that so your server knows how to run the rest of the command.

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 3 parts as shown below:

* * * * *  PHP +Path +Command

The Path is what you got in the second step and the commands are the ones listed in the first step

so, let's say you ran the script and your path was "/var/www/html/"

Then your cron jobs would be like this:

* * * * *  PHP /var/www/html/ mautic:segments:update
* * * * *  PHP /var/www/html/ mautic:campaigns:rebuild
* * * * *  PHP /var/www/html/ mautic:campaigns:update
* * * * *  PHP /var/www/html/ mautic:campaigns:trigger

Notice there are spaces all over the place, you need to respect every space or you will get 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 simpler 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.

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

I will update this article with more details about the main hosting providers and panels, if you know the solution for any specific hosting company or panel please share it with me so I can add it here.

 

Please help me write more posts like this one by sharing, Thank you!
Log in to comment