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.

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

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计