-
-
Save tessro/257849 to your computer and use it in GitHub Desktop.
#!/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 |
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.
Looking at the source, I think redis ignores SIGHUP.
Thanks a lot for this script!
Wrote some install notes with it :) https://gist.github.com/coder4web/5762999
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
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
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,
great
Thanks.
Thanks for this. Most helpful.
How to set pid file? I set it in the .conf file, but it should be set in the init script too.
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/
tnx for useful script.
"restart" get failed error!
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)
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
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
@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
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.
How about stopping the Redis with the command __"shutdown" __ provided by Redis? will it be better?