# Help with running cron from within a PHP script

**URL:** <https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426>\
**Category:** Product Support\
**Created:** [December 12, 2024, 4:39pm UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426 "2024-12-12T16:39:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![benhunt1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/benhunt1/32/13272_2.png) [@benhunt1](https://forum.mautic.org/u/benhunt1)\
**Post date:** [December 12, 2024, 4:39pm UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426/1 "2024-12-12T16:39:22Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![rahul.shinde](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/rahul.shinde/32/10438_2.png) [@rahul.shinde](https://forum.mautic.org/u/rahul.shinde)\
**Post date:** [December 13, 2024, 1:09pm UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426/2 "2024-12-13T13:09:44Z")

</div>

You can write a PHP script to run shell commands like the one you described. For executing shell commands, PHP has the [exec()](https://www.php.net/manual/en/function.exec.php), [shell\_exec](https://www.php.net/manual/en/function.shell-exec.php)(), and [passthru](https://www.php.net/manual/en/function.passthru.php)() 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.

```auto
<?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.

---

<div class="post-metadata">

**Author:** ![benhunt1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/benhunt1/32/13272_2.png) [@benhunt1](https://forum.mautic.org/u/benhunt1)\
**Post date:** [December 13, 2024, 1:30pm UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426/3 "2024-12-13T13:30:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![rahul.shinde](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/rahul.shinde/32/10438_2.png) [@rahul.shinde](https://forum.mautic.org/u/rahul.shinde)\
**Post date:** [December 13, 2024, 1:56pm UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426/4 "2024-12-13T13:56:25Z")

</div>

I’m glad that the solution helped you.

---

<div class="post-metadata">

**Author:** ![marcus42](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@marcus42](https://forum.mautic.org/u/marcus42)\
**Post date:** [December 14, 2024, 11:13am UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426/5 "2024-12-14T11:13:31Z")

</div>

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:

```auto
#!/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.

---

<div class="post-metadata">

**Author:** ![benhunt1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/benhunt1/32/13272_2.png) [@benhunt1](https://forum.mautic.org/u/benhunt1)\
**Post date:** [December 24, 2024, 6:56am UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426/6 "2024-12-24T06:56:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [December 24, 2024, 7:08am UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426/7 "2024-12-24T07:08:42Z")

</div>

> [@benhunt1](#):
>
> php /path/to/mautic/bin/console messenger:consume email

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.

---

<div class="post-metadata">

**Author:** ![benhunt1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/benhunt1/32/13272_2.png) [@benhunt1](https://forum.mautic.org/u/benhunt1)\
**Post date:** [December 24, 2024, 7:22am UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426/8 "2024-12-24T07:22:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [December 24, 2024, 7:24am UTC](https://forum.mautic.org/t/help-with-running-cron-from-within-a-php-script/34426/9 "2024-12-24T07:24:41Z")

</div>

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