Search code examples
phpapachecronapc

Running a PHP script with command line as if it was called by Apache


I'm using PHP's APC module's user cache. I'm using a cron job to call a script that updates variables. When I run the script through a browser, everything works fine. However, it can't store nor retrieve variables when I'm running the script through command line. I recall reading somewhere that APC stores the variables in the Apache process. With command line, I am running the script directly (without Apache), so APC user cache isn't working.

I could not find a way to get APC to work with command line (apc.enable_cli does not work). Therefore, I want to find a way to run a PHP script through command line as if it was called by Apache. The scripts aren't located in the document root, so I can't use a local browser. How can I use the command line to make Apache run a PHP script?

My line for cron looks something like this:

0 * * * * /path/to/php /path/to/script.php

Solution

  • Running php with php-cli you will never reach the same APC cache as the one used in Apache by mod_php. This is by design.

    When using apc.enable_cli you simply activate another APC cache, used for the lifetime of your script.

    So APC is not the right place to store a 'variable', as it is not a shared storage like a Database or a key/value storage would be.

    You have for example a very long thread on a drupal.org issue on how to empty a drupal cache set on APC while running in cli mode, and this usually means a two step mode, where you store on a shared storage an instruction that the mod_php version could read later, no direct access.

    I do not know any way to run mod_php in command line mode.