Redis Startup/Shutdown/Restart service script under Linux

Source: Internet
Author: User

Scripting Features:

enable the normal startup, shutdown, and restart of a single Redis instance in a multi-instance redis single-machine scenario. Complete the following common functions for system standard Services : Start|stop|status|restart

Note: The Redis program code masks the HUP signal and does not support the online reload configuration file, so the reload function is removed.

This script optimizes the Redis stop and restart logic to resolve data loss issues when the original Redis script is closed.

Script Name:

Redis # can be named in multiple instances by instance port , such as : redis-6001 , to differentiate between different instances

Script usage:

1. Create a new Redis file in the /etc/rc.d/init.d/ directory and copy the contents of the script

2. Chkconfig--add Redis # Registration Service

3. chkconfig --level 345 redis on # specifies that the service runs at 3,4,5 levels

4. I redis program installed in the/usr/local/redis directory, configured as/usr/local/redis/bin/redis.conf, such as installed in other directories, please modify their own

Script Parameters :

redis-p [Port] [Start|stop|status|restart]

Parameter Description:

- p [port]: Specify the port for the Redis instance, the server for the multi-instance

start: Redis Service to start the specified port

Stop: The Redis service that stops the specified port

Status: Process status

Restart: Turn off the Redis service before starting the redis Service

Note: When you do not specify a port, the script defaults to the Redis that launches the 6379 Port

Usage examples:

service redis-p 6381 start # starts redis for 6381 Port instances

/etc/init.d/redis Start # default start of Redis for 6379 Port instances

Script content:

#!/bin/bash
#chkconfig: 2345 55 25
#description: Starts,stops and restart the Redis-server
#Ver: 1.1
#Write by ND Chengh (200808)
#usage:./script_name-p [port] {Start|stop|status|restart}

# Source function library.
. /etc/rc.d/init.d/functions

# Source Networking configuration.
. /etc/sysconfig/network

# Check networking is up.
["$NETWORKING" = "no"] && exit 0

Retval=0
redis_port=6379
Pid=

If ["$" = "-P"]; Then
Redis_port=$2
Shift 2
Fi

Redis_dir= "/usr/local/redis"
Redis= "${redis_dir}/bin/redis-server"
prog=$ (basename $REDIS)

conf= "${redis_dir}/bin/redis-${redis_port}.conf"

if [!-f $CONF]; Then
If [-F "${redis_dir}/bin/redis.conf"];then
conf= "${redis_dir}/bin/redis.conf"
Else
Echo-n $ "$CONF not exist."; Warning;echo
Exit 1
Fi
Fi

Pid_file= ' grep ' pidfile ' ${conf}|cut-d '-f2 '
Pid_file=${pid_file:=/var/run/redis.pid}
Lockfile= "/var/lock/subsys/redis-${redis_port}"

if [!-X $REDIS]; Then
Echo-n $ "$REDIS not exist."; Warning;echo
Exit 0
Fi


Start () {

Echo-n $ "Starting $PROG:"
$REDIS $CONF
Retval=$?
If [$RETVAL-eq 0]; Then
Success;echo;touch $LOCKFILE
Else
Failure;echo
Fi
Return $RETVAL

}

Stop () {

Echo-n $ "Stopping $PROG:"

If [-f $PID _file]; then
Read PID < "$PID _file"
Else
Failure;echo;
Echo-n $ "$PID _file not found."; Failure;echo
return 1;
Fi

If Checkpid $PID; Then
Kill-term $PID >/dev/null 2>&1
Retval=$?
If [$RETVAL-eq 0]; then
Success;echo
Echo-n "Waiting for Redis to shutdown ..."
While Checkpid $PID;d o
Echo-n "."
Sleep 1;
Done
Success;echo;rm-f $LOCKFILE
Else
Failure;echo
Fi
Else
Echo-n $ "Redis is dead and $PID _file exists."; Failure;echo
Retval=7
Fi
Return $RETVAL

}

Restart () {
Stop
Start
}

Rhstatus () {
Status-p ${pid_file} $PROG
}

Hid_status () {
Rhstatus >/dev/null 2>&1
}

Case "$" in
Start
Hid_status && Exit 0
Start
;;
Stop
Rhstatus | | Exit 0
Stop
;;
Restart
Restart
;;
Status
Rhstatus
Retval=$?
;;
*)
echo $ "Usage: $0-p [port] {Start|stop|status|restart}"
Retval=1
Esac

Exit $RETVAL



Reprinted from: http://blog.csdn.net/chhxo/article/details/8673020

Redis Startup/Shutdown/Restart service script under Linux

Related Article

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.