Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to run Mautic (https://mautic.org) commands from a URL.
<?php
if (!isset($_GET['ILoveMauticReallyIDo'])) {
echo 'The secret phrase is wrong.';
die;
}
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$allowedTasks = array(
'cache:clear',
'mautic:leadlists:update',
'mautic:campaigns:update',
'mautic:campaigns:trigger',
'mautic:email:process',
'mautic:fetch:email',
'doctrine:migrations:migrate',
'doctrine:schema:update --dump-sql',
'doctrine:schema:update --force'
);
if (!isset($_GET['task'])) {
echo 'Specify what task to run. You can run these:';
foreach ($allowedTasks as $task) {
$href = $link . '&task=' . urlencode($task);
echo '<br><a href="' . $href . '">' . $href . '</a>';
}
echo '<br><a href="https://www.mautic.org/docs/setup/index.html">Read more</a>';
echo '<br><b style="color:red">Please, backup your database before executing the doctrine commands!</b>';
die;
}
$task = urldecode($_GET['task']);
if (!in_array($task, $allowedTasks)) {
echo 'Task ' . $task . ' is not allowed.';
die;
}
$fullCommand = explode(' ', $task);
$command = $fullCommand[0];
$argsCount = count($fullCommand) - 1;
$args = array('console', $command);
if ($argsCount) {
for ($i = 1; $i <= $argsCount; $i++) {
$args[] = $fullCommand[$i];
}
}
echo '<h3>Executing ' . implode(' ', $args) . '</h3>';
require_once __DIR__.'/app/bootstrap.php.cache';
require_once __DIR__.'/app/AppKernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\BufferedOutput;
defined('IN_MAUTIC_CONSOLE') or define('IN_MAUTIC_CONSOLE', 1);
try {
$input = new ArgvInput($args);
$output = new BufferedOutput();
$kernel = new AppKernel('prod', false);
$app = new Application($kernel);
$app->setAutoExit(false);
$result = $app->run($input, $output);
echo "<pre>\n".$output->fetch().'</pre>';
} catch (\Exception $exception) {
echo $exception->getMessage();
}
@escopecz

This comment has been minimized.

Owner

escopecz commented Aug 31, 2015

This script can be used in shared web hostings which doesn't allow to execute CLI commands to run Mautic cron and maintenance tasks.

How to make it work

  1. Create a PHP file in the root folder of Mautic.
  2. Copy and paste the code of the script above.
  3. Change ILoveMauticReallyIDo in that script to some secret phrase so only you'll be able to run the commands.
  4. Run a command by accessing this URL with your browser http://[yourmautic.com]/commands.php?ILoveMauticReallyIDo. Change[yourmautic.com] to your domain or wherever your Mautic runs and ILoveMauticReallyIDo to your secret phrase form 3.

How to use it

When you execute the URL form 4., you should get the list of available commands with URL addresses. You can try to click on those to run the command manually or insert those URL addresses to a cron UI provided by your web hosting company.

Or use Cronfig.io

I prepared a cron service at https://cronfig.io if you don't want to deal with a PHP script. Plus it will give you logs in your Mautic administration and monitoring.

@kuzmany

This comment has been minimized.

kuzmany commented Sep 3, 2015

Thank you so much. Our hosting provider don't accept run php commands on shared hosting.
This solution works great :)

@ninjoan

This comment has been minimized.

ninjoan commented Sep 23, 2015

Is not working for me im getting this error

[RuntimeException]
Failed to start the session because headers have already been sent by "/home/MYDOMAIN/public_html/marketing/commands.php" at line 59.

Any idea?

@escopecz

This comment has been minimized.

Owner

escopecz commented Oct 28, 2015

@ninjoan script updated. Try it now please.

@malcolmgroves

This comment has been minimized.

malcolmgroves commented Oct 28, 2015

@escopecz, I'm getting the similar error as @ninjoan

[RuntimeException]
Failed to start the session because headers have already been sent by "/home/MYDOMAIN/public_html/code-partners.com/ma/croncommands.php" at line 42.

@malcolmgroves

This comment has been minimized.

malcolmgroves commented Nov 10, 2015

If it helps, that message is what is being returned from $output->fetch() on line 56, which I'm guessing is the output from $app->run on the previous line.

@different-thinking

This comment has been minimized.

different-thinking commented Nov 15, 2015

Thanks a million @escopecz !!!
After commenting out line 42 the script worked!

When running campaigns:trigger I get: Script in progress. Use -f or --force to force execution.
running with -f or --force I get: Task mautic:campaigns:trigger -f is not allowed.

What to do?

Robert

@bandst

This comment has been minimized.

bandst commented Jul 12, 2016

I have updated from 1.4.1 to 2.0.0 version and the scrip doesn't work. When I execute the script the system sends me this message

Executing console mautic:campaigns:trigger

Fatal error: Uncaught Error: Class 'Symfony\Component\Console\Input\ArgvInput' not found in /homepages/path/2.0.0/commands.php:50 Stack trace: #0 {main} thrown in /homepages/path/2.0.0/commands.php on line 50

@joew2

This comment has been minimized.

joew2 commented Jul 13, 2016

