


Linux daemon process example code analysis
The role of the resident process under Linux cannot be ignored, but the problems within it cannot be ignored either. How to start the process, how to end the process, and how to restart the process after the process hangs up must be designed reasonably. Let's look at an example of a shell-controlled PHP resident process.
Copy code The code is as follows:
#!/bin/sh
#filename test.sh
#The location of the file is absolutely positioned and does not change with the execution directory
cd $(cd "$(dirname "$0")";pwd)
readonly path=$(pwd)/
file=$1;
runfile="${path}data/${file} .run"
diefile="${path}data/${file}.die"
readonly file="${path}${file}.php"
if [ ! -f "$ file" ]; then
echo "please select a exists file"
elif [ ! -f "$runfile" ]; then
#Judge here. If the runfile file does not exist, it means that the process does not exist. , start the process below
echo $$>${runfile}
while true
do
if [ ! -f $diefile ]; then
If the diefile file does not exist, then Indicates that the program continues to execute, otherwise it enters else and performs the exit operation Clear runfile and diefile to exit
if rm -rf $runfile && rm -rf $diefile ; then
exit
fi
fi
else
# Here is existence Runfile is used to try to start the process
oldpid=`cat $runfile`
newpid=`ps aux | grep "process.sh $1" | grep -v grep | grep "$oldpid" | awk '{print $2}'`
if [[ $oldpid -eq $newpid ]]; then
#If the process number in the runfile is consistent with the running target process number, everything is fine^_^
echo "the process is running now"
exit
else
#If the process number in the runfile cannot match the running target process, it means there is a problem with the process. Delete the runfile directly and end the running process
echo "error situation, kill the run process and delete the run file"
ps aux | grep "process.sh $1" | grep -v 'grep' | awk '{print $2}' | grep -v $ $ | xargs --no-run-if-empty kill
If [ $? -eq 0 ]; then
/data/error
There are no limitations to this. What I want to explain here is this method of running a resident process.
When the runfile exists, but the process number does not match the killing process (that is, where the red else is executed), you must `grep -v $$`, the function is to filter out the currently running processes, otherwise they will be killed, and the subsequent ones will not be executed.
Another thing to note is about automatic restart
Automatic restart It can be placed in the crontab and executed every once in a while. The specific situation will be treated on a case-by-case basis.
Copy code The code is as follows:
crontab -e
#Open the current user schedule and add mode
#There are 5 asterisks in the schedule, f1, f2, f3, f4, f5,
#where f1 represents minute, f2 represents hour, f3 represents day, f4 represents month, f5 represents the day of the week
#* represents every minute/hour/day/month/day of the week, */n means to execute once every n minutes/hours/......
#Execute once every 2 minutes
Such a complete resident process function is completed. If you want to terminate the process, you only need to touch ${diefile} in the corresponding directory.
The above is the detailed content of Linux daemon process example code analysis. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)
