Hướng dẫn can a php script run forever? - một tập lệnh php có thể chạy mãi mãi không?

Đừng quên đặt thời gian thực hiện tối đa thành vô hạn (0).

Tốt hơn hãy chắc chắn rằng bạn không chạy nhiều hơn một trường hợp, nếu đó là ý định của bạn:

ignore_user_abort(true);//if caller closes the connection (if initiating with cURL from another PHP, this allows you to end the calling PHP script without ending this one)
set_time_limit(0);

$hLock=fopen(__FILE__.".lock", "w+");
if(!flock($hLock, LOCK_EX | LOCK_NB))
    die("Already running. Exiting...");

while(true)
{

    //avoid CPU exhaustion, adjust as necessary
    usleep(2000);//0.002 seconds
}

flock($hLock, LOCK_UN);
fclose($hLock);
unlink(__FILE__.".lock");

Nếu ở chế độ CLI, chỉ cần chạy tệp.

Nếu trong một PHP khác trên máy chủ web, bạn có thể bắt đầu một loại phải chạy vô cùng như thế này (thay vì sử dụng Curl, điều này sẽ loại bỏ sự phụ thuộc):

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);

Hoặc bạn có thể bắt đầu từ Linux Cron bằng cách sử dụng WGE như thế này:

`* * * * * wget -O - http://localhost/infinite_loop.php`

Hoặc bạn có thể bắt đầu từ Trình lập lịch Windows bằng cách sử dụng Bitsadmin chạy tệp .bat có chứa điều này:

bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop

16 năm trước

Ratty tại Brohoof dot com

10 năm trước

rsallo tại gna dot nospam dot es ¶

PHP tại DOT com tiêu tốn chứng khoán ¶

Alexander Dot Krause tại ed-solutions dot de ¶

(Php 4, Php 5, Php 7, Php 8)

set_time_limit - giới hạn thời gian thực hiện tối đaLimits the maximum execution time

Sự mô tả

set_time_limit (int $seconds): bool(int $seconds): bool

Khi được gọi, set_time_limit () khởi động lại bộ đếm thời gian chờ từ 0. Nói cách khác, nếu thời gian chờ là 30 giây mặc định và 25 giây vào thực hiện tập lệnh, một cuộc gọi như set_time_limit(20) được thực hiện, tập lệnh sẽ chạy trong tổng số 45 giây trước khi hết thời gian.set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

Thông số

seconds

Thời gian thực hiện tối đa, tính bằng giây. Nếu được đặt thành 0, không có giới hạn thời gian được áp đặt.

Trả về giá trị

Trả về true khi thành công, hoặc false về thất bại.true on success, or false on failure.

Ghi chú

Ghi chú::

Hàm set_time_limit () và chỉ thị cấu hình MAX_EXECATED_TIME chỉ ảnh hưởng đến thời gian thực hiện của chính tập lệnh. Bất kỳ thời gian nào dành cho hoạt động xảy ra bên ngoài việc thực thi tập lệnh như các cuộc gọi hệ thống bằng hệ thống (), hoạt động luồng, truy vấn cơ sở dữ liệu, v.v. không được bao gồm khi xác định thời gian tối đa mà tập lệnh đã chạy. Điều này không đúng trên các cửa sổ trong đó thời gian đo là có thật.set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

kexianbin tại DIYism dot com ¶

8 năm trước

Both set_time_limit(...) and  ini_set('max_execution_time',...); won't count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec(), to run static method/function in background/detached process and time is out kill it:

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
0

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
1

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
2

mba_aslam tại yahoo dot com ¶

15 năm trước

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
3

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
4

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
5

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
6

Jonathon Dot Keogh tại Gmail Dot Com ¶

14 năm trước

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
7

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
8

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
9

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

Eric Pecoraro tại Shepard Com ¶

17 năm trước

`* * * * * wget -O - http://localhost/infinite_loop.php`
1

`* * * * * wget -O - http://localhost/infinite_loop.php`
2

`* * * * * wget -O - http://localhost/infinite_loop.php`
3

`* * * * * wget -O - http://localhost/infinite_loop.php`
4

`* * * * * wget -O - http://localhost/infinite_loop.php`
5

`* * * * * wget -O - http://localhost/infinite_loop.php`
6

Ẩn danh ¶

2 năm trước

