Node. js-how can I keep running my php script as a daemon?

Source: Internet
Author: User
I used swoole to write a server, and planned to put it on the server as a daemon, and allowed the script to be restarted as soon as possible after unexpectedly exiting due to an error. what can I do? I used swoole to write a server, and planned to put it on the server as a daemon, and allowed the script to be restarted as soon as possible after unexpectedly exiting due to an error. what can I do?

Reply content:

I used swoole to write a server, and planned to put it on the server as a daemon, and allowed the script to be restarted as soon as possible after unexpectedly exiting due to an error. what can I do?

Run the linux nohup command to run the php script and then write a shell to monitor the running status of the script. if the script is dead, restart it.

Write the script as a loop, such as while (1), so that you can run it all the time.

In this script header, if the script process already exists, it will exit and not be executed,

In addition, write a linix scheduled task, such as executing the php script every minute.

After a script is executed, the script will run continuously, and the scheduled task will execute the script every minute. if the script is alive, it will exit and start execution when it is dead.

Write the php script. We recommend that you periodically check the memory usage so that the core logic is not written. This is related to the business.

If (memory_get_usage ()> 100*1024*1024) {exit (0); // exit the program with memory larger than MB to prevent the task terminal from being killed by the system due to memory leakage}

Assume that the php file path is/Root/run. php
Open Terminal

setsid php /root/run.php > /dev/null &

Edit the process monitoring script. the script restarts automatically when the process does not exist./Root/monitor. sh

#!/bin/bashalive=`ps aux|grep root\/run|grep -v grep|wc -l`if [ $alive -eq 0]thenphp /root/run.php > /dev/null &fi

Add a scheduled task (detected every minute)

crontab -e* * * * * /root/monitor.sh > /dev/null &

Manual

Guard a PHP service that may suddenly exit with a simple and reliable Shell script

Add the boot command to/etc/rc. local:

nohup /path/to/swoole.sh >>/path/to/swoole.sh.log 2>&1 &

The content of swoole. sh is as follows:

#!/bin/shPREFIX=/home/eechenINTERVAL=1nohup php ${PREFIX}/swoole.php >>${PREFIX}/swoole.log 2>&1 & echo $! > ${PREFIX}/swoole.pidwhile [ 1 ]; do    if [ ! -d /proc/`cat ${PREFIX}/swoole.pid` ]; then        nohup php ${PREFIX}/swoole.php >>${PREFIX}/swoole.log 2>&1 & echo $! > ${PREFIX}/swoole.pid        echo 'NEW_PID:'`cat ${PREFIX}/swoole.pid && date '+%Y-%m-%d %H:%M:%S'`    fi    sleep ${INTERVAL}done

Where:

Nohup indicates that the SIGHUP signal (number 1) is ignored. for example, the SIGHUP signal (kill-sighup pid) sent when the terminal is exited is ignored.> $ {PREFIX}/swoole. log indicates redirecting the standard output (> indicates appending,> indicates overwriting) to the file swoole. log2> & 1 indicates redirecting a standard error (2: stderr) to the standard output (1: stdout ). add & at the end of the script to run the command in the background. $! Indicates the PID of the PHP process running in the background.

That is, swoole. sh checks whether the directory/proc/PID exists every one second. if it does not exist, restart the service.
Swoole. sh. log records the service restart time.
Swoole. log records the output of the service itself.

For example, use Shell to guard vmstat:

Vmstat. sh #! /Bin/shPREFIX =/home/eechenINTERVAL = 1 nohup vmstat 1 >> {PREFIX}/vmstat. log 2> & 1 & echo $! >$ {PREFIX}/vmstat. pidwhile [1]; do if [! -D/proc/'cat $ {PREFIX}/vmstat. pid ']; then nohup vmstat 1 >$ {PREFIX}/vmstat. log 2> & 1 & echo $! >$ {PREFIX}/vmstat. pid echo 'new _ PID: ''cat $ {PREFIX}/vmstat. pid & date '+ % Y-% m-% d % H: % M: % s' fi sleep $ {INTERVAL} done # run nohup/home/eechen/vmstat. sh>/home/eechen/vmstat. sh. log 2> & 1 & # kill. we can see that vmstat is restarted and kill 'cat/home/eechen/vmstat. pid'

The vmstat. sh script can also be implemented using PHP:

nohup php /home/eechen/vmstat.php >>/home/eechen/vmstat.php.log 2>&1 &
  >$prefix/vmstat.log 2>&1 & echo $! > $prefix/vmstat.pid");while ( 1 ) {    if ( !file_exists('/proc/'.trim(file_get_contents("$prefix/vmstat.pid"))) ) {        shell_exec("nohup vmstat 1 >>$prefix/vmstat.log 2>&1 & echo $! > $prefix/vmstat.pid");        echo 'NEW_PID:'.trim(file_get_contents("$prefix/vmstat.pid")).' '.date('Y-m-d H:i:s');    }    sleep($interval);}

1. the crontab must be available. check whether there are any crontab entries (ps-ef | grep...). if there is exit, create and execute the crontab.
2. a process that has been running can have an endless loop ..
3. the crontab script can detect the state of the process that has been running. if there is a problem, wait, and no response is returned, you have to manually kill the problem and restart it.

Linux shell execution

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.