Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a MAMP setup which runs PHP 5.3.5 on my Mac OS X 10.5 computer. I am trying to install a crontab that executes a PHP script, which is located on my MAMP server. I can only get the crontab to execute if I use the php installation from /usr/bin/php, which is version 5.2.15. In other words, this is the pre-installed MAC OS X installation of PHP. How can I use my MAMP's version of PHP when executing the crontab? I am not knowledgeable enough with unix to install a new version of PHP at /usr/bin/php, though would this work?

I want crontab to execute my PHP script using MAMP's version of PHP; this is because I know that the script runs successfully, and I get the desired output. However, when I try executing the crontab using the system default PHP installation at /usr/bin/php, I get Fatal PHP errors.

--

In case this might be useful, here is the outcome of /usr/bin/php vs. MAMP's version. The first code example executes my php script successfully:

$  MAMP_PHP_PATH="/applications/mamp/bin/php5.3/bin/php"
$  $MAMP_PHP_PATH -f /path/to/my/script.php

Now, here's what happens when I simply run the following command:

$  # whereis php in this case returns usr/bin/php
$  php -f /path/to/my/script.php

Results in the following error (formatting mine):

Fatal error: require_once(): 
Failed opening required '/path/to/includes/initialize.php' 
(include_path='.:') in /path/to/my/script.php on line 3

Finally, this is what my crontab file looks like (with MAMP's php):

15 * * * * /Applications/MAMP/bin/php5.3/bin/php /path/to/my/script.php

And without MAMP's php:

15 * * * * php /path/to/my/script.php

Thanks

share|improve this question
    
crontabs don't use shebang lines. –  Ignacio Vazquez-Abrams Jun 22 '12 at 16:17
    
@IgnacioVazquez-Abrams when I removed the shebang and simply added 15 * * * * /Applications/MAMP/bin/php5.3/bin/php /path/to/my/script.php, the crontab didn't even execute, as if there was some error...is there a path issue between crontab and the php installation location that I am specifying? If so, how can I fix this to get crontab to accept the php installation that I am specifying? Thanks! –  fettereddingoskidney Jun 22 '12 at 17:05
    
I have seen a bunch of crontabs that have the prepending variables: SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ –  Tim Jun 22 '12 at 19:22
    
@Tim, could you please expand a little more on your comment? I am not sure what setting HOME=/ will solve...can you elaborate? –  fettereddingoskidney Jun 22 '12 at 19:38
    
It was more of the PATH and SHELL that I thought might help you. MAILTO obviously will set email address logs are sent to. HOME probably helps with cases where cron doesn't have an active shell or home folder defined. I mostly thought that it might help since your stuff doesn't run when there is no shebang. –  Tim Jun 22 '12 at 20:20

2 Answers 2

Set the environment variables you need in the crontab. For example:

PATH=/Applications/MAMP/bin/php5.3/bin:/usr/local/bin:/usr/bin:/bin
MAMP_PHP_PATH=/applications/mamp/bin/php5.3/bin/php
15 * * * * php /path/to/my/script.php
share|improve this answer
up vote 1 down vote accepted

The SHORT answer: Ultimately, I just installed the newest version of PHP onto my system.

The LONG answer (and all the pain I endured along the way):
I kept getting an error when crontab would run, which stated that a certain class that I instantiated in my script – SoapClient – was not being found. My autoload function wasn't finding it either; hence, as shown in the OP, I was getting this error:

Fatal error: require_once(): 
Failed opening required '/path/to/includes/initialize.php' 
(include_path='.:') in /path/to/my/script.php on line 3

There was another similar error that I kept getting like this, and I discovered that the problem was that the old version of php did not have the SOAP extension enabled, and when the autoload function went looking for it, it checked the php installation's php.ini file for the line: include_path and checks the directories therein to find the SOAP class that I was trying to include. When it couldn't find the class, a Fatal Error resulted. Note: (include_path in your php.ini file works similar to the $PATH variable in your Unix enviornment).

I used the locate php.ini command and a little bit of intuition and found that my system's php.ini file was at /private/etc/php.ini.default. This was the location of the old php.ini file – the one for the php 5.2 version. Point is, soap was simply not enabled, and therefore the include_path parameter of my php.ini file was not finding its location.

So, I Downloaded PHP 5.4.4 and ran the following commands:

$ ./configure --enable-soap --with-mysql
$ make
$ make install

The installation was made in /usr/local/bin. However, the root php installation was in /usr/bin, so I did the following command to move all the contents of /usr/local/bin into /usr/bin, to overwrite the old php version:

$ sudo cp -Rvf /usr/local/bin/ /usr/bin

I specify:
-R to copy ALL the files within the /usr/local/bin/ heirarchy, -v to simply display an output message stating which files are moved as the process occurs, and -f, which allows me to overwrite the applicable files in /usr/bin as desired.

Once I overwrote the old version of PHP with the new version, the location of the new php.ini file was somewhere else. But where? I ran this to find out:

$ php -i | grep -i "configuration file"
  Configuration File (php.ini) Path => /usr/local/lib
  Loaded Configuration File => /usr/local/lib/php.ini

After making the applicable changes, I overwrote the file at /private/etc/php.ini.default with the new php.ini file that came with my php 5.4.4 installation.

Viola. The cron job is working and I didn't need to specify a different php path at all. Cheers!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.