我该如何解决为什么我的PHP脚本不会cron的工作时,它的命令行? [英] How can I troubleshoot why my PHP script won't work in cron when it does from the command line?

查看:151
本文介绍了我该如何解决为什么我的PHP脚本不会cron的工作时,它的命令行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有调用两个函数,A和B,在同一类的脚本。 A创建亚马逊的虚拟服务器和B击毁一,双方通过了shell_exec()的亚马逊的命令行工具。该脚本,doActions.php,拉从队列动作。如果操作为创建,它创建一个实例;当动作是消灭它杀死的。

该脚本工作正常执行A和B,当我在命令行中执行它:PHP的script.php

当我把它放在一个cron,它运行,但只成功运行的B功能。它删除破坏的实例,但不会创建它们。

故障点显然是函数B.它闷死在第一和最重要的了shell_exec,返回和呼应什么。

 回声$字符串=了shell_exec('/家庭/用户/的public_html / domain.com /私营/ EC2-API工具/斌/ EC2-运行情况AMI-23b6534a -k GSG-密钥对-z美国东部-1A');
 

除非你知道具体的关于道路亚马逊的命令行工具的工作的东西,请建议我的原因一个可能了shell_exec工作在一种情况下,而不是其他。

在同一地点的另一了shell_exec行为与预期:

 回声$字符串=了shell_exec('回声溢出');
 

我的猜测是,它必须做一些与权限。但是,当我有它运行了shell_exec('WHOAMI')它返回根,当I 和运行命令它工作正常。我有一个很难思维创造性的方式来解决,为什么我的PHP脚本将不会在cron的工作时,它的命令行。你能推荐一些?

解决方案

在一些运行在命令行,但拒绝在的cron 这样做,它往往是一个环境问题(路径或多数民众赞成你所需要正在运行的code其他一些环境变量)。

一开始,你应该修改脚本输出电流环境下(了shell_exec('ENV')?)是在顶部和检查命令行输出和cron。

希望,会有一些明显的如 AMAZON_EC2_VITAL_VAR 但是,如果没有,你应该将cron的环境对你的命令行之一,一个变量的时间,直到它开始工作。

一个简单的测试,以确定这一点。从你的命令行,这样做:

  ENV> /tmp/pax_env.sh
 

然后从shell脚本首先执行其中运行PHP脚本

 。 /tmp/pax_env.sh
 

,以使环境是相同的。

和记住,自己不会给你同样的环境,你会得到直接作为特定用户(<$ C登录$ C>苏 - 确实,我的认为的)。您可能要检查,当你以root直接登录的行为。


回复您的评论:

  

是的,我相信你已经明白了。我可能会标记为正确的答案,但需要你通过你巧妙地解决了一些增编受苦。首先,什么是执行pax_env.sh脚本的最佳方式?难道了shell_exec()的工作?

决不让它说我没有为我的钱:-)号工作了shell_exec 将几乎肯定会运行一个子shell这样的变量会设置的的该子壳,但不会影响到PHP的父进程。

我的建议是,如果你想让所有这些变量设定,将创建一个shell脚本,包括所有的命令在 /tmp/pax_env.sh (大概$ p $有pfixing每个导出),其次是你目前在的cron 运行命令,沿着线的东西:

 导出PATH =:在/ usr /斌
出口PS1 =的Urk:
出口PS2 = MoreUrk:
/home/user/pax/scriptB.php
 

然后运行的的从剧本的cron ,而不是 /home/user/pax/scriptB.php 直接。这将确保环境设置你的PHP code调用之前。

细心的读者可能已经注意到了那句如果你想让所有这些变量设定上面。我不个人认为这是一个好主意,倾倒的所有的命令行变量放到shell脚本的的cron 的工作。我想preFER实际找出哪些是需要的人,只包括那些。这减轻您的cron作业已在运行的污染。例如,这是不太可能的 PS1 / PS2 提示符变量将需要你的PHP脚本。

如果一切正常,你的可以的设置所有的环境变量 - 我只是preFER绝对最低,所以我不用太担心,当事情变

找出什么是需要的一种方式是注释掉一个导出在一个时间,直到你的脚本再次打破。然后,你知道的变量是必要的。一旦它与出口语句注释掉的最高金额,你可以完全删除这些评论导出报表,哪些仍然存在,但不可能也一定是好(与道歉爵士阿瑟·柯南·道尔)。

I've got a script that calls two functions, A and B, from the same class. A creates an Amazon virtual server and B destroys one, both via shell_exec()'s of Amazon's command line tools. The script, doActions.php, pulls actions from a queue. If the action is "create" it creates an instance; when the action is "destroy" it kills one.

The script works fine to execute both A and B when I execute it from the command line: php script.php.

When I put it on a cron, it runs but only successfully runs the B function. It deletes destroys instances but won't create them.

The point of failure is clearly function B. It chokes at the first and most important shell_exec, returning and echoing nothing.

echo $string = shell_exec('/home/user/public_html/domain.com/private/ec2-api-tools/bin/ec2-run-instances ami-23b6534a -k gsg-keypair -z us-east-1a');

Unless you know something specific about the way Amazon's command line tools work, please suggest to me reasons why a shell_exec might work in one case and not the other.

Another shell_exec in the same place behaves as expected:

echo $string = shell_exec ('echo overflow');

My guess is that it has to do something with permissions. But when I have it run shell_exec('whoami') it return "root," and when I su and run the command it works fine. I'm having a hard time thinking of creative ways to troubleshoot why my PHP script won't work in cron when it does from the command line. Can you suggest some?

解决方案

When something runs from the command line but refuses to do so within cron, it's often an environment issue (path or some other environment variable that's needed by the code you're running).

For a start you should modify the script to output the current environment (shell_exec('env')?) at the very top and examine the output from the command line and cron.

Hopefully, there will be something obvious such as AMAZON_EC2_VITAL_VAR but, if not, you should move the cron environment towards your command line one, one variable at a time, until it starts working.

A quick test to ascertain this. From your command line, do:

env >/tmp/pax_env.sh

Then run your PHP script from a shell script which first executes:

. /tmp/pax_env.sh

so that the environments are identical.

And keep in mind that su on its own doesn't give you the same environment as you'd get from logging in directly as a specific user (su - does, I think). You may want to check the behaviour for when you log in as root directly.


Re your comment:

Yes, I do believe you've got it. I'm likely going to mark your answer as correct but need you to suffer through a few addendums about your clever solution. First of all, what's the best way to execute the pax_env.sh script? Does shell_exec() work?

Never let it be said I didn't work for my money :-) No. The shell_exec will almost certainly be running a sub-shell so the variables would be set in that sub-shell but would not affect the PHP parent process.

My advice, if you wanted all those variables set, would be to create a shell-script consisting of all the commands in /tmp/pax_env.sh (probably prefixing each with export) followed by the command you currently have running in cron, something along the lines of:

export PATH=.:/usr/bin
export PS1=Urk:
export PS2=MoreUrk:
/home/user/pax/scriptB.php

Then run that script from cron rather than /home/user/pax/scriptB.php directly. That will ensure the environment is set up before your PHP code is called.

Astute readers will have noticed the phrase "if you wanted all those variables set" above. I don't personally think it's a good idea to dump all your command line variables into the shell script for the cron job. I'd prefer to actually find out which ones are needed and only include those. That lessens the pollution your cron job has to run under. For example, it's unlikely that the PS1/PS2 prompt variables will be required for your PHP script.

If it works, you can set all the environment variables - I just prefer the absolute minimum so I don't have to worry too much when things change.

A way of finding out what's needed is to comment out one export at a time until your script breaks again. Then you know that variable is needed. Once it works with the maximum amount of export statements commented out, you can just delete those commented export statements altogether and what remains, however improbable, must be okay (with apologies to Sir Arthur Conan Doyle).

这篇关于我该如何解决为什么我的PHP脚本不会cron的工作时,它的命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