Skip to content

Instantly share code, notes, and snippets.

@tessro
Created December 16, 2009 14:20
Show Gist options
  • Save tessro/257849 to your computer and use it in GitHub Desktop.
Save tessro/257849 to your computer and use it in GitHub Desktop.
A CentOS initscript for Redis
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
redis="/usr/local/sbin/redis-server"
prog=$(basename $redis)
REDIS_CONF_FILE="/etc/redis/redis.conf"
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
start() {
[ -x $redis ] || exit 5
[ -f $REDIS_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $redis $REDIS_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
killproc $redis -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
@jackeylu
Copy link

How about stopping the Redis with the command __"shutdown" __ provided by Redis? will it be better?

@tessro
Copy link
Author

tessro commented Jun 18, 2012

Caveat emptor: I'm no longer running CentOS, and haven't tested this.

When Redis receives a SIGTERM it performs a graceful shutdown. There's no need to use the SHUTDOWN command. (See https://github.com/antirez/redis/blob/unstable/src/redis.c#L860-865)

However, this script was written to use SIGQUIT instead of SIGTERM, which was probably a mistake. If you're using this script, you might want to try using SIGTERM (change L43). That should give you graceful shutdown without increasing complexity.

@sheldonh
Copy link

Looking at the source, I think redis ignores SIGHUP.

@coder4web
Copy link

Thanks a lot for this script!
Wrote some install notes with it :) https://gist.github.com/coder4web/5762999

@cagerton
Copy link

I'd like to suggest adding a user variable and changing the start command to this (line 34):
daemon --user $user $redis $REDIS_CONF_FILE

@mrcmorales
Copy link

Hi,

Stopping with killproc there are the problem that is possible lost data.

Redis in his documentation say:

Redis will spontaneously save the dataset only from time to time (for instance after at least five minutes if you have at least 100 changes in your data), so if you want your database to persist and be reloaded after a restart make sure to call the SAVE command manually every time you want to force a data set snapshot. Otherwise make sure to shutdown the database using the SHUTDOWN command

I'm in correct?

How Can I do that this script stop with shutdown command ?

Thanks

Regards

@matthewlzb
Copy link

mrcmorales:
here is what i did in .etc/init.d/redis
...
redis="/usr/local/bin/redis-server"
prog=$(basename $redis)
CLIEXEC=/usr/local/bin/redis-cli
REDISPORT=6379
REDIS_CONF_FILE="/etc/redis/redis.conf"
...
stop() {
echo -n $"Stopping $prog: "
#killproc $prog -QUIT
$CLIEXEC -p $REDISPORT shutdown
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
...

Regards,

@aaronyao
Copy link

great

@bojanvidanovic
Copy link

Thanks.

@dominicnicholas
Copy link

Thanks for this. Most helpful.

@vinceyuan
Copy link

How to set pid file? I set it in the .conf file, but it should be set in the init script too.

@scaryguy
Copy link

Well, if you don't have any special reasons to compile Redis from source and do all of this chkconfig initscripts, you may consider to use remi repository to install Redis without any extra effort except typing yum install redis. After installation just add it to chkconfig list as usual: chkconfig redis on

For more info:
b. Install RHEL EPEL and remi repository seciton
http://sonercan.com/how-to-prepare-a-ruby-on-rails-production-server-from-scratch-part-2/

and
http://sonercan.com/how-to-prepare-a-ruby-on-rails-production-server-from-scratch-part-5/

@sima-ktb
Copy link

tnx for useful script.
"restart" get failed error!

@tahajahangir
Copy link

The best fork from this gist (currently) is: https://gist.github.com/tahajahangir/155176867afed0ff80ad
It contains this improvements:

  • using pid file for stop/status/start
  • support changin user (and defaults to redis user)
  • stop by sigterm (not sigquit)

@vikas027
Copy link

I have redis v2.8.19-2 installed on my CentOS 6.5 server.

# rpm -qa | grep redis
redis-2.8.19-2.el6.art.x86_64

I have just commented out below line from stop () function and assigned retval=1

stop() {
    echo -n $"Stopping $name: "
    #[ -x $shut ] && $shut
    retval=1
    if [ -f $pidfile ]
    then
        # shutdown haven't work, try old way
        killproc -p $pidfile $name
        retval=$?
    else
        success "$name shutdown"
    fi
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

Now, it does not throws error in stop, start, status, or restart.

Hope this helps someone.

Regards,
Vikas

Copy link

ghost commented May 27, 2015

This script is not working for me. Anyone know what is going on?

Starting redis-server: chown: invalid user: redis:redis' /usr/bin/dirname: extra operand.pid'
Try `/usr/bin/dirname --help' for more information.
runuser: user redis does not exist

@johlo
Copy link

johlo commented Jun 8, 2015

@tpaveyjr I used the https://gist.github.com/tahajahangir/155176867afed0ff80ad fork and had the same problem (on CentOS 6.6). Solved by adding --pidfile "/var/run/redis.pid" to the daemon call

@ac-astuartkregor
Copy link

ac-astuartkregor commented Oct 19, 2016

This didn't quite work for me, so I tweaked it to use more generic/plain constructs: https://gist.github.com/ac-astuartkregor/5d11fe0e9f028087b66ac90423293263
This assumes you've told redis to run with daemonize yes in the config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment