0

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?

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123

2 Answers2

3

This is probably a PHP problem, look at the code reponsible for that message:

if ( ! extension_loaded('mcrypt'))
{
    die('Laravel requires the Mcrypt PHP extension.'.PHP_EOL);

    exit(1);
}

Test your php on the command line running:

php -r 'echo PHP_EOL . (extension_loaded("mcrypt") ? "loaded" : "not loaded") . PHP_EOL . PHP_EOL;'

MCrypt is installed and available to php? Test it running:

php -i | grep  mcrypt

It must show you at least:

mcrypt support => enabled
mcrypt_filter support => enabled

EDIT:

Another possibility is cron running a different php (php, php-cli, php-cgi) and when you explicitly selected the right one it worked. Take a look at all your php.ini files to see if mcrypt is enabled in all of them.

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • returns `loaded` & both `mcrypt support` & `mcrypt_filter` are enabled – Jared Eitnier Oct 08 '13 at 14:55
  • Ok, so I just set the php path explicitly to MAMP's PHP path in my crontab command. Previously I was just using `* * * * * php /path/to/my/script.php`. It works now but I don't want to have to explicitly set the path. Can I fix that? – Jared Eitnier Oct 08 '13 at 14:57
3

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).

chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • 1
    So, essentially you are saying that best practice should be to set the absolute path in a cron command? I didn't know that. Isn't there a way to tell the version of php running CLI commands to use the same version as my PATH? – Jared Eitnier Oct 08 '13 at 15:08
  • @JaredEitnier: you could do `* * * * * . /home/your_user/.bash_profile && php /path/to/vendor/laravel/framework/src/Illuminate/Foundation/start.php`(more info about . or "source" here: http://superuser.com/questions/46139/what-does-source-do) – chelmertz Oct 08 '13 at 15:10
  • 1
    I'll stick to absolute path instead :) Thank you! – Jared Eitnier Oct 08 '13 at 15:16