I'm also getting the same error with version 2.0 and 2.0.1

Fatal error: Class 'Symfony\Component\Console\Input\ArgvInput' not found in XX

@bluekheng

This comment has been minimized.

bluekheng commented Jul 28, 2016

I'm also getting error. Trying to run the script from upgrade of 1.4.1 to 2.0.1.

Fatal error: Class 'Symfony\Component\Console\Input\ArgvInput' not found in XXX on line 50

@kuzmany

This comment has been minimized.

@eplaku

This comment has been minimized.

eplaku commented Jan 4, 2017

Guys I just upgraded to version 2.5 and it's not working

Example: Fatal error: Class 'Symfony\Component\HttpKernel\Kernel' not found in /home/albkrist/public_html/cms/app/AppKernel.php on line 24

Please advice.

@Luisfmarkes

This comment has been minimized.

Luisfmarkes commented Jan 28, 2017

me too!
Not working in 2.5

@niteshluharuka

This comment has been minimized.

niteshluharuka commented Feb 3, 2017

For me also on 2.5, its not working...

@7six

This comment has been minimized.

7six commented Feb 3, 2017

The same is happening to me.
I updated yesterday and I got that error.

@ChgoChad

This comment has been minimized.

ChgoChad commented Feb 3, 2017

Regarding Mautic 2.5, they made a few changes in how the back-end processes get loaded. I have updated the script and fixed it here:
https://gist.github.com/ChgoChad/fe9950c628ad8169cd27a58ee64106e8

This works in 2.5. I also added a few additional commands as well as some basic HTML formatting to the output.

@Paul979

This comment has been minimized.

Paul979 commented Feb 4, 2017

Thanks a ton ChgoChad! That finally got my crons working now.

I was getting cron syntax errors at my host, and Cronfig was giving me missing secret key 403 errors. But this now finally works :D

@7six

This comment has been minimized.

7six commented Feb 6, 2017

it's worked!

@khaoua

This comment has been minimized.

khaoua commented Feb 21, 2017

Hi Guys

I'm pretty new to Mautic, and have no coding skills (yet lol) I'm an SAP expert, but starting with those digital tools like this marketing automation tool that looks pretty efficient, so let me get time to get use with it :-)
Can you guys let me know how I can use this latest update concretely? I follow the steps with url as I have put the php file in our servers in mautic root directory, but when I insert in chrome navigator xxx.com/commands.php?secretphrase, nothing happens at all, I must have missed something as nothing occurs and only website is poping up
I've downloaded the latest zip file to proceed, but I'm a bit lost, so if you guys can show me the exact steps of what I should exactly do that would help
I could download the plugin available in this procedure here below
https://cronfig.io/

But we have only a max of 3 Crons runing in batch job, so if I could make a run of more to get more efficient, this script seems to work from the latest comments so if you can let me know how to proceed as pure beginner that would be great, many thanks in advance for your help

@khaoua

This comment has been minimized.

khaoua commented Feb 22, 2017

I tested it on my shared host, I put it in the root file of my website, called the script in URL and nothing happens, can you guys let me know what's the issue or if I missed something?

I've put it this way in the url: http://xxx.com/cronjobfile.php

it displays the wordpress initial website page, nothing else

any thought?

@khaoua

This comment has been minimized.

khaoua commented Feb 22, 2017

ok, sorry, forgot the secret phrase, and renamed correctly the file, so I did get the web page with all the links, when I click in one link, for example, leads update I get: "Executing console mautic:leadlists:update"
any info of where should we see the evolution or logs of those script to know that they went well?

@khaoua

This comment has been minimized.

khaoua commented Feb 22, 2017

ok, I've just opened a free account to test them on easycron, segment, campaign are failing, those are the only ones I've tested, but there is an issue, so, if you guys have some tips, feel free to share

@khaoua

This comment has been minimized.

khaoua commented Feb 22, 2017

Hello
I found the mistake, it works now on easycron, jobs succeed

when in a post above it said: "When you execute the URL form 4., you should get the list of available commands with URL addresses. You can try to click on those to run the command manually or insert those URL addresses to a cron UI provided by your web hosting company"

Actually, there is a clarification to be done: The links for crons shown after they are generated from initial link with secret phrase, they can be copied, but we should remove the secret phrase that is still in the url link, then, it works, at least it worked this way for me

@khaoua

This comment has been minimized.

khaoua commented Feb 22, 2017

Hello
I found the mistake, it works now on easycron, jobs succeed

when in a post above it said: "When you execute the URL form 4., you should get the list of available commands with URL addresses. You can try to click on those to run the command manually or insert those URL addresses to a cron UI provided by your web hosting company"

Actually, there is a clarification to be done: The links for crons shown after they are generated from initial link with secret phrase, they can be copied, but we should remove the secret phrase that is still in the url link, then, it works, at least it worked this way for me

@4evermaat

This comment has been minimized.

4evermaat commented May 2, 2018

@escopecz, @alanhartless, any particular reason why you have not merged the fork that updated your commands.php which seems to be outdated for more recent mautic installs:
https://gist.github.com/ChgoChad/fe9950c628ad8169cd27a58ee64106e8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment