PHP extension program daemon, php extension daemon _ PHP Tutorial

Source: Internet
Author: User
The PHP Extension program implements the daemon process and the php extension daemon process. The PHP Extension program implements the daemon process. The php extension daemon generally runs on the system background, which is very different from the common interactive command line program. There is a PHP extension daemon in glibc, and the php extension daemon

Generally, Server programs run in the system background, which is very different from common interactive command line programs. There is a function daemon in glibc. By calling this function, the current process can be detached from the terminal into a daemon. For more information, see man daemon. This function is not available in PHP for the time being. if you are interested, you can write a PHP extension function.

The PHP command line program provides two daemon methods:

1. use nohup

The code is as follows:
Nohup php myprog. php> log.txt &

Daemon is implemented here.

Execute php myprog. php separately. when ctrl + c is pressed, the program execution will be interrupted and the current process and sub-processes will be killed.

Php myprog. php &. in this way, although the execution program is also converted to backend running, it is actually dependent on the terminal, and the process will be killed when the user exits the terminal.

II. use PHP code to implement

Function daemonize () {$ pid = pcntl_fork (); if ($ pid =-1) {die ("fork (1) failed! \ N ");} elseif ($ pid> 0) {// let the process started by the user exit (0 );} // create a new session different from the terminal to detach the terminal posix_setsid (); $ pid = pcntl_fork (); if ($ pid =-1) {die ("fork (2) failed! \ N ");} elseif ($ pid> 0) {// the parent process exits, and the child process becomes the final independent process exit (0) ;}} daemonize (); sleep (1000 );

Use the above code to implement daemonize () daemonize. when your PHP program needs to be converted to the background for running, you only need to call the encapsulated function daemonize.
Note: the redirection of standard input and output is not implemented here.

The above is all the content of this article. I hope you will like it.

Generally, the worker Server program runs on the system background, which is very different from the common interactive command line program. There is a... in glibc...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.