每15秒循环一部分PHP代码

时间:2010-08-16 08:51:08

标签: php perl while-loop

我正在尝试使用php创建一个记录功能,用于在Windows上捕获计算机信息的工具。每15秒生成一个txt文件,里面有信息。我想让它在后台运行,而用户访问其他页面而不会被打断。

这是我现在正在使用的代码

<?php
        $tracker = $_GET['counter'];
        if (isset($tracker) && $tracker == 1)
        {
            $_SESSION['tracker'] = 1;

            $query = "SELECT hostname FROM computers ORDER BY hostname ASC";
            $computer_search = mysql_query($query);
            confirm_query($computer_search);

            while ($computers = mysql_fetch_array($computer_search)){
                $COMP = $computers['hostname'];
        $time = time();
        shell_exec('perl logger.pl ' . $COMP . ' >> logs/' . $COMP . '-' . $time . '.txt');
            }

            redirect_to('index.php?tracker=1');
        }
        elseif (isset($tracker) && $tracker == 0)
        {
            $_SESSION['tracker'] = 0;

            redirect_to('index.php?tracker=0');
        }
    ?>

起初我尝试使用

    $query = "SELECT hostname FROM computers ORDER BY hostname ASC";
            $computer_search = mysql_query($query);
            confirm_query($computer_search);

            while ($computers = mysql_fetch_array($computer_search)){
                $COMP = $computers['hostname'];
                $time = time();
                // 1 is the counter entered into the perl script
                shell_exec('nohup perl logger.pl ' . $COMP . ' 1 > /dev/null 2> /dev/null >> logs/' . $COMP . '-' . $time . '.txt');
            }

但是代码只捕获一台计算机的信息,并在捕获下一台计算机的信息之前卡在循环中。另一方面,第一个代码仅捕获所有计算机的数据,但只捕获一次。

至于logger.pl,这就是我所做的。

    !/usr/bin/perl -w
    use BER;
    use SNMP_util;
    use SNMP_Session;

    $MIB1 = ".1.3.6.1.2.1.25.3.3.1.2"; #Cpu Processors
    $MIB2 = ".1.3.6.1.2.1.1.3"; #System Uptime
    $MIBIPAdd = ".1.3.6.1.2.1.4.20.1.1";

    $HOST = shift;
    # taken out for 1st code
    #$tracker = shift;

    #while ($tracker == 1)
    {
    print "Computer Name: $HOST\n";
    $count = 0;
    # ($MIB1) && ($HOST) || die "Usage: $0 MIB_OID HOSTNAME";
    (@values) = &snmpwalk("$HOST","$MIB1");
    foreach $value (@values)
        {
        $count++;
    if ($value) {
        $goodvalue = &strip_comment($value);
        print "CPU Usage of Processor $count: $goodvalue%\n"; }
        if ($value > 90){
            print "Warning: CPU Usage over 90%! \n"
        }
    else { warn "No response from host :$HOST:\n"; }
}

(@valuesIP) = &snmpwalk("$HOST","$MIBIPAdd");
$ipaddress = &strip_comment($valuesIP[1]);
print "IP Address: $ipaddress \n";

($value) = &snmpwalk("public\@$HOST","$MIB2");
if ($value) {
    $goodvalue = &strip_comment($value);
    print "System Up Time: $goodvalue\n"; }
else { print "No response from host: $HOST\n"; }

print "\n";
sleep(15);
    }

    sub strip_comment
    {
$theline = shift;

$where = index($theline, ":");

if($where eq -1){
    return $theline;
}

$selectedpart = substr($theline, $where);
$goodpart = substr($selectedpart, 1);


return $goodpart;
}

3 个答案:

答案 0 :(得分:2)

你应该使用一个cron工作, 使用您希望每15秒运行一段代码创建一个脚本,然后添加一个cron作业,该作业将安排脚本每15秒运行一次

答案 1 :(得分:0)

答案 2 :(得分:0)

在循环中使用sleep(15);。在计时器到期之前,它将使用很少的资源。