PHP - 如何创建BASH脚本并让它作为新进程运行?

时间:2013-11-09 02:49:45

标签: php bash zend-framework

我有一个队列列表(在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做的?

修改

第1步:开始工作

  $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;
  }

1 个答案:

答案 0 :(得分: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');