`* * * * * wget -O - http://localhost/infinite_loop.php`
7

`* * * * * wget -O - http://localhost/infinite_loop.php`
8

`* * * * * wget -O - http://localhost/infinite_loop.php`
9

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

Robertbrogers tại Gmail Dot Com ¶

8 năm trước

bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop
1

bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop
2

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

mba_aslam tại yahoo dot com ¶

15 năm trước

bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop
4

Jonathon Dot Keogh tại Gmail Dot Com ¶

14 năm trước

bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop
5

bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop
6

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

Eric Pecoraro tại Shepard Com ¶

17 năm trước

bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop
8

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1, //at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.php", false, $cx);
4

$seconds0

$seconds1

Ẩn danh ¶

2 năm trước

$seconds2

$seconds3

$seconds4

$seconds5

Robertbrogers tại Gmail Dot Com ¶

PHP tại Mightycpa.com

$seconds6

$seconds7

$seconds8

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

19 năm trước

15 năm trước

set_time_limit(20)0

set_time_limit(20)1

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

Jonathon Dot Keogh tại Gmail Dot Com ¶

15 năm trước

set_time_limit(20)3

set_time_limit(20)4

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

Jonathon Dot Keogh tại Gmail Dot Com ¶

14 năm trước

set_time_limit(20)6

set_time_limit(20)7

set_time_limit(20)8

set_time_limit(20)9

Eric Pecoraro tại Shepard Com ¶

2 năm trước

seconds0

seconds1

seconds2

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

Robertbrogers tại Gmail Dot Com ¶

PHP tại Mightycpa.com

seconds4

seconds5

seconds6

seconds7

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

19 năm trước

f.nakamura ¶

seconds9

true0

true1

true2

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

7 năm trước

15 năm trước

true4

true5

true6

true7

true8

true9

false0

false1

false2

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

Jonathon Dot Keogh tại Gmail Dot Com ¶

f.nakamura ¶

false4

7 năm trước

2 năm trước

false5

false6

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

Robertbrogers tại Gmail Dot Com ¶

PHP tại Mightycpa.com

false8

false9

`* * * * * wget -O - http://localhost/infinite_loop.php`
0

19 năm trước

PHP tại Mightycpa.com

Both set_time_limit(...) and  ini_set('max_execution_time',...); won't count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec(), to run static method/function in background/detached process and time is out kill it:1

Both set_time_limit(...) and  ini_set('max_execution_time',...); won't count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec(), to run static method/function in background/detached process and time is out kill it:2

Both set_time_limit(...) and  ini_set('max_execution_time',...); won't count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec(), to run static method/function in background/detached process and time is out kill it:3

Both set_time_limit(...) and  ini_set('max_execution_time',...); won't count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec(), to run static method/function in background/detached process and time is out kill it:4

Làm cách nào để làm cho các tập lệnh PHP chạy mãi mãi?

Bạn có thể làm cho nó chạy mãi mãi bằng cách đặt giá trị hoặc gọi set_time_limit trong tập lệnh của bạn (http://php.net/manual/en/function.set-time-limit.php).setting the value or call set_time_limit in your script (http://php.net/manual/en/function.set-time-limit.php).

Các tập lệnh PHP được thực thi như thế nào?

Pha đầu tiên phân tích mã nguồn PHP và tạo biểu diễn nhị phân của mã PHP được gọi là mã hóa Zend.Opcodes là bộ hướng dẫn tương tự như các byte Java.Các opcode này được lưu trữ trong bộ nhớ.Giai đoạn thứ hai của xử lý động cơ Zend bao gồm thực hiện các mã hóa được tạo.. Opcodes are sets of instructions similar to Java bytecodes. These opcodes are stored in memory. The second phase of Zend engine processing consists in executing the generated opcodes.

Bạn có thể viết tập lệnh trong PHP không?

Kịch bản dòng lệnh.Bạn có thể tạo một tập lệnh PHP để chạy nó mà không cần bất kỳ máy chủ hoặc trình duyệt nào.Bạn chỉ cần trình phân tích cú pháp PHP để sử dụng nó theo cách này.Loại sử dụng này là lý tưởng cho các tập lệnh thường xuyên được thực hiện bằng cron (trên *Nix hoặc Linux) hoặc Trình lập lịch tác vụ (trên Windows).You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows).