Hướng dẫn how to run php process in background? - làm thế nào để chạy quá trình php trong nền?

Sử dụng chức năng này để chạy chương trình của bạn trong nền. Nó đa nền tảng và hoàn toàn có thể tùy chỉnh.

 is_string($redirectStdout) ? array('file', $redirectStdout, 'w') : array('pipe', 'w'),
        2 => is_string($redirectStderr) ? array('file', $redirectStderr, 'w') : array('pipe', 'w'),
    );
    if (is_string($stdin)) {
        $descriptorspec[0] = array('pipe', 'r');
    }
    $proc = proc_open($command, $descriptorspec, $pipes, $cwd, $env, $other_options);
    if (!is_resource($proc)) {
        throw new \Exception("Failed to start background process by command: $command");
    }
    if (is_string($stdin)) {
        fwrite($pipes[0], $stdin);
        fclose($pipes[0]);
    }
    if (!is_string($redirectStdout)) {
        fclose($pipes[1]);
    }
    if (!is_string($redirectStderr)) {
        fclose($pipes[2]);
    }
    return $proc;
}

Lưu ý rằng sau khi lệnh bắt đầu, theo mặc định, chức năng này đóng phần stdin và stdout của quá trình chạy. Bạn có thể chuyển hướng đầu ra quá trình vào một số tệp thông qua $ RedirectStDout và $ RedirectStderr đối số.

Lưu ý cho người dùng Windows: Bạn không thể chuyển hướng stdout/stderr sang

startBackgroundProcess('ping yandex.com >nul 2>&1');
3 theo cách sau:
You cannot redirect stdout/stderr to
startBackgroundProcess('ping yandex.com >nul 2>&1');
3 in the following manner:

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');

Tuy nhiên, bạn có thể làm điều này:

startBackgroundProcess('ping yandex.com >nul 2>&1');

Ghi chú cho người dùng *Nix:

1) Sử dụng lệnh Exec Shell nếu bạn muốn nhận PID thực tế:

$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));

2) Sử dụng đối số $ stdin nếu bạn muốn chuyển một số dữ liệu cho đầu vào của chương trình của bạn:

startBackgroundProcess('cat > input.txt', "Hello world!\n");

php-script-background-processer

Ở đây chúng ta có thể chạy một tệp PHP (tập lệnh) trong nền, quá trình này được ẩn với người dùng cuối. Nó cải thiện hiệu quả trang web của bạn.

Mục lục:

  • Thực hiện tập lệnh PHP trong xử lý nền
    • Làm thế nào để kiểm tra quy trình nền trong Linux?
  • Lệnh thực thi tệp PHP trong nền là gì?
  • Nohup là gì?
  • Exec là gì?
  • Làm thế nào để sử dụng thư viện PHP này trên mã của bạn?
  • Làm thế nào để có được tất cả các quy trình đang chạy?
  • Làm thế nào để tiêu diệt một quá trình?
  • Làm thế nào để tạo stdout và stderr trong một tệp? (Chỉ được đề xuất để gỡ lỗi)
  • Làm thế nào để sử dụng bên trong codeigniter4 này?
  • Cách sử dụng trong Codeigniter 3
    • Các vấn đề tôi đã gặp phải trong khi sử dụng nó với Codeigniter3.

-> Libcurl.so.4 Curl_openssl_4 Không tìm thấy.

startBackgroundProcess('cat > input.txt', "Hello world!\n");
2 Có thể có hai tệp libcurl.so có sẵn để PHP bị nhầm lẫn. Để tìm thấy điều đó trong Linux
startBackgroundProcess('cat > input.txt', "Hello world!\n");
3. Đầu ra của tôi
startBackgroundProcess('cat > input.txt', "Hello world!\n");
4 Bạn sẽ tìm thấy hai đường dẫn bao gồm
startBackgroundProcess('cat > input.txt', "Hello world!\n");
5 và
startBackgroundProcess('cat > input.txt', "Hello world!\n");
6

