dongmijgnnq0118 2017-06-21 09:14
浏览 84

在PHP中运行后台进程

I did not want to do it using "shell, exec, etc"... Only with loop in php!

My understanding is in this case:

  1. I have a form in page, on form submit, i will call on a PHP script which will run in Background.
  2. Background process started and running ....running ....running .....running......
  3. I want to got back process id with which Process id Background process started and previous background process running ....running ....running .....running ..........

I need to do this, and if possible a file where it executes a single code with loop until finish some line


Idea to project structure: click here

  • 写回答

2条回答 默认 最新

  • dsgrgaz321973284 2017-06-21 09:45
    关注

    Many, many moons ago I wrote a PHP daemon which ran endlessly in the background on a client system that received instructions by polling a server and then performed various file I/O actions in response. For what it's worth, here's the relevant portion of the code that allowed this to run as a daemon, i.e. a background PHP process:

    // (Note:
    // The writeLog() function has been set up previously and is a 
    // trivial function that adds timestamped lines into a logfile.)
    
    
    $PIDfile='/tmp/myPID';           // Store process ID in this file
    
    // If a deamon is already running, display its PID and abort:
    if (file_exists ($PIDfile)) {
      if (is_readable ($PIDfile))
        die ($PIDfile . " already exists, PID=" . 
             file_get_contents ($PIDfile) . " (stale PID file?)
    ");
      else
      die ($PIDfile . " already exists, but is not readable.
    ");
    }
    
    // Set execution directives if we're running a PHP version prior to 5.3.0
    // (ticks are deprecated as of PHP5.3.0 and will be removed from PHP6.0.0):
    if (version_compare (PHP_VERSION, '5.3.0', '<'))
      declare (ticks = 1);
    
    // Fork off the background (daemon) process:
    $pid = pcntl_fork ();
    if ($pid == -1) {
      die ("Fatal: unable to fork.
    ");
    } elseif ($pid)
      exit ();                                      // We are the parent process
    
    // If we arrive at this point, we are the child process.
    // Detach from the controlling terminal:
    if (posix_setsid () == -1)
      die ("Fatal: unable to detach from terminal.
    ");
    
    // Register child PID:
    $posid = posix_getpid ();
    $fp = fopen ($PIDfile, "w");
    fwrite ($fp, $posid);
    fclose ($fp);
    
    // Set up signal handlers:
    pcntl_signal (SIGTERM, "SIGhandler");
    pcntl_signal (SIGHUP, "SIGhandler");
    
    // Log successful deamonization:
    writeLog ("Daemon spawned, PID = " . $posid);
    
    // Daemon payload code starts here:
    
    while (1) {
    
      // Do all kinds of interesting stuff that does not require any user
      // input or screen output. In my case this consisted of file I/O and
      // communication with a server using various sockets.
    
    }
    
    // EOF
    

    Hope this helps! Needless to say the above is a copy-and-paste of years old stuff from my code morgue and is supplied without any warranty or guarantee whatsoever. Enjoy!

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置