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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失