守护进程(Daemon)是运行在后台的一种特殊进程。它独立于控制终端并且周期性地执行某种任务或等待
处理某些发生的事件。守护进程是一种很有用的进程。
Linux的大多数服务器就是用守护进程实现的。比如,Internet服务器inetd,Web服务器httpd等。
同时,守护进程完成许多系统任务。比如,作业规划进程crond,打印进程lpd等。
Linux下的常驻进程的作用不可忽略,但这里面的问题也不能忽略,怎么启动进程,怎么结束进程,怎么在进程挂掉之后重启进程都要设计的合理。下面看一个shell控制的php常驻进程的例子。
不废话,直接捞干货,上代码,通过代码来讲解更容易理解:
#!/bin/sh
#filename test.sh #绝对定位该文件的位置,不随执行目录而变化 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
#这里进行判断如果RunFile文件不存在,则表示该进程不存在,下面启动进程
echo $$>${RunFile} while true do if [ ! -f $DieFile ]; then
#这里如果DieFile文件不存在,则表示程序继续执行,否则进入else,执行退出操作 /usr/bin/php -f ${file} touch $RunFile sleep 1 else
#如果DieFile文件存在清除RunFile和DieFile退出 if rm -rf $RunFile && rm -rf $DieFile ; then exit fi fi done else #这里是在存在RunFile的情况下试图启动该进程
oldpid=`cat $RunFile` newpid=`ps aux | grep "process.sh $1" | grep -v grep | grep "$oldpid" | awk '{print $2}'` if [[ $oldpid -eq $newpid ]]; then
#如果RunFile中的进程号和正在运行的目标进程号一致,表明一切安好^_^ echo "the process is runing now" exit else
#如果用RunFile中的进程号匹配不到正在运行的目标进程,则表示进程有问题,直接删除RunFile并结束运行的进程 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 rm -f $RunFile else echo $?>${path}/data/error fi fi fi
只里面有几个要强调的地方:
我用这个shell去调用php程序,这个没有局限性,这里要说明的是这种运行常驻进程的方法
在RunFile存在,但进程号对不上杀进程时(也就是红色额else所执行的地方),一定要 `grep -v $$`,作用是过滤掉当前运行的进程,要不然都被杀掉了,将后面的就不执行了
还有一个要注意的地方就是关于自动重启了
自动重启可以放在crontab中,每隔一段时间执行一次,具体情况具体对待。
crontab -e #打开当前用户日程表,添加模式 #日程表中有5个星号,f1,f2,f3,f4,f5,
#其中f1表示分,f2表示时,f3表示日,f4表示月,f5表示一个星期第几天
#*表示每分/时/日/月/周天,*/n表示每n分/时/......执行一次 */2 * * * * /root/test.sh #没2分钟执行一次
守护进程的编程本身并不复杂,复杂的是各种版本的Unix的实现机制不尽相同,
造成不同 Unix环境下守护进程的编程规则并不一致。
需要注意,照搬某些书上的规则(特别是BSD4.3和低版本的System V)到Linux会出现错误的。
以上就是关于linux中的守护进程的内容,更多相关内容请关注PHP中文网(www.php.cn)!

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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),