Để sửa lỗi này theo lệnh dưới 2 nếu cần sử dụng

startBackgroundProcess('cat > input.txt', "Hello world!\n");
7

Hãy thử lại để chạy ứng dụng và kiểm tra xem bạn có gặp bất kỳ lỗi nào không. Tôi cũng nhận được lỗi dưới đây. Giải quyết cùng một cách trên.

Làm cách nào để chạy một quá trình trong nền?

ls -l &
exec php index.php > /dev/null 2>&1 & echo \$!

Làm thế nào để kiểm tra quy trình nền trong Linux?


ps -l (list all process)
ps -ef (all full details of the process)

Lệnh thực thi tệp PHP trong nền là gì?

Nohup là gì?

Example:

nohup exec php process.php hello world > /dev/null &

Nohup là gì?

Exec là gì?

Làm thế nào để sử dụng thư viện PHP này trên mã của bạn?

Làm thế nào để có được tất cả các quy trình đang chạy?

Exec là gì?

Làm thế nào để sử dụng thư viện PHP này trên mã của bạn?

Làm thế nào để sử dụng thư viện PHP này trên mã của bạn?

Làm thế nào để có được tất cả các quy trình đang chạy? create two file name index.php and process.php

Bước 2: Bao gồm tệp tinprocessor.php trong index.php include the BackgroundProcessor.php file in the index.php

Bước 3: Tạo một thể hiện của lớp nền lớp create an instance of the class BackgroundProcess

Chúng ta có thể sử dụng điều này:

Loại 1:

$proc=new BackgroundProcess('exec php /process.php hello world');

Loại 2:

$proc=new BackgroundProcess();
$proc->setCmd('exec php /process.php hello world');
$proc->start();

Loại 3:

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
0

Một cách khác để thực hiện URL PHP trong nền mà không cần tệp trực tiếp (.php).

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
1

Làm thế nào để có được tất cả các quy trình đang chạy?

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
2

Làm thế nào để tiêu diệt một quá trình?

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
3

Cách tạo stdout và stderr trong một tệp. (Chỉ được đề xuất để gỡ lỗi)

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
4

Sau khi chạy sẽ tạo

startBackgroundProcess('ping yandex.com >nul 2>&1');
4 cho
startBackgroundProcess('ping yandex.com >nul 2>&1');
5 và
startBackgroundProcess('ping yandex.com >nul 2>&1');
6 cho
startBackgroundProcess('ping yandex.com >nul 2>&1');
7

Vui lòng không sử dụng trong sản xuất, điều này sẽ lấp đầy lưu trữ của bạn nếu bạn không làm sạch tất cả các bản ghi.

Cách sử dụng bên trong Codeigniter4 này

Vui lòng làm theo cùng một bước bên trong thư mục

startBackgroundProcess('ping yandex.com >nul 2>&1');
8

Bước 1: Sao chép

startBackgroundProcess('ping yandex.com >nul 2>&1');
9 vào
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
0 Bước 2: Thêm không gian tên, ví dụ
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
1 Dòng đầu tiên sau
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
2 Bước 3: Tạo bộ điều khiển cho quy trình nền
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
3. Đảm bảo tên lớp và tên tệp nên giống nhau. \
Copy the
startBackgroundProcess('ping yandex.com >nul 2>&1');
9 to
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
0
Step 2: Add a namespace e.g
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
1 first line after
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
2
Step 3: Create a controller for background process
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
3. Make sure the class name and file name should same. \

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
5

Bước 4: Tạo bộ điều khiển để thực thi mã trên trong nền

$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
4: Create a controller to execute above code in background
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
4

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
6

Bước 5: Bắt đầu máy chủ của ứng dụng của bạn và thử

$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
5 Bạn sẽ thấy một số là ID xử lý. Nó có nghĩa là quá trình nền bắt đầu. Start the server of your application and try
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
5 you will see a number which is process id. it means background process started.

