2017-06-21 152 views
-1

我不想使用「shell,exec等」來完成它......只有在php中循環!在PHP中運行後臺進程

我的理解是在這種情況下:

  1. 我在網頁表單,在表單提交,我會請一個PHP腳本將在後臺運行。
  2. 後臺進程已啓動並正在運行....正在運行....正在運行.....正在運行......
  3. 我想找回進程ID,其中進程ID後臺進程已啓動,並且之前的後臺進程正在運行....正在運行....正在運行.....正在運行..........

我需要這樣做,並且如果可能的話,執行一個文件帶環單碼,直到結束一些線


想法到項目結構: click here

+0

您可以使用[Ajax](https://www.w3schools.com/xml/ajax_intro.asp)。 –

+2

後臺進程需要由網頁瀏覽器啓動?它是否需要在Web服務器重新啓動後才能生存?它需要跨平臺(Unix和Windows)嗎? –

+0

嗨,是使用Windows,通過瀏覽器啓動.. 表單提交 - >在服務器上運行後臺php腳本以靜默模式「窗口」 - >獲取進程ID - >後臺腳本stil運行在服務器上的靜默模式。 –

回答

0

很多,很久以前我寫在通過輪詢接收指令的服務器的客戶端系統的後臺不斷地跑了PHP後臺程序,然後進行各種文件I/O操作作爲迴應。對於它的價值,下面是允許這作爲一個守護進程運行的代碼的相關部分,即後臺PHP程序:

// (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?)\n"); 
    else 
    die ($PIDfile . " already exists, but is not readable.\n"); 
} 

// 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.\n"); 
} 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.\n"); 

// 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 

希望這有助於!不用說,以上是從我的代碼太平間複製和粘貼的歲月東西,並提供沒有任何保證或擔保。請享用!

+1

請注意[PCNTL擴展名](http://php.net/manual/en/intro.pcntl.php)在Windows上不可用。 –

+0

好點,謝謝!雖然當我試圖fork一個進程來創建一個守護進程時,我會自動思考* ix而不是Windows,但那只是我...... :) –

0

後臺程序執行:code font

<?php 
set_time_limit(7200); 
ignore_user_abort(true); 

fopen('./.exec','w'); 

$conn = mysql_connect($servidor,$usuario,$senha); 
mysql_select_db($bdados); 

while (file_exists('./.exec')) { 
    clearstatcache(); 
    if (file_exists('./.in') && filesize('./.in')) { 
     $out = fopen('./.out','w'); 
     $query = mysql_query(file_get_contents('./.in')); 
     $campos = mysql_num_fields($query); 
     $sep = ''; 
     for ($i = 0; $i < $campos; $i++) { 
      fwrite($out,$sep.mysql_field_name($query,$i)); 
      $sep = ';'; 
     } 
     fwrite($out,"\n"); 
     while ($res = mysql_fetch_row($query)) { 
      for ($i = 1; $i < $campos; $i++) { 
       fwrite($out,$sep.$res[$i]); 
       $sep = ';'; 
      } 
      fwrite($out,"\n"); 
     } 
     fclose($out); 
     fclose(fopen('./.in','w')); 
    } 
    sleep(1); 
} 
?> 

也許類似的東西,你說什麼?

我想讓它工作,沒有mysqli & mysql。