Home > Backend Development > PHP Tutorial > PHP script daemon process principle and implementation code

PHP script daemon process principle and implementation code

小云云
Release: 2023-03-19 21:46:02
Original
1755 people have browsed it

This article mainly introduces the principle and implementation method of the PHP script daemon to you. It analyzes the implementation ideas, principles, formats and specific implementation methods of the PHP script daemon in more detail. Friends who need it can refer to it. I hope it can help you. .

Ideas:

1. While loop, if there is currently no data to operate, you can sleep;
2. crontab script every fixed Execute the script during the time period. When executing, first check whether it is already being executed. If it is not executed, skip it.
3. nohup background execution
4. flock -xn lock

Example:

Code to be executed: index. php


<?php
set_time_limit(0);
//死循环
while(1) {
  $message = &#39;1111111&#39; . "\n";
  error_log($message);
  sleep(5);
}
Copy after login


#/tmp/lock/test1.lock 为当前进程要锁定的文件,不同的进程配置不同的锁文件,该文件会自动创建
* * * * * flock -xn /tmp/lock/test1.lock -c &#39;nohup php index.php >> /php/test.log 2>&1 &&#39;
* * * * * flock -xn /tmp/mytest.lock -c &#39;php /home/fdipzone/php/test.php >> /home/fdipzone/php/test.log&#39;
Copy after login

is writing the php script. To prevent daemon process memory overflow, it is recommended to regularly check memory usage.
Put the following code into the business script:


if(memory_get_usage()>100*1024*1024){
  exit(&#39;内存溢出&#39;);//大于100M内存退出程序,防止内存泄漏被系统杀死导致任务终端
}
Copy after login

Note:

nohup task View and close methods:

Close:


//方法一:
ps -e | grep commend
kill -9 pid
//方法二:
fg %n  //n为jobs命令查看的进程号
Copy after login

View:


//查看后台进程
jobs
Copy after login

Principle:

Use linux flock file lock to implement task locking and resolve conflicts

Format:


flock [-sxun][-w #] fd#
flock [-sxon][-w #] file [-c] command
Copy after login

Options

-s, --shared: Get a shared lock
-x, --exclusive: Get a Exclusive lock
-u, --unlock: Remove a lock, which is usually unnecessary. The lock will be automatically discarded after the script is executed
-n, --nonblock: If the lock is not obtained immediately, it will fail directly instead of Wait
-w, --timeout: If the lock is not obtained immediately, wait for the specified time
-o, --close: Close the file descriptor before running the command. Used to control the locking process if the command spawns a child process
-c, --command: Run a separate command in the shell
-h, --help Display help
-V, - -version: Display version

Run a php file. The file lock uses an exclusive lock. If it is locked, it will fail without waiting. The parameters are -xn

* * * * * flock -xn /tmp/mytest.lock -c 'php /home/fdipzone/php/test.php >> /home/fdipzone/ php/test.log'

In this way, when the task is not completed and the next task determines that /tmp/mytest.lock is locked, the current task will be ended and the judgment will be made again in the next cycle.

Related recommendations:

Detailed explanation of common ways to implement daemon processes in PHP

Two common ways to implement daemon processes in PHP

Method analysis on the implementation principle of script daemon process in php

The above is the detailed content of PHP script daemon process principle and implementation code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template