Linux php system origin principle function time

Source: Internet
Author: User
Tags php server

I would like to recommend a very good Linux php system, for example, to give you a better understanding of the Linux php system, and then give a comprehensive introduction to the Linux php system, we hope to use the Daemon in Linux/Unix. For example, httpd and mysqld are common programs running in the resident memory, which are similar to services in Windows. Generally, the daemon is written in C/C ++, that is, a subprocess is generated through fork.

The parent process under the current shell is killed, and the child process is transferred to the background for running. In order not to generate output information on the terminal, log files are written through functions such as syslog. We know that php is a scripting language and is executed through the php script engine. Therefore, it is troublesome to make a daemon process.

Today, we will use Unix/Linux commands to implement the functions of our daemon process. The nohup command in Unix is used to run the command without hanging up. At the same time, nohup puts all the output of the program to the nohup in the current directory. out file. If the file cannot be written, put it in <user's main directory>/nohup. out file.

With this command, our Linux php program will write the shell script. Using loops to keep our scripts running, we can keep running our Linux php scripts regardless of whether the terminal window is closed or not. Of course, when our Linux php process is killed or our operating system is restarted, it will naturally stop.

I will certainly ask, what is the use of our Linux php script as a daemon process? Of course, for example, the most typical function can basically Replace the cron function. For example, some operations that we need to perform on a regular basis can be handed over to it without the need for cron.

Of course, there is no way to restart the server. However, the average Unix server is not that easy to restart. In addition, we can also provide a simple server-side function, such as a server capable of Telnet, which can be used as a small backdoor, but this implementation is a little complicated.

Example 1: automatically generate a file. Now we have two examples to prove our above statement. The first one is to automatically generate a file every thirty seconds and keep running forever. First, make sure that the operating system is Unix or Linux, such as FreeBSD, Redhat, Fedora, or SUSE.

Then we must ensure that our Linux php script engine is in/usr/local/php/bin/php. The specific path can be written according to your actual path. If there is no script engine, install it on your own. For example, if the current directory is/home/heiyeluren/, we can use vi or another editor to compile a file named php_daemon1.php:
$ Vi php_daemon1.php
Then write the following code:

 
 
  1. #! /usr/local/php/bin/php  
  2. <? 
  3. set_time_limit(0);  
  4. while(1)  
  5. {  
  6.  @fopen("test_".time().".txt","w");  
  7.  sleep(30);  
  8. }  
  9. ?> 

Save and exit vi, and then grant the executable permission to the php_daemon1.php file:
$ Chmod + x/home/heiyeluren/php_daemon1.php

Then let's run the script in the background and execute the following command:
$ Nohup/home/heiyeluren/php_daemon1.php &

Remember to add the & symbol to the end so that you can run it in the background. The following prompt appears after executing the preceding command:

[1] 82480
Appending output to nohup. out

A shell prompt will appear after you return to the car. The above prompt means that all output information for command execution will be put in the nohup. out file, which has been mentioned above. After executing the above command, we will see more files starting with test _ in the current directory every thirty seconds, such as test_1139901144.txt test_1139901154.txt and so on, this proves that our program has been run in the background.

So how do we terminate the program? The best way is to restart the operating system. Oh, of course, this is not desirable. We can use the kill command to kill the process. Before killing the process, we will naturally know the PID of the process, it is the Process ID. You can see it by using the ps command.

 
 
  1. $ ps  
  2.   PID  TT  STAT      TIME COMMAND  
  3. 82374  p3  Ss     0:00.14 -bash (bash)  
  4. 82510  p3  S      0:00.06 /usr/local/php/bin/php /home/heiyeluren/php_daemon1.php  
  5. 82528  p3  R+     0:00.00 ps 

As we have seen above, the process id of our Linux php is 82510, so we can run the kill command again:

 
 
  1. $ kill -9 82510  
  2. [1]+  Killed                  nohup /home/heiyeluren/php_daemon1.php 

When you see this prompt, you will understand that this process has been killed, and then ps will find that there is no more:

 
 
  1. $ ps  
  2.   PID  TT  STAT      TIME COMMAND  
  3. 82374  p3  Ss     0:00.17 -bash (bash)  
  4. 82535  p3  R+     0:00.00 ps  
  5.  

If the process cannot be viewed through the ps command, you can use the ps & apos command to view the process. On the basis of the above process extension, you can make your own cron program, then you do not need cron, of course, this is just a way.

