search
HomeBackend DevelopmentPHP TutorialTwo methods of daemon process in php

Two methods of daemon process in php

May 15, 2018 pm 05:45 PM
phpmethodprocess

This article mainly introduces the two daemon methods in php. Interested friends can refer to it. I hope it can help everyone.

The first way is to use nohup and & together.

Adding the ampersand after the command allows the started process to run in the background without occupying the console. The console can also run other commands. Here I use a while infinite loop to demonstrate. The code is as follows

<?php

while(true){
        echo time().PHP_EOL;
        sleep(3);
}


Use & method to start the process

[root@localhost php]# php deadloop.php &
[1] 3454
[root@localhost php]# ps aux | grep 3454
root      
3454  0.0  0.8 284544  8452 pts/0    
T    18:06   0:00 php deadloop.php
root      
3456  0.0  0.0 103316   896 pts/0    
S+   18:08   0:00 grep 3454

[1]+  Stopped                 
php deadloop.php
[root@localhost php]#


You can see that the process does not occupy the console, and the console can also run other commands. At this time We can also use the fg command to restore the process to the normal mode of occupying the console.



##

[root@localhost php]# fg
php deadloop.php
1470996682
1470996685
1470996688
1470996691

The above is a brief introduction to the & command


Let’s look at another command nohup

. Add nohup before the command. The started process will ignore the Linux hang signal (SIGHUP). So under what circumstances will the SIGHUP signal under Linux be triggered? The following content is excerpted from Baidu Encyclopedia:


SIGHUP会在以下3种情况下被发送给相应的进程:
1、终端关闭时,该信号被发送到session首进程以及作为job提交的进程(即用 & 符号提交的进
程)
2、session首进程退出时,该信号被发送到该session中的前台进程组中的每一个进程
3、若父进程退出导致进程组成为孤儿进程组,且该进程组中有进程处于停止状态(收到SIGSTOP或
SIGTSTP信号),该信号会被发送到该进程组中的每一个进程。

Combining 1 and 2 we know that regardless of whether the process is started with & (job mode), the SIGHUP signal will be received when closing the terminal. So how will the process handle the SIGHUP signal? Look at the sentence also taken from Baidu Encyclopedia

系统对SIGHUP信号的默认处理是终止收到该信号的进程。所以若程序中没有捕捉该信号,当收到该
信号时,进程就会退出。

That is to say, closing the terminal process will receive the SIGHUP signal, and the default processing method of this signal is to end the process. process, of course we can also capture and handle the signal ourselves, or ignore it, for example, in the following code

<?php
pcntl_signal(SIGHUP, function(){
   //这地方处理信号的方式我们只是简单的写入一句日志到文件中
   file_put_contents(&#39;logs.txt&#39;, &#39;pid : &#39; . posix_getpid() . &#39; 
   receive SIGHUP 信号&#39; . PHP_EOL);
});
        
while(1) {
        sleep(10);
        pcntl_signal_dispatch();
}

we run this routine on the command line, then directly close the shell terminal window, and then reopen a terminal to view Is this process still running:

[root@localhost php]# ps -ef | grep deadloop.php 
root     16112     1  0 17:20 ?        00:00:00 php deadloop.php
root     16138 16115  0 17:24 pts/4    00:00:00 grep deadloop.php
[root@localhost php]# cat logs.txt 
pid : 16112 receive SIGHUP 信号

You can see that deadloop.php is still running, and its parent process has become the init process (because its original parent process exited and was adopted by the init process), from writing You can also see in the file contents that the SIGHUP signal is received when the terminal process is closed. In fact, we don't have to be so troublesome. We only need to use the nohup command provided by Linux. But when we use nohup to start the process, the process will ignore the SIGHUP signal received and will not exit. First, remove the signal processing code just now. Then run nohup.


[root@localhost php]# nohup php deadloop.php 
nohup: 忽略输入并把输出追加到"nohup.out"


And nohup will redirect the output of the program to the nohup.out file in the current directory by default, if there is no writable permission , then write $homepath/nohup.out


[root@localhost php]# ls
cmd.sh  deadloop.php  getPhoto.php  nohup.out  pics
[root@localhost php]# tail -f nohup.out 
1470999772
1470999775
1470999778
1470999781
1470999784
1470999787
1470999790
1470999793
1470999796
1470999799
1470999802

At this time, close the terminal, the process will not end, but become an orphan process (ppid=1 ) because the parent process that created it exited.



##

[root@localhost ~]# ps -ef | grep 3554
root      3554  3497  0 19:09 pts/0    00:00:00 php deadloop.php
root      3575  3557  0 19:10 pts/1    00:00:00 grep 3554
[root@localhost ~]# ps -ef | grep 3554
root      3554     1  0 19:09 ?     00:00:00 php deadloop.php
root      3577  3557  0 19:10 pts/1    00:00:00 grep 3554
[root@localhost ~]#



Conclusion : So when we combine the nohup and & methods, the started process will not occupy the console nor rely on the console. After the console is closed, the process will be adopted by process No. 1 and become an orphan process. This is the same as the daemon process mechanism. Very similar.

[root@localhost php]# nohup php deadloop.php >logs.txt 2>error.txt &
[1] 3612
[root@localhost php]# ps -ef |grep 3612
root      3612  3557  0 19:18 pts/1    00:00:00 php deadloop.php
root      3617  3557  0 19:19 pts/1    00:00:00 grep 3612
[root@localhost php]#

Where >logs.txt redirects standard output, 2>error.txt redirects standard error output.



The above is an introduction to the first implementation method.

The second implementation method is to implement it through code according to the rules and characteristics of the daemon process. The biggest feature of the daemon process is that it is separated from the user terminal and session. The following is the implemented code, with comments in key places.

<?php
$pid = pcntl_fork();
if ($pid == -1)
{
    throw new Exception(&#39;fork子进程失败&#39;);
}
elseif ($pid > 0)
{
    //父进程退出,子进程不是进程组长,以便接下来顺利创建新会话
    exit(0);
}

// 最重要的一步,创建一个新的会话,脱离原来的控制终端
posix_setsid();

// 修改当前进程的工作目录,由于子进程会继承父进程的工作目录,修改工作目录以释放对父进
程工作目录的占用。
chdir(&#39;/&#39;);

/*
 * 通过上一步,我们创建了一个新的会话组长,进程组长,且脱离了终端,但是会话组长可以
 申请重新打开一个终端,为了避免
 * 这种情况,我们再次创建一个子进程,并退出当前进程,这样运行的进程就不再是会话组长。
 */
$pid = pcntl_fork();
if ($pid == -1)
{
    throw new Exception(&#39;fork子进程失败&#39;);
}
elseif ($pid > 0)
{
    //  再一次退出父进程,子进程成为最终的守护进程
    exit(0);
}

// 由于守护进程用不到标准输入输出,关闭标准输入,输出,错误输出描述符
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);

/*
 * 处理业务代码
 */

while(TRUE)
{
    file_put_contents(&#39;log.txt&#39;, time().PHP_EOL, FILE_APPEND);
    sleep(5);
}


Related recommendations:


PHP process signal processing

PHP inter-process Detailed explanation of communication


How to implement PHP process lock

The above is the detailed content of Two methods of daemon process in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

How often should you regenerate session IDs?How often should you regenerate session IDs?Apr 23, 2025 am 12:03 AM

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!