Home Backend Development PHP Tutorial How to start and monitor the sh memcached process

How to start and monitor the sh memcached process

Jun 11, 2018 am 10:41 AM

memcached 进程启动及监控
1.memcached_inc.sh
设置路径,端口等讯息。

#!/bin/sh
#config include
HOST=$(hostname)
SITE="mysite"
PORT=11211
MEMCACHED_PID_FILE="/tmp/memcached.pid"
MEMCACHED_DAEMON_PID_FILE="/tmp/memcached_daemon.pid"
MEMCACHED="memcached -d -m 64 -p $PORT -u memcache -l 127.0.0.1 -P $MEMCACHED_PID_FILE"
MEMCACHED_DAEMON_FILE="memcached_daemon.sh"
ERROR_LOG_FILE="${ROOT}/memcached_${SITE}_${HOST}_${PORT}.log"
Copy after login

2.gm_memcached.sh
控制memcached 启动,停止,重启。

#!/bin/sh
#memcached start and stop
#$1 action
ROOT=$(cd "$(dirname "$0")"; pwd)
. ${ROOT}/memcached_inc.sh
start() {
    if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then
        printf "memcached already running\n"
    else
        printf "starting memcached\n"
        $MEMCACHED
        sleep 2
        PID=$(cat $MEMCACHED_PID_FILE)
        printf "memcached is started PID:$PID\n"
        printf "starting memcached daemon\n"
        ${ROOT}/${MEMCACHED_DAEMON_FILE} &
        DAEMON_PID=$!
        echo ${DAEMON_PID} > ${MEMCACHED_DAEMON_PID_FILE}
        printf "memcached daemon is started PID:${DAEMON_PID}\n"
    fi
}
stop() {
    if [ -f "$MEMCACHED_DAEMON_PID_FILE" ] && [ -s "$MEMCACHED_DAEMON_PID_FILE" ]; then
        DAEMON_PID=$(cat $MEMCACHED_DAEMON_PID_FILE)
        rm -f ${MEMCACHED_DAEMON_PID_FILE}
        if [ ! -z ${DAEMON_PID} ]; then
            kill -9 ${DAEMON_PID}
        fi
        printf "memcached daemon is stopped\n"
    else
        printf "no memcached daemon running\n"
    fi
    sleep 1
    if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then
        PID=$(cat $MEMCACHED_PID_FILE)
        rm -f ${MEMCACHED_PID_FILE}
        if [ ! -z ${PID} ]; then
            kill -9 ${PID}
        fi
        printf "memcached is stopped\n"
    else
        printf "no memcached running\n"
    fi
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 3
        start
        ;;
    *)
        printf "Usage:$0 {start|stop|restart}\n"
        exit 1  
esac
exit 0
Copy after login

3.memcached_daemon.sh
监控memcached 进程,如进程失效则自动启动。

#!/bin/sh
#memcached daemon
ROOT=$(cd "$(dirname "$0")"; pwd)
. ${ROOT}/memcached_inc.sh
while :
do
    if [ -f "$MEMCACHED_PID_FILE" ] && [ -s "$MEMCACHED_PID_FILE" ]; then
        PID=$(cat $MEMCACHED_PID_FILE)
    else
        PID=""
    fi
   
    if [ -z "$PID" ] || [ -z $(ps aux|awk '{print $2}' | grep "^$PID$") ]; then
        $MEMCACHED
        sleep 1
        printf "[$(date +%Y-%m-%d' '%H:%M:%S)] ${SITE} ${HOST} memcached ${PORT} is restarted\n" >> $ERROR_LOG_FILE
        echo "Subject: ${SITE} ${HOST} memcached ${PORT} is restarted $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail me@gmail.com
    fi
    sleep 5
done
exit 0
Copy after login

使用方法:

./gm_memcached.sh start   #启动memcached
./gm_memcached.sh stop    #停止memcached
./gm_memcached.sh restart #重启memcached
Copy after login

本文讲解了如何通过sh memcached 进程启动及监控 ,更多相关内容请关乎php中文网。

相关推荐:

介绍自动登入google play下载app report 的相关内容

关于Apache rewrite 的相关介绍

关于php click captcha 验证码类的介绍

The above is the detailed content of How to start and monitor the sh memcached process. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles