简体   繁体   English

无法使用exec命令运行PHP

[英]Can't run PHP using exec Command

I've a problem in running a continous background process in which the parent initiate 2 or 3 child process then end itself, child processes have heavy computation so they will take alot of time. 我在运行一个连续的后台进程时出现问题,其中父进程启动2或3子进程然后结束自己,子进程有大量计算因此它们将花费大量时间。

I'm using exec command to start the process but don't know it doesn't start neither it generates any error(error reporting is on,E_ALL and display_errors) 我正在使用exec命令启动进程,但不知道它没有启动它既不会生成任何错误(错误报告打开,E_ALL和display_errors)

Here is how I'm trying to do this 这是我试图这样做的方式

error_reporting(E_ALL);
ini_set('display_errors',1);
$output = '';
$dir = dirname(__FILE__).'/';
//$cmd = "nohup php {$dir}/background-service.php > /dev/null & echo $!";
$cmd = "nohup php background-service.php >/dev/null 2>&1 &";
exec($cmd );

background-service.php 背景service.php

<?php
ini_set('max_execution_time', 0);
ini_set('display_errors',1);

file_put_contents('a'.time().'.txt',"this is the test code");

?>

when I hit the file directly it generates a file, but not with exec, I've tested exec is enabled(ubuntu server) 当我直接命中文件时它会生成一个文件,但不能用exec,我已经测试过exec已启用(ubuntu服务器)

if ( $safe_mode = ini_get( 'safe_mode' ) && strtolower( $safe_mode ) != 'off' )
{
    echo 'Safe Mode is Disabled';
}
else
    echo 'Safe Mode is Enabled<br/>';


if ( in_array( 'exec', array_map( 'trim', explode( ',', ini_get( 'disable_functions' ) ) ) ) )
{
    echo 'exec is Disabled';
}
else
echo 'exec is Enabled<br/>';

Someone please tell me where I'm wrong in it, how can I detect if its disabled by server 有人请告诉我我错在哪里,如何通过服务器检测它是否被禁用

Thanks 谢谢

This is just a guess, but try using the full path of anything you're executing. 这只是猜测,但尝试使用您正在执行的任何内容的完整路径。 Running the command from a shell may work for YOUR user account, but keep in mind the process is running as the apache user. 从shell运行命令可能适用于您的用户帐户,但请记住,该进程正在以apache用户身份运行。

Also, using exec() with additional parameters may be useful for you. 此外,使用带有其他参数的exec()可能对您有用。 The third parameter can specify where to store the return status of the command (probably helping you figure this out). 第三个参数可以指定存储命令返回状态的位置(可能帮助您解决这个问题)。 (courtesy the PHP manual ) PHP手册提供

检查此safe-mode-exec-dir并且您不需要为命令行脚本设置最大执行时间,默认情况下它们没有限制。

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

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