简体   繁体   English

如果我通过cron运行php脚本时使用相对路径或绝对路径是否重要?

[英]Does it matter if I use relative or absolute path's when running a php script via cron?

I am currently working on a script that I am attempting to automate via cron. 我目前正在编写一个脚本,我试图通过cron自动化。 Running the script via terminal is just fine, but when I attempt to run the script with crontab, I am getting some issues. 通过终端运行脚本很好,但是当我尝试使用crontab运行脚本时,我遇到了一些问题。

Part of my script loads and validates and xml file via DOMDocument::loadXML() and DOMDocument::validate() and php throws an error when attempting to validate stating: 我的脚本的一部分加载和验证和xml文件通过DOMDocument :: loadXML()DOMDocument :: validate()和php在尝试验证声明时引发错误:

Failed to load external entity: /linuxuser/homefolder/my_dtd.dtd

Within the xml file, the dtd is set to: 在xml文件中,dtd设置为:

../../../../../../../my_dtd.dtd

Is there some misconfiguration of the server or is it more likely something wrong with my php code at this point? 是否有一些服务器配置错误或者此时我的php代码更可能出错? It seems to grab my linux home directory rather than the path relative to the xml file. 它似乎抓住我的linux主目录而不是相对于xml文件的路径。 Just wondering if anyone else has seen an issue like this or could point me in the right direction. 只是想知道是否有人看到过这样的问题,或者可以指出我正确的方向。 Thanks. 谢谢。

问题很可能出在工作目录和解决相对路径上。

Quoting the PHP documentation for differences in CLI usage (command-line interface): 引用PHP文档以了解CLI使用的差异 (命令行界面):

The CLI SAPI does not change the current directory to the directory of the executed script! 在CLI SAPI 不会将当前目录切换到执行脚本的目录!

When PHP scripts are run via CRON, it is executed on the user's home directory. 当PHP脚本通过CRON运行时,它将在用户的主目录中执行。 You can either replace all relative path references used by the script to absolute, or place this on the start of the script: 您可以将脚本使用的所有相对路径引用替换为绝对路径,也可以将其放在脚本的开头:

chdir(dirname(__FILE__)); # for PHP 5.2.x and below
# or
chdir(__DIR__); # for PHP 5.3+

尝试xml文件中的绝对路径。

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

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