Search code examples
phpcronlaravellaravel-4mcrypt

Laravel requires the Mcrypt PHP extension. Not working for cron jobs


So I am plagued with this issue like many others have been but with no solution.

The Issue: commands issued by a cron task do not run and give the message: Laravel requires the Mcrypt PHP extension.

I can run commands through artisan and they work fine. I am using MAMP on OSX 10.8.

I've quadrupal checked my .bash_profile to ensure the correct PATH is set which is: export PATH=/Applications/MAMP/bin/php/php5.4.4/bin:$PATH. Confirmed by which php in terminal. php -v confirms PHP 5.4.4 is being used. php -i confirms mcrypt extension is installed and enabled. Even adding die(phpversion().PHP_EOL); to vendor/laravel/framework/src/Illuminate/Foundation/start.php it's confirmed that it's using the correct version.

So I'm stumped. I don't know why cronjobs aren't recognizing either the correct PHP version or that the mcrypt extension is installed. What can I try?


Solution

  • Don't rely on PATH being set for cronjob through .bash_profile (it's a shell feature and cronjobs are not running through a shell), you should rather use something like

    * * * * * /Applications/MAMP/bin/php/php5.4.4/bin/php-something? /path/to/vendor/laravel/framework/src/Illuminate/Foundation/start.php
    

    A test could be something like this:

    # /tmp/test.php
    <?php file_put_contents('/tmp/a_test', `id`."\n".var_export($_ENV, true)."\n".var_export(extension_loaded('mcrypt'), true));
    # in crontab
    * * * * * /Applications/MAMP/bin/php/php5.4.4/bin/php-something? /tmp/test.php
    

    Run php /tmp/test.php one time manually to spot the differences between you running the script, and cron; and always try to use absolute paths in crontab (in this case to your php binary).