Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php错误日志文件中获取命令行错误?_Php_Bash_Curl_Stderr_Error Log - Fatal编程技术网

如何在php错误日志文件中获取命令行错误?

如何在php错误日志文件中获取命令行错误?,php,bash,curl,stderr,error-log,Php,Bash,Curl,Stderr,Error Log,我试图在php文件中运行curl命令,并试图在错误日志文件中列出它的输出。除了从exec()函数中得到的错误之外,所有php错误都列在文件中。我正在尝试的php代码是: exec("nohup curl --output ".$_SERVER['DOCUMENT_ROOT']."/abc.mp3 http://192.99.8.170:8098/stream/1; --max-time $time_in_seconds > /devnull&"); 任何人都可以提供帮助,以便我可

我试图在php文件中运行
curl
命令,并试图在错误日志文件中列出它的输出。除了从
exec()
函数中得到的错误之外,所有php错误都列在文件中。我正在尝试的php代码是:

exec("nohup curl --output ".$_SERVER['DOCUMENT_ROOT']."/abc.mp3 http://192.99.8.170:8098/stream/1; --max-time $time_in_seconds > /devnull&");
任何人都可以提供帮助,以便我可以在错误日志文件中获取此命令生成的错误。我在谷歌上搜索过这个,但没有得到足够的结果

谢谢

命令 命令本身有几个问题

exec(“nohup curl--output”。$\u SERVER['DOCUMENT\u ROOT']。 “/abc.mp3;--最大时间$time(以秒为单位)>/devnull&;”

(为了清晰起见,我已将原始行包装起来。)

无需输入
nohup

nohup
命令的主要目的是,如果您由于终端线路挂起而从shell注销,则让进程运行。(当shell退出时,它向其子进程发送SIGHUP。)使用
nohup
可以启动命令,然后注销shell

curl
命令确实在SIGHUP时终止。您可以使用
nohup
将下载过程置于后台运行。但您的目标是捕获命令的输出,无论是STDIN还是STDOUT。了解命令何时完成工作以及如何完成工作也很重要。例如,同步执行命令后,可以检查退出代码(零表示成功),并检查文件是否实际下载到文件系统:

$output_dir = empty($_SERVER['DOCUMENT_ROOT']) ?
'/tmp/default-download-dir' : $_SERVER['DOCUMENT_ROOT'];
if (!is_dir($output_dir)) {
  if (!mkdir($output_dir, 0755, true))
    die("Failed to create directory $output_dir");
}

$url = 'http://php.net/images/logo.php';
$output_file = "{$output_dir}/logo.svg"; ;
// 2>&1 redirects standard error descriptor (2)
// to the standard output descriptor (1)
$command = sprintf("curl -o %s %s 2>&1",
  escapeshellarg($output_file),
  escapeshellarg($url));
$output = [];
exec($command, $output, $exit_code);
if ($exit_code != 0) {
  die("Failed to download $url to $output_file");
}

clearstatcache(true, $output_file);
if (!file_exists($output_file)) {
  die("File $output_file not found on file system, command: $command");
}

echo "$url has been saved to $output_file:\n",
  implode(PHP_EOL, $output), PHP_EOL;
输出

注意
escapeshellarg
函数的使用。您应该转义所有shell参数,否则shell将解释其特殊字符

分号

用分号(
)分隔的字符串被解释为不同的命令。因此,您的
--max time$time(以秒为单位)>/devnull&
被解释为第二个命令。我不知道你试图实现什么,但你很可能不需要它

重定向到
/dev/null

您的
/devnull&
表达式中有一个输入错误。您可能是指重定向到
/dev/null
伪设备:
/dev/null

将输出捕获到不同的文件描述符 在上面的代码中,我们捕获了标准输出。如果还想将输出捕获到其他文件描述符,则应使用
proc\u open()
函数:

$output_dir = empty($_SERVER['DOCUMENT_ROOT']) ?
  '/tmp/default-download-dir' : $_SERVER['DOCUMENT_ROOT'];
if (!is_dir($output_dir)) {
  if (!mkdir($output_dir, 0755, true))
    die("Failed to create directory $output_dir");
}

$url = 'http://php.net/images/logo.php';
$output_file = "{$output_dir}/logo.svg"; ;
$command = sprintf("curl -o %s %s",
  escapeshellarg($output_file),
  escapeshellarg($url));

$descriptors = [
  1 => ['pipe', 'w'], // stdout
  2 => ['pipe', 'w'], // stderr
];

$proc = proc_open($command, $descriptors, $pipes);
if (!is_resource($proc))
  die("Failed to open process for command $command");

if ($output = stream_get_contents($pipes[1]))
  echo "Output: $output\n\n";
fclose($pipes[1]);

if ($errors = stream_get_contents($pipes[2]))
  echo "Errors: >>>>>>>>\n$errors\n<<<<<<<\n\n";
fclose($pipes[2]);

$exit_code = proc_close($proc);

if ($exit_code != 0) {
  die("Failed to download $url to $output_file");
}

clearstatcache(true, $output_file);
if (!file_exists($output_file)) {
  die("File $output_file not found on file system, command: $command");
}

echo "$url\nhas been saved to $output_file\n";

请阅读有关输出流重定向的信息。您当前忽略命令中的错误输出。你必须重定向它。添加一个
2>/path/to/logfile
,或者更好的是,添加一个
2>&1
,将stderr重定向到stdout,从而允许
exec
调用接收它。使用PHP,您只能使用
exec($command,&$output)
的第二个参数从stdout获取输出。您可以做的是将curl stderr输出重定向到stdout,或者使用
proc\u open()
而不是
exec()
我将上述代码替换为
exec(“nohup curl--output”。$\u SERVER['DOCUMENT\u ROOT']./abc.mp3http://192.99.8.170:8098/stream/1--最大时间$time(以秒为单位)2>&1&echo$!”并使用
proc\u open()
我做了类似于
proc\u open(“nohup curl--output”。$\u SERVER['DOCUMENT\u ROOT')。/abc.mp3http://192.99.8.170:8098/stream/1;--最大时间$time_(以秒为单位),数组(),$foo)但它不会在错误文件中生成任何内容。你能写一个示例代码吗。
$output_dir = empty($_SERVER['DOCUMENT_ROOT']) ?
  '/tmp/default-download-dir' : $_SERVER['DOCUMENT_ROOT'];
if (!is_dir($output_dir)) {
  if (!mkdir($output_dir, 0755, true))
    die("Failed to create directory $output_dir");
}

$url = 'http://php.net/images/logo.php';
$output_file = "{$output_dir}/logo.svg"; ;
$command = sprintf("curl -o %s %s",
  escapeshellarg($output_file),
  escapeshellarg($url));

$descriptors = [
  1 => ['pipe', 'w'], // stdout
  2 => ['pipe', 'w'], // stderr
];

$proc = proc_open($command, $descriptors, $pipes);
if (!is_resource($proc))
  die("Failed to open process for command $command");

if ($output = stream_get_contents($pipes[1]))
  echo "Output: $output\n\n";
fclose($pipes[1]);

if ($errors = stream_get_contents($pipes[2]))
  echo "Errors: >>>>>>>>\n$errors\n<<<<<<<\n\n";
fclose($pipes[2]);

$exit_code = proc_close($proc);

if ($exit_code != 0) {
  die("Failed to download $url to $output_file");
}

clearstatcache(true, $output_file);
if (!file_exists($output_file)) {
  die("File $output_file not found on file system, command: $command");
}

echo "$url\nhas been saved to $output_file\n";
Errors: >>>>>>>>
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2202  100  2202    0     0   4231      0 --:--:-- --:--:-- --:--:--  4735

<<<<<<<

http://php.net/images/logo.php
has been saved to /tmp/default-download-dir/logo.svg
$url = 'http://192.99.8.170:8098/stream/1';
$command = sprintf("curl -o %s %s --max-time $time_in_seconds",
  escapeshellarg($output_file),
  escapeshellarg($url));