Linux under Redis boot/shutdown/Restart service script __linux

Source: Internet
Author: User
Tags redis

Scripting Features:

A single Redis instance is implemented to redis the normal startup, shutdown, and restart of multiple instances. Complete the following common functions of System standard service: Start|stop|status|restart

Note: The Redis program code shields the HUP signal, does not support the online overload configuration file, therefore removes the reload function.

This script optimizes the Redis stop and restart logic to resolve data loss problems when the original Redis script closes.

Script Name:

Redis #在多实例里可以按实例端口, such as: redis-6001 naming, to distinguish between different instances

Scripting Usage:

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

2. Chkconfig--add Redis #注册服务

3. Chkconfig--level 345 Redis on #指定服务在3, 4, 5 levels not running

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

Script Parameters:

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

Parameter description:

-P [Port]: Specifies the port for the Redis instance, the server for multiple instances

Start: Start the Redis service for the specified port

Stop: Stops the Redis service for the specified port

Status: Process Status

Restart: First turn off the Redis service, then start the Redis service

Note: When no port is specified, the script defaults to the boot 6379 port Redis

Usage Example:

Service Redis-p 6381 Start #启动6381端口实例的redis

/etc/init.d/redis Start #默认启动6379端口实例的redis

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



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

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.