SoFunction
Updated on 2025-03-10

Memcached process monitoring implemented by shell scripts

The WEB server uses memcached, but I don’t know why memcached always hangs up (basically about 20 minutes to 50 minutes), which leads to errors in some website pages when accessing; after defining the log, I can’t find anything when viewing the log; it is initially determined that it is related to the previous update of libevent. Because of the online server, I will make up for it with scripts first.

#!/bin/sh 
pid=`ps aux|grep -v grep|grep memcached|awk '{print $2}'` 
memcached=`/usr/local/memcached/bin/memcached -u www &` 
nginx=`/usr/local/nginx/sbin/nginx -s reload &` 
if [ -z "$pid"] 
then 
echo $memcached 
echo $nginx 
fi

The above script mainly reviews two knowledge points, one is awk and the other is a conditional expression of if; of course, those single quotes, double quotes, and special single quotes are also annoying. It's just a basic script, which is very terrible, but it can implement the functions I want. First, determine whether the memcached process exists. If it does not exist, start memcached and overload nginx.

Finally, add it to the system task and determine it every 5 minutes:

*/15 * * * * /root/

Completed!

There are more about if conditional expressions. You can search online, so I won’t repeat them here.