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.