Search code examples
phpshellcroncommand-line-interface

How can I force PHP Version for Command Line?


I am hosted with 1and1.com, and I have setup my files to be parsed with php5 using .htaccess.

But that only works in apache, and not in command line, which defaults to the server default php4.

So currently I can not setup cron jobs to run my code as php5. Any ideas?


Solution

  • If both are installed, all you need to do is run the script using the relevant PHP binary.

    So for example:

     // Runs using the PHP binary located at /usr/bin/php
     * * * * * root /usr/bin/php -n "/path/to/script.php"
    

    or

     // Runs using the PHP binary located at /var/php5
     * * * * * root /var/php5 -n "/path/to/script.php"
    

    All you need to know is the full file system path of the PHP CLI binaries, and call the relevant one to run your code.