dsqdpn31467 2013-05-20 07:07
浏览 117
已采纳

php,如何在后台为消费者创建/保持服务?

im running an ec2 instance, with ubuntu 12.04.

i am using aws infastructure, and right now im trying to implement a consumer worker which will consume an sqs queue (queue of msgs, not very important).

to do that i created a php file, which "harvest" the queue for 30 seconds. on top i have a crontab running this page every 30 seconds.

what is a more elegant/proper solution? how do i make a background php proccess, and how i do check if its alive, and kill it restart it, if needed?

  • just a clarification - i am asking about the process which consumes the queue, regardless of which/what mechanizem is selected (sqs, rabbitMQ)

thanks for your help

  • 写回答

2条回答 默认 最新

  • dsfdsfds521521 2013-05-23 17:22
    关注

    create a monitor script utilizing the class process (which i copied from php docs), which launches your worker script if the process is not running. the monitor script can keep the pid in a file.

    then simply add the monitor to crontab.

    class Process{
        private $pid;
        private $command;
    
        public function __construct($cl=false){
            if ($cl != false){
                $this->command = $cl;
                $this->runCom();
            }
        }
        private function runCom(){
            $command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!';
            exec($command ,$op);
            $this->pid = (int)$op[0];
        }
    
        public function setPid($pid){
            $this->pid = $pid;
        }
    
        public function getPid(){
            return $this->pid;
        }
    
        public function status(){
            $command = 'ps -p '.$this->pid;
            exec($command,$op);
            if (!isset($op[1]))return false;
            else return true;
        }
    
        public function start(){
            if ($this->command != '')$this->runCom();
            else return true;
        }
    
        public function stop(){
            $command = 'kill '.$this->pid;
            exec($command);
            if ($this->status() == false)return true;
            else return false;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器