With this command, we will write shell scripts in php and use loops to keep our scripts running. No matter whether the terminal window is closed or not, we can keep running php scripts. In Unix, The nohup command function means to run the command without hanging up. At the same time, nohup puts all the output of the program into the nohup. out file in the current directory. If the file cannot be written, it is placed in <用户主目录> /Nohup. out file. With this command, we will write shell scripts in php and use loops to keep our scripts running. No matter whether the terminal window is closed or not, we can keep running php scripts.
Write a PHP applet immediately. The function is to record the time every 30 seconds and write it to the file.
The Code is as follows:
# Vi for_ever.php
#! /Usr/local/php/bin/php
Define ('root', dirname (_ FILE __).'/');
Set_time_limit (0 );
While (true ){
File_put_contents(ROOT.'for_ever.txt ', date ('Y-m-d H: I: s'). "\ n", FILE_APPEND );
Echo date ('Y-m-d H: I: s'), 'OK! ';
Sleep (30 );
}
?>
Save and exit, and then grant the executable permission to the for_ever.php file:
# Chmod + x for_ever.php
Run the following command in the background:
# Nohup/home/andy/for_ever.php.php &
Remember to add the & symbol at the end so that you can run it in the background.
After executing the preceding command, the following prompt is displayed:
[1] 5157
Nohup: appending output to 'nohup. out'
All command execution output information is stored in the nohup. out file.
Now you can open for_ever.php in the same directory as for_ever.txt and nohup. out to see the effect!
Okay, it will run forever. How can we end it?
# Ps
PID TTY TIME CMD
4247 pts/1 00:00:00 bash
5157 pts/1 00:00:00 for_ever.php
5265 pts/1 00:00:00 ps
# Kill-9 5157
Find process no. 5157 and you will see
[1] + Killed nohup/home/andy/for_ever.php
OK!
================================
In many projects, many similar back-end Scripts may need to be periodically executed through crontab. For example, you can check the user status every 10 seconds. The script is as follows:
@ File:/php_scripts/scan_userstatus.php
The Code is as follows:
#! /Usr/bin/env php-q
$ Status = has_goaway ();
If ($ status ){
// Done
}
?>
Execute the script scan_userstatus.php at regular intervals through crontab.
# Echo "*: */10 ***/php_scripts/scan_userstatus.php"
In this way, the script will be executed every 10 seconds.
We found that in a short time, the memory resources of the script have not been released, and a new script has been enabled. That is to say, the new script is started, and the resources occupied by the old script have not been released as expected. This wastes a lot of memory resources over time. We have improved the script as follows:
@ File:/php_scripts/scan_userstatus.php
The Code is as follows:
#/Usr/bin/env php-q
While (1 ){
$ Status = has_goaway ();
If ($ status ){
// Done
}
Usleep (10000000 );
}
?>
In this way, no crontab is required. Run the following command to execute the script to achieve the same functional effect:
# Chmod + x/php_scripts/scan_userstatus.php
# Nohup/php_scripts/scan_userstatus.php &
Here, we run the script in the background through &. To prevent the process from being killed as the terminal session window closes, we use the nohup command. Is there a way to run the nohup command, just like the Unin/Linux Daemon command. Next we will talk about the daemon functions.
What is a daemon? A daemon is often regarded as a background task that does not control the terminal. It has three notable features: it runs in the background, and is separated from the process that starts it, without the need to control the terminal. The common implementation method is fork ()-> setsid ()-> fork (). The details are as follows:
@ File:/php_scripts/scan_userstatus.php
The Code is as follows:
#/Usr/bin/env php-q
Daemonize ();
While (1 ){
$ Status = has_goaway ();
If ($ status ){
// Done
}
Usleep (10000000 );
}
Function daemonize (){
$ Pid = pcntl_fork ();
If ($ pid ===-1 ){
Return FALSE;
} Else if ($ pid ){
Usleep (500 );
Exit (); // exit parent
}
Chdir ("/");
Umask (0 );
$ Sid = posix_setsid ();
If (! $ Sid ){
Return FALSE;
}
$ Pid = pcntl_fork ();
If ($ pid ===-1 ){
Return FALSE;
} Else if ($ pid ){
Usleep (500 );
Exit (0 );
}
If (defined ('stdin ')){
Fclose (STDIN );
}
If (defined ('stdout ')){
Fclose (STDOUT );
}
If (defined ('stderr ')){
Fclose (STDERR );
}
}
?>
After implementing the daemon function, you can create a resident process, so you only need to execute it once:
#/Php_scripts/scan_userstatus.php
The two key php functions are pcntl_fork () and posix_setsid (). Fork () is a process that creates a copy of the running process. The copy is considered as a sub-process, and the original process is considered as a parent process. After fork () runs, it can be separated from the process that starts it and the terminal control. This also means that the parent process can exit freely. Pcntl_fork () return value.-1 indicates execution failed, 0 indicates sub-process, and return process ID indicates that it is in the parent process. Here, exit the parent process. Setsid (), which first makes a new process the "Leader" of a new session and stops the process from controlling the terminal. This is also the most critical step for the daemon process, the process will not be forcibly exited as the terminal is closed. This is a critical step for a resident process that will not be interrupted. This step is not necessary for the last fork (), but is usually done in this way. Its biggest significance is to prevent obtaining control terminals. (If a terminal device is opened directly without the O_NOCTTY flag, the control terminal is obtained ).
Other matters:
1) chdir () puts the daemon in a directory that always exists. Another advantage is that your resident process does not restrict your umount file system.
2) umask () sets the file mode and creates a mask to the maximum extent allowed. If a daemon needs to create a file with readable and writable permissions, an inherited mask with stricter permissions will be counterproductive.
3) fclose (STDIN), fclose (STDOUT), and fclose (STDERR) Close the standard I/O Stream. NOTE: If an output (echo) exists, the daemon fails. Therefore, STDIN, STDOUT, and STDERR are usually redirected to a specified file.