doufu6130 2015-05-16 06:37
浏览 92
已采纳

如何通过php脚本调试shell进程执行?

I have a php script which is supposed to execute a perl script on the machine and print the process ID. While executing it, I found that the process id was printed out, but on checking the process list, I couldnt find a running process for the perl script.

I logged the command to a file, and found that the command is correct, and executing it from shell correctly executes the script. Both scripts are owned by 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, "
". "Command: ".$cmd);
   print 'Download started with PID '.$pid;
   fwrite($logfile, "
". "Download started with PID ".$pid);
   fwrite($logfile, "
". "Output lines: ".$pid);
    fwrite($logfile, "
". "Return code: ".$return);
   foreach ($output as &$value) {
    fwrite($logfile, "
". $value); 
   }
} 

Log file output:

#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

I also reviewed apache2 access and error logs, but it doesnt show errors:

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"

How can I debug this? I'm assuming that the shell process execution starts but is then killed by the system. How can I monitor what error is being shown?

exec is not a disabled function in php.ini on my server. I have tested a 'ls -l' on the server with php exec and it is executed.

I tried monitoring process execution with htop, but couldnt find the process starting.

  • 写回答

1条回答 默认 最新

  • doutuo3935 2015-05-16 08:27
    关注

    To debug, I changed the exec command to $command = $cmd . ' > debug.log 2>&1';

    Now, the log output showed that a perl module required for starting the script was not found. I missed this, because in my shell I was running as root user while testing. The www-data user did not have access to these modules.

    To fix this, I had to create shell and sudo rights for www-data. I then logged in as www-data, used cpan minus to install the required modules, and then logged out. Now the script started running properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题