简体   繁体   English

使用命令行运行PHP脚本,就像Apache调用它一样

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

I'm using PHP's APC module's user cache. 我正在使用PHP的APC模块的用户缓存。 I'm using a cron job to call a script that updates variables. 我正在使用cron作业来调用更新变量的脚本。 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. 我记得在某处看到APC将变量存储在Apache进程中。 With command line, I am running the script directly (without Apache), so APC user cache isn't working. 使用命令行,我直接运行脚本(没有Apache),因此APC用户缓存不起作用。

I could not find a way to get APC to work with command line ( apc.enable_cli does not work). 我找不到让APC使用命令行的方法( apc.enable_cli不起作用)。 Therefore, I want to find a way to run a PHP script through command line as if it was called by Apache. 因此,我想找到一种通过命令行运行PHP脚本的方法,就像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? 如何使用命令行使Apache运行PHP脚本?

My line for cron looks something like this: 我的cron行看起来像这样:

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

Running php with php-cli you will never reach the same APC cache as the one used in Apache by mod_php. 使用php-cli运行php,你将永远不会达到与mod_php在Apache中使用的缓存相同的APC缓存。 This is by design . 这是设计的

When using apc.enable_cli you simply activate another APC cache, used for the lifetime of your script. 使用apc.enable_cli您只需激活另一个APC缓存,用于脚本的生命周期。

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. 因此,APC不是存储“变量”的正确位置,因为它不是像数据库或键/值存储那样的共享存储。

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. 例如,你在drupal.org问题上有一个非常长的线程,关于如何在cli模式下运行时清空APC上的drupal缓存集 ,这通常意味着一个两步模式,你在共享存储上存储一条指令, mod_php版本可以稍后阅读,无法直接访问。

I do not know any way to run mod_php in command line mode. 我不知道在命令行模式下运行mod_php的方法。

Maybe you should use php-cgi version. 也许你应该使用php-cgi版本。 When you run php with terminal you use php cli version, but it can have another settings than php-cgi. 当您使用终端运行php时,您使用的是php cli版本,但它可以拥有除php-cgi之外的其他设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM