【问题标题】:How to debug shell process execution by php script?如何通过php脚本调试shell进程执行?
【发布时间】:2015-07-28 03:39:35
【问题描述】:

我有一个 php 脚本,它应该在机器上执行 perl 脚本并打印进程 ID。执行的时候发现进程id被打印出来了,但是查看进程列表却找不到perl脚本正在运行的进程。

我将命令记录到文件中,发现命令是正确的,并且从 shell 执行它可以正确执行脚本。这两个脚本都归 www-data 所有。

if (isset($_GET['path'])) {
   $spath=$_GET['path'];
   $cmd="/usr/bin/perl ".getcwd().'/rotten2torrent.pl "'.$spath.'"';
   $outputfile="tmpfile";
   $pidfile="pid";
   if (isset($_GET['recursion'])) {
      $recursion=1;
      $cmd=getcwd().'/htmlrscrape.pl '.$spath;
   } else {
      $recursion=0;
   }  
   $command =  $cmd . ' > /dev/null 2>&1 & echo $!; ';
    $pid = exec($command, $output, $return);
   fwrite($logfile, "\n". "Command: ".$cmd);
   print 'Download started with PID '.$pid;
   fwrite($logfile, "\n". "Download started with PID ".$pid);
   fwrite($logfile, "\n". "Output lines: ".$pid);
    fwrite($logfile, "\n". "Return code: ".$return);
   foreach ($output as &$value) {
    fwrite($logfile, "\n". $value); 
   }
} 

日志文件输出:

#cat dldebug.log
http://www.rottentomatoes.com/top/bestofrt/top_100_horror_movies/?category=10 test
Command: /usr/bin/perl /var/www/rotten2torrent.pl "http://www.rottentomatoes.com/top/bestofrt/top_100_horror_movies/?category=10"
Download started with PID 13147
Output lines: 13147
Return code: 0
13147
Done

我还查看了 apache2 访问和错误日​​志,但没有显示错误:

tac /var/log/apache2/access.log | less

ip1 - - [16/May/2015:12:18:13 +0530] "HEAD / HTTP/1.1" 200 285 "-" "Cloud mapping experiment. Contact research@pdrlabs.net"
ip2 - - [16/May/2015:11:54:41 +0530] "GET /dlnow.php?path=http%3A%2F%2Fwww.rottentomatoes.com%2Ftop%2Fbestofrt%2Ftop_100_horror_movies%2F%3Fcategory%3D10&action=test HTTP/1.1" 200 321 "http://199.204.187.162/dlbox.php" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36"

我该如何调试呢? 我假设 shell 进程执行开始但随后被系统杀死。如何监控显示的错误?

exec 不是我服务器上 php.ini 中的禁用功能。我已经用 php exec 在服务器上测试了一个 'ls -l' 并且它被执行了。

我尝试使用 htop 监控进程执行,但找不到正在启动的进程。

【问题讨论】:

    标签: php perl shell debian exec


    【解决方案1】:

    为了调试,我把 exec 命令改成了$command = $cmd . ' > debug.log 2>&1';

    现在,日志输出显示没有找到启动脚本所需的 perl 模块。我错过了这一点,因为在我的 shell 中,我在测试时以 root 用户身份运行。 www-data 用户无权访问这些模块。

    为了解决这个问题,我必须为 www-data 创建 shell 和 sudo 权限。然后我以 www-data 身份登录,使用 cpan minus 安装所需的模块,然后注销。现在脚本开始正常运行。

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 2014-10-06
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多