简体   繁体   English

从PHP应用程序运行PHP CLI脚本

[英]Running a PHP CLI Script from a PHP Application

I have a php file which needs to be executed in CLI from my Php application which runs on CodeIgniter framework. 我有一个PHP文件,需要从运行在CodeIgniter框架上的Php应用程序在CLI中执行。 I tried the exec and shell_exec command and it seems to not start the process. 我尝试了exec和shell_exec命令,但似乎没有启动该过程。 Please find the code below which I try to execute. 请找到下面我尝试执行的代码。

$phpPath = exec('which php');
$cmd = $phpPath.' '.dirname(__DIR__).'/API/notification/NotificationServer.php';
echo "<pre>".shell_exec($cmd)."</pre>";

When I try running the above in Terminal it executes fine. 当我尝试在Terminal中运行以上命令时,它可以正常运行。 I need to automatically trigger this once the user has a session set. 用户设置了会话后,我需要自动触发此操作。

The path/$cmd variable prints path / $ cmd变量打印

when I print the variable $cmd i get the below output, and the below when given in terminal works fine. 当我打印变量$ cmd时,我得到以下输出,而在终端中给出的以下效果很好。

/usr/bin/php /Users/lalithb/Desktop/Android/AndroidWS/API/notification/NotificationServer.php

the above command works fine in the Terminal. 上面的命令在终端中工作正常。 When i try to exec/shell_exec it, it is not working. 当我尝试执行exec / shell_exec时,它不起作用。

APACHE_ERROR LOGS ARE : APACHE_ERROR日志是:

sh: line 1:  2437 Trace/BPT trap: 5       /usr/bin/php
sh: line 1: /Users/lalithb/Desktop/Android/AndroidWS/API/notification/NotificationServer.php: Permission denied

Can someone help me out in running this above code in CLI ? 有人可以帮助我在CLI中运行以上代码吗?

I solved the problem, Guess there would be someone else like me trying to access the restricted folders. 我解决了这个问题,猜猜还会有其他人像我一样尝试访问受限制的文件夹。 I found the solution here 我在这里找到解决方案

http://ubuntuforums.org/showthread.php?t=1302259 http://ubuntuforums.org/showthread.php?t=1302259

exec('chmod +x'.dirname(__DIR__).'/API/notification/NotificationServer.php);

The above worked the magic. 以上成功了。

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

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