如何通过 php 脚本调试 shell 进程执行?
How to debug shell process execution by php script?
我有一个 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 监视进程执行,但找不到正在启动的进程。
为了调试,我将 exec 命令更改为 $command = $cmd . ' > debug.log 2>&1';
现在,日志输出显示找不到启动脚本所需的 perl 模块。我错过了这个,因为在我的 shell 中,我在测试时是 运行 作为 root 用户。 www-data 用户无权访问这些模块。
为了解决这个问题,我必须为 www-data 创建 shell 和 sudo 权限。然后我以 www-data 身份登录,使用 cpan minus 安装所需的模块,然后注销。现在脚本已正确启动 运行。
我有一个 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 监视进程执行,但找不到正在启动的进程。
为了调试,我将 exec 命令更改为 $command = $cmd . ' > debug.log 2>&1';
现在,日志输出显示找不到启动脚本所需的 perl 模块。我错过了这个,因为在我的 shell 中,我在测试时是 运行 作为 root 用户。 www-data 用户无权访问这些模块。
为了解决这个问题,我必须为 www-data 创建 shell 和 sudo 权限。然后我以 www-data 身份登录,使用 cpan minus 安装所需的模块,然后注销。现在脚本已正确启动 运行。