我有對象服務器這樣的數組:PHP運行多個腳本同時
Array
(
[0](
(
[id] => 1
[version] => 1
[server_addr] => 192.168.5.210
[server_name] => server1
)
)
[1](
(
[id] => 2
[server_addr] => 192.168.5.211
[server_name] => server2
)
)
)
通過運行下面的代碼,我能夠獲得所需的輸出
foreach ($model as $server) {
$cpu_usage = shell_exec('sudo path/to/total_cpu_usage.sh '.$server->server_addr);
$memory_usage = shell_exec('sudo path/to/total_memory_usage.sh '.$server->server_addr);
$disk_space = shell_exec('sudo path/to/disk_space.sh '.$server->server_addr);
$inode_space = shell_exec('sudo path/to/inode_space.sh '.$server->server_addr);
$network = shell_exec('sudo path/to/network.sh '.$server->server_addr);
exec('sudo path/to/process.sh '.$server->server_addr, $processString);
$processArray = array();
foreach ($processString as $i) {
$row = explode(" ", preg_replace('/\s+/', ' ', $i));
array_push($processArray,$row);
}
$datetime = shell_exec('sudo path/to/datetime.sh '.$server->server_addr);
echo $cpu_usage;
echo $mem_usage;
echo $disk_space;
......
}
我的腳本類似於:
#!/bin/bash
if [ "$1" == "" ]
then
echo "To start monitor, please provide the server ip:"
read IP
else
IP=$1
fi
ssh [email protected]$IP "date"
但整個過程花費的時間爲5秒,而服務器的花費時間爲10秒比2秒。這是爲什麼?無論如何減少時間?我的猜測是exec命令正在等待輸出被分配給變量,然後再進入下一個循環?我試圖谷歌一點點,但大部分的答案是沒有返回任何輸出...我需要的輸出雖然
start_time = 2016-12-23T17:42:50,end_time = 2016-12-23T17:43:01。大約11秒爲5個循環,第一個循環在42:51結束大約1 ++秒 –