Help with running cron from within a PHP script

My hosting provider allows me to configure cron tasks that run specific php scripts, but not specific standalone commands.

I’m guessing I could collect a few commands into a single PHP file (or multiple) and fire those scripts say every 5 minutes on the server.

I’m not really expert in OO PHP though. Is there any guide to the syntax for running a command like this from within a PHP script?

php /path/to/mautic/bin/console messenger:consume email

You can write a PHP script to run shell commands like the one you described. For executing shell commands, PHP has the exec(), shell_exec(), and passthru() methods.

I suggest using the exec.

The following file, mautic-commands.php , contains the solution you want. Please Update the list of commands you want to group.

<?php

// List of commands
$commands = [
    'php /path/to/mautic/bin/console messenger:consume email',
    'php /path/to/mautic/bin/console cache:clear',
];

foreach ($commands as $command) {
    $output = [];
    $returnVar = 0;

    exec($command, $output, $returnVar);

    if ($returnVar === 0) {
        echo "Command '$command' executed successfully:\n";
        echo implode("\n", $output);
    } else {
        echo "Command '$command' failed with return code $returnVar.\n";
        echo "Output:\n";
        echo implode("\n", $output);
    }
    echo "\n";
}

Then include the php /path/to/mautic-commands.php to the cron.

1 Like

Thanks brother, that’s great. Now I find my host has disabled exec() so I’m having to go back to them, but I’m getting closer. Really appreciate your help, thank you!

I’m glad that the solution helped you.

What you’ve reported sounds phony baloney to me;

but not specific standalone commands.

You sure about that and not that your cronjob entry is interpreting the args as a new cmd?

I would first try to double quote the CMD.

i.e.
* * * * * /usr/local/bin/php "/path/to/mautic/bin/console messenger:consume email"

If that fail, you don’t need to have exec to run a PHP script…

How to do it;
Create a shell file with that content:

#!/usr/bin/bash

# First CMD
/usr/local/bin/php /path/to/mautic/bin/console messenger:consume email

# Second CMD
/usr/local/bin/php /path/to/mautic/bin/console messenger:consume email

# Third CMD
/usr/local/bin/php /path/to/mautic/bin/console messenger:consume email

#  Add more lines for more CMD

Save it and then run chmod a+x on it

From there, in your crontab, add your conrjob to run every now and then.
* * * * * /path/to/your/shell/script

I’ll also suggest to make use of “flock” (probably already installed on your server). This would avoid overlapping the cronjob.

That fixed the issue for me. My Control Panel (which is a custom one, not CPanel) did have an option to enable exec() and other similar functions, so once I did that, all good. Your script worked great, thanks @rahul.shinde

So this will crash your server within a very short time.

php /path/to/mautic/bin/console messenger:consume email

You need to set time parameters like --time-limit=50 if you want to run it for 50 sec.

Well actually I’m getting emails that Mautic is unpublishing my campaign:

“Due to repeated event failures, your [***]) campaign has been unpublished.”

I haven’t had time to look into this in detail yet, but was wondering if it was due to testing with dummy emails that can’t be delivered. Or could it be related to the messenger:consume email command??

Any help greatly appreciated.

Check a contact who had errors, click on the icon next to the error, then you know what the issue is.