确定php脚本执行速度慢的原因

时间:2016-04-26 14:12:19

标签: php time

我有一个运行bash脚本的php脚本来终止进程并启动进程的新实例。不幸的是,在尝试运行它时,运行需要很长时间。我该如何确定原因?它是Debian 7专用服务器上的apache2服务器。

php脚本:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');  
$start = microtime(TRUE);  
if (isset($_GET['resetnow'])) {
   $myreset = $_GET['resetnow'];
   if ($myreset == "now") {
         $cmd=getcwd()."/resetut.sh  2>&1";
         exec($cmd, $output, $return_var);
         $outputstr=implode(",", $output);
         $finish = microtime(TRUE);  
         $totaltime = $finish - $start;      
         $timetaken = " This script took ".$totaltime." seconds to run"; 
         print $cmd." ".$outputstr.$timetaken;
   } else {
         print "Didnt receive correct parameters";
   }
} else {
  print "Undefined";
}
?>

shell脚本:

#!/bin/bash
cd pathto/utorrent
killall utserver
./utstart start
a=$(pgrep utserver)
echo Utorrent restarted as new process $a

不幸的是输出看起来像这样:

/pathto/resetut.sh 2>&1 Starting utorrent server,
Utorrent restarted as new process 25044 25381 
This script took 275.10148310661 seconds to run

这太长了。如果从命令行执行shell脚本,则完成时间不到一秒。

1 个答案:

答案 0 :(得分:0)

您可以在Linux上使用一些工具。您可以使用“ps”或“top”命令查看所有正在运行的进程。这些将显示所有正在运行的进程的列表。我的猜测是其他程序正在利用CPU,你可能想杀死它们。

如果不是这样,您可以使用“time”命令进行一些时间测试。由于PHP和Bash都是shell脚本,因此与C ++程序相比,它们的运行速度会慢一些,因为每次运行PHP和Bash时都必须进行解释。