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


PHP - How can i create a BASH script and let it run as new process?

我有一个队列列表(在Zend Framework PHP中创建并存储在MySQL中),需要将其传递给接收器(Pager,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 步:

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');