search
HomeBackend DevelopmentPHP TutorialHow to start and monitor the sh memcached process

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"

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

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

使用方法:

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

本文讲解了如何通过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
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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 Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools