Use Shell script to monitor ports and set Redis to turn off automatic startup

Posted by jnewing on Mon, 24 Jan 2022 08:32:02 +0100

Click Redis installation

Make the file have miscellaneous attributes

chmod a+x /etc/init.d/redis

Crond service related commands( reference resources)

crond service

To install crontab:

yum install crontabs

Service operation instructions:

/sbin/service crond start //Start service
/sbin/service crond stop //Shut down service
/sbin/service crond restart //Restart service
/sbin/service crond reload //service crond reload 

To view crontab service status:

service crond status

To start the crontab service manually:

service crond start

Check whether the crontab service is set to startup and execute the command:

ntsysv

Add automatic startup:

chkconfig –level 35 crond on

crontab command details

Command format:

crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]

Command function:

With the crontab command, we can execute the specified system instructions or shell script at fixed intervals. The unit of time interval can be any combination of minutes, hours, days, months, weeks and above. This command is very suitable for periodic log analysis or data backup.

Command parameters:

**-u user * *: used to set the * * crontab * * service of a user. For example, "* * - u ixdba * *" means to set the crontab service of ixdba user. This parameter is generally run by root user.

**File * *: * * file * * is the name of the command file, which means that * * file * * is used as the task list file of * * crontab * * and loaded into * * crontab * *. If this file is not specified on the command line, the * * crontab * * command accepts commands typed on standard input (keyboard) and loads them into * * crontab * *.

**-e * *: edit the contents of a user's * * crontab * * file. If no user is specified, it means that the * * crontab * * file of the current user is edited.

**-l * *: displays the contents of a user's * * crontab * * file. If no user is specified, the contents of the current user's * * crontab * * file are displayed.

**-r * *: delete a user's * * crontab * * file from the * * / var/spool/cron * * directory. If no user is specified, the current user's * * crontab * * file will be deleted by default.

**-i * *: prompt for confirmation when deleting the user's crontab file.

Add Redis related files and sh commands

Redis command related

In * * / etc / init Create a new redis file (directly * * vim redis * *) in the d * * directory, as shown in the following figure

**/etc/init.d/redis * * file content

#redis.sh
#$1 is the first parameter passed in from the server
case $1 in
	'start' )
	#Controls the operation of redis startup
	/usr/local/soft/redis6/bin/redis-server /usr/local/soft/redis6/conf/redis.conf &
		;;
	
	'stop' )
	#Controls the closing of redis
	/usr/local/soft/redis6/bin/redis-cli shutdown
		;;
	
	'restart' )
	#Controls the restart of redis, that is, close it first and then start it	
	/usr/local/soft/redis6/bin/redis-cli shutdown
	/usr/local/soft/redis6/bin/redis-server /usr/local/soft/redis6/conf/redis.conf &
		;;
	
	'status' )
	#Viewing redis status
	 status=`pstree |grep redis |wc -l`
	 
	 if [ $status == 0 ]; then
	 	echo 'redis close'
	 else
	 	echo -e "\033[31;47m redis running \033[0m"	
	 fi
		;;

	'redis-cli' )
	#Start redis client
	/usr/local/soft/redis6/bin/redis-cli
		;;
		* )
	#Prompt to user when input error
	echo "Please input [status],[start],[stop],[redis-cli]"
	echo "For example:service redis start"

		;;				

esac

Then execute the following instructions:

//Authorization document
chmod a+x /etc/init.d/redis
//Executive document
sh /etc/init.d/redis

After that, you can quickly operate redis by entering an instruction

service redis stop|start|restart

Listen to Redis6379 port and restart it when redis is turned off

Create script

vim redis_monitor_restart.sh

Script content

#!/bin/bash
ListeningPort=$(netstat -an | grep ":6379" | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l)
if [ $ListeningPort -eq 0 ]; then
    {
        echo "Redis has stopped, restart..."
        # If port 6379 goes down, restart the service
        /etc/init.d/redis restart
    }
else
    {
        echo "6379 Port OK"
    }
fi


Give script execution permission

chmod +x  /usr/local/soft/shells/redis_monitor_restart.sh

Configure Crontab

Write scheduled tasks

crontab -e

Fill in the content (this is to monitor whether the port is alive once a minute)

* * * * * sh /usr/local/soft/shells/redis_monitor_restart.sh

Start Crontab

service crond start

Set startup and self startup

chkconfig –level 35 crond on

Command set (restart)

//redis startup
service redis start
//crontab startup
service crond  start

Topics: Linux Operation & Maintenance Redis shell server