Example 2: the server-side Daemon is related to the network. It simulates the use of Linux php as the server side and then runs in the background to achieve the server-side Daemon effect. In our home directory:/home/heiyeluren, edit the php_daemon2.php file:

 
 
  1. $ Vi php_daemon2.php
  2. Enter the following code from the PHP manual, and I have modified and commented on it ):
  3.  
  4. #! /Usr/local/php/bin/php
  5. <?Php 
  6. /* The setting does not display any errors */
  7. Error_reporting (0 );
  8.  
  9. /* The script times out infinitely */
  10. Set_time_limit (0 );
  11.  
  12. /* Start fixed cleanup */
  13. Ob_implicit_flush ();
  14.  
  15. /* Local IP address and port to be opened */
  16. $Address='1970. 168.0.1';
  17. $Port=10000;
  18.  
  19. /* Generate a Socket */
  20. If ($Sock=Socket_create(AF_INET, SOCK_STREAM, SOL_TCP ))< 0){
  21. Echo "socket_create () failed: reason:". socket_strerror ($ sock). "\ n ";
  22. }
  23.  
  24. /* Bind the IP address and port */
  25. If ($Ret=Socket_bind($ Sock, $ address, $ port ))< 0){
  26. Echo "socket_bind () failed: reason:". socket_strerror ($ ret). "\ n ";
  27. }
  28.  
  29. /* Listen for Socket connection */
  30. If ($Ret=Socket_listen($ Sock, 5 ))< 0){
  31. Echo "socket_listen () failed: reason:". socket_strerror ($ ret). "\ n ";
  32. }
  33.  
  34. /* Always-cycle monitoring accepts user connections */
  35. Do {
  36. If ($Msgsock=Socket_accept($ Sock ))< 0){
  37. Echo "socket_accept () failed: reason:". socket_strerror ($ msgsock). "\ n ";
  38. Break;
  39. }
  40. /* Send a prompt to the connected user */
  41. $Msg="============================================== ===\ R \ n".
  42. "Welcome to the PHP Test Server. \ r \ n ".
  43. "To quit, type 'quit'. \ r \ n ".
  44. "To shut down the server type 'shutdown '. \ r \ n ".
  45. "To get help message type 'help'. \ r \ n ".
  46. "============================================== ===\ R \ n ".
  47. "Php>";
  48. Socket_write ($ msgsock, $ msg, strlen ($ msg ));
  49.  
  50. Do {
  51. If (False===( $Buf=Socket_read($ Msgsock, 2048, PHP_NORMAL_READ ))){
  52. Echo "socket_read () failed: reason:". socket_strerror ($ ret). "\ n ";
  53. Break 2;
  54. }
  55. If (! $Buf=Trim($ Buf )){
  56. Continue;
  57. }
  58. /* Close the client connection when the client enters the quit command */
  59. If ($Buf= 'Quit '){
  60. Break;
  61. }
  62. /* When the client enters the shutdown command, both the server and client are closed */
  63. If ($Buf= 'Shutdown '){
  64. Socket_close ($ msgsock );
  65. Break 2;
  66. }
  67. /* The client outputs help information when entering the help Command */
  68. If ($Buf= 'Help '){
  69. $Msg="PHP Server Help Message \ r \ n".
  70. "To quit, type 'quit'. \ r \ n ".
  71. "To shut down the server type 'shutdown '. \ r \ n ".
  72. "To get help message type 'help'. \ r \ n ".
  73. "Php>";
  74. Socket_write ($ msgsock, $ msg, strlen ($ msg ));
  75. Continue;
  76. }
  77. /* Message displayed when the command entered by the client does not exist */
  78. $Talkback="PHP: unknow command '$ buf'. \ r \ nphp>";
  79. Socket_write ($ msgsock, $ talkback, strlen ($ talkback ));
  80. Echo "$ buf \ n ";
  81. } While (true );
  82. Socket_close ($ msgsock );
  83. } While (true );
  84.  
  85. /* Close the Socket connection */
  86. Socket_close ($ sock );
  87. ?>
  88.  


Save the preceding code and exit.

The above code roughly completes a function similar to the Telnet server, that is, when the server runs the program, the client can connect to port 10000 of the server for communication. Add the executable permissions of the file:
$ Chmod + x/home/heiyeluren/php_daemon2.php run the following command on the server:
$ Nohup/home/heiyeluren/php_daemon2.php & is running in the background. telnet through the Windows client:

C: \> telnet 192.168.0.1 10000 if the prompt is: connecting to 192.168.0.188... the connection to the host cannot be opened. If the connection fails at port 10000, the server is not enabled or the preceding program is not correctly executed. Check whether php is enabled-sockets. If the prompt is:

 
 
  1. ==========================================  
  2.  Welcome to the PHP Test Server.  
  3.  
  4.  To quit, type 'quit'.  
  5.  To shut down the server type 'shutdown'.  
  6.  To get help message type 'help'. 
  7. ==========================================  
  8. php> 
  9.  


It means that the server-side daemon process written in PHP is successfully connected, and three commands such as help, quit, and shutdown can be executed at the end of the php> prompt, if not, the following message is displayed:

 
 
  1. php> asdf  
  2. PHP: unknow command 'asdf'.  
  3.  

Run the help command to obtain help:

 
 
  1. php> help  
  2.  PHP Server Help Message  
  3.  
  4.  To quit, type 'quit'.  
  5.  To shut down the server type 'shutdown'.  
  6.  To get help message type 'help'.  


This server is not introduced and can be expanded on its own. The kill process is similar to the example. Through the above learning, we know that Linux php can also be used as a daemon process. If the design is good, the function will be powerful. However, we only need to learn about it here. We can study and update it on our own. This article has referred to the Linux php Chinese manual. It is very helpful to read more manual.

  1. Linux PHP compilation and generation extension and Configuration Modification
  2. Introduction to the configuration of Linux pam_mysql server program
  3. Configure the Java compiling and running environment for Linux JDK
  4. Linux disk partitioning tool and command usage
  5. Linux Kernel 2.6.33 official version released

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.