Cách sử dụng trong Codeigniter 3

Bước 1: Sao chép

startBackgroundProcess('ping yandex.com >nul 2>&1');
9 vào
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
7 Bước 2: Tạo bộ điều khiển cho quy trình nền
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
8.
Copy the
startBackgroundProcess('ping yandex.com >nul 2>&1');
9 to
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
7
Step 2: Create a controller for background process
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
8.

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
7

Bước 3: Thêm tên chức năng thành viên

$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
9 vào
startBackgroundProcess('cat > input.txt', "Hello world!\n");
0. Đặt mã bên dưới ở đó.
Add a member function name
$proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null');
print_r(proc_get_status($proc));
9 to
startBackgroundProcess('cat > input.txt', "Hello world!\n");
0. Place below code there.

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
8

Bước 4: Thử chạy

startBackgroundProcess('cat > input.txt', "Hello world!\n");
1: try running
startBackgroundProcess('cat > input.txt', "Hello world!\n");
1

startBackgroundProcess('ping yandex.com', null, 'nul', 'nul');
9

Các vấn đề tôi đã gặp phải trong khi sử dụng nó với Codeigniter3.

-> Libcurl.so.4 Curl_openssl_4 Không tìm thấy.

startBackgroundProcess('ping yandex.com >nul 2>&1');
0

startBackgroundProcess('cat > input.txt', "Hello world!\n");
2 Có thể có hai tệp libcurl.so có sẵn để PHP bị nhầm lẫn. Để tìm thấy điều đó trong Linux
startBackgroundProcess('cat > input.txt', "Hello world!\n");
3. Đầu ra của tôi
startBackgroundProcess('cat > input.txt', "Hello world!\n");
4 Bạn sẽ tìm thấy hai đường dẫn bao gồm
startBackgroundProcess('cat > input.txt', "Hello world!\n");
5 và
startBackgroundProcess('cat > input.txt', "Hello world!\n");
6
There might be two libcurl.so file available so php get confused. To find that in linux
startBackgroundProcess('cat > input.txt', "Hello world!\n");
3. My output
startBackgroundProcess('cat > input.txt', "Hello world!\n");
4 You will find two path which consist of
startBackgroundProcess('cat > input.txt', "Hello world!\n");
5 and
startBackgroundProcess('cat > input.txt', "Hello world!\n");
6

Để sửa lỗi này theo lệnh dưới 2 nếu cần sử dụng

startBackgroundProcess('cat > input.txt', "Hello world!\n");
7

startBackgroundProcess('ping yandex.com >nul 2>&1');
1

Hãy thử lại để chạy ứng dụng và kiểm tra xem bạn có gặp bất kỳ lỗi nào không. Tôi cũng nhận được lỗi dưới đây. Giải quyết cùng một cách trên.

startBackgroundProcess('ping yandex.com >nul 2>&1');
2

Làm cách nào để chạy một quá trình trong nền?

Sử dụng BG để gửi các lệnh chạy đến nền, bạn có thể dễ dàng gửi các lệnh đó đến nền bằng cách nhấn các phím Ctrl + Z và sau đó sử dụng lệnh BG.Nhấn Ctrl + Z dừng quá trình chạy và BG đưa nó lên nền. You can easily send such commands to the background by hitting the Ctrl + Z keys and then using the bg command. Hitting Ctrl + Z stops the running process, and bg takes it to the background.

Quá trình chạy tập lệnh PHP là gì?

Để chạy cục bộ một tập lệnh PHP:..
Nhấp vào mũi tên bên cạnh nút chạy.Trên thanh công cụ và chọn Chạy Cấu hình -or- đi chạy |Chạy cấu hình.Một hộp thoại chạy sẽ mở ..
Bấm đúp vào tùy chọn tập lệnh PHP để tạo cấu hình chạy mới ..