2015-05-16 06:37



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'])) {
   $cmd="/usr/bin/perl ".getcwd().'/ "'.$spath.'"';
   if (isset($_GET['recursion'])) {
      $cmd=getcwd().'/ '.$spath;
   } else {
   $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 test
Command: /usr/bin/perl /var/www/ ""
Download started with PID 13147
Output lines: 13147
Return code: 0

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"
ip2 - - [16/May/2015:11:54:41 +0530] "GET /dlnow.php? HTTP/1.1" 200 321 "" "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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doutuo3935 doutuo3935 6年前

    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.

    点赞 评论 复制链接分享