0

我有一个队列列表(在 Zend Framework PHP 中创建并存储在 MySQL 中),需要传送到接收器(寻呼机、Arduino、微控制器)。

但接收器可以关闭(电源故障或未连接电源)或没有网络(wifi 不可用或电缆已拔出)。

稍后一旦其(设备)可用/在线。PHP 应该从服务器发送到设备(不是设备将进行爬取,因为设备只有一个侦听端口作为 TCP 协议的服务器模式)。

所以,我必须从 PHP 创建一个 BASH 脚本并将其作为带有循环的新进程运行。你是怎样做的?这是一个粗略的示例,我的意思是:

新流程作为工作:

$ cat /var/tmp/job1.sh
#!/bin/bash
while :
do
  # by triggering this it will send a TCP command to the device for downloading the new job

  #curl "http://myphpserver/controller/action?valuesfromPHP1=1&valuesfromPHP2=2" &
  curl "http://myphpserver/controller/action?valuesfromPHP$1=1&valuesfromPHP2=$2" &
  sleep 1
done

从 PHP 分配任务我如何准确地发送带有 shell 函数的命令行参数并让 PHP 不等待脚本完成,以便在执行脚本后不到 1 秒内 PHP 是免费的,而不是冻结的?:

$ php -R "shell_exec('/var/tmp/job1.sh value1 value2');"

你如何用 PHP 做到这一点?

编辑:

第一步:开始工作

  $l  = strtolower($this->data->language);
  $d1 = strtolower($rec->department1);
  $d2 = strtolower($rec->department2);
  $d3 = strtolower($rec->department3);
  $d4 = strtolower($rec->department4);
  shell_exec("/var/tmp/job1.sh {$l} {$d1} {$d2} {$d3} {$d4}");

第 2 步:BASH 到 PHP 触发器

  public function triggerdeviceAction() {
    $d1  = $_GET['department1'];
    $d2  = $_GET['department2'];
    $d3  = $_GET['department3'];
    $d4  = $_GET['department4'];
    $l   = $_GET['language'];

    $sql = "select *from sh_av_profile where
              `group` = 'agent' and
              status='free' and
              operator <> '' and 
              (
                department1=LOWER('{$rec->department1}') OR
                department2=LOWER('{$rec->department2}') OR
                department3=LOWER('{$rec->department3}') OR
                department4=LOWER('{$rec->department4}')                    
              )";
    $tmpres = $this->db->fetchAll($sql);      

    if (count($tmpres) > 0) {      
      foreach ($tmpres as $workstations) {
        $workstations['department1'] = strtolower($workstations['department1']);
        $workstations['department2'] = strtolower($workstations['department2']);
        $workstations['department3'] = strtolower($workstations['department3']);
        $workstations['department4'] = strtolower($workstations['department4']);

        $sql = "select *from sh_av_users where
                  username = LOWER('{$workstations['operator']}') and
                  status='online' and
                  (
                    language1=LOWER('{$l}') OR
                    language2=LOWER('{$l}') OR
                    language3=LOWER('{$l}')
                  ) and
                  department in (LOWER('{$workstations['department1']}'), 
                                 LOWER('{$workstations['department2']}'), 
                                 LOWER('{$workstations['department3']}'),
                                 LOWER('{$workstations['department4']}') 
                                )
                limit 1";
        $operatorFind = $this->db->fetchAll($sql);            
        if(count($operatorFind) > 0) {      
          try {
            $reject = new Application_Model_Device($workstations['ip'], 58888);
            $reject->sendKioskNoWait("calling");
          } catch(Exception $e) {

          }
          break;
        }
      }

    }

    echo "OK";
    exit;
  }
4

1 回答 1

0

第1步:

cat > /var/tmp/job1.sh <<\EOF
    #!/bin/bash
    i=0  
    while :
    do
      (( i++ ))
      a=$(curl -s --connect-timeout 1 'http://localhost/a/triggerdevice?a=$1&b=$2&c=$3&d=$4&e=$5&f=$6&g=$7' | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["result"]')

      if [ a -eq "success"]; then
        echo "[OK]: $i"
        exit
      else
        echo "[FAIL]: $i"
      fi

      sleep 1

    done

EOF
chmod +x /var/tmp/job1.sh

第 2 步:使用 shell_exec 从 php 调用它

shell_exec('/var/tmp/job1.sh tech dev root test NL 123 USER1');
于 2013-11-09T04:31:56.163 回答