系统开机启动时会去加载/etc/init.d/下面的脚本,通常而言每个脚本文件会自定义实现程序的启动;若想将新的程序开机自启动,只需在该目录下添加一个自定义启动程序的脚本,然后设置相应规则即可。
- 修改redis内置的启动脚本
vi /data/redis-5.0.5/utils/redis_init_script
将图中箭头部分的内容修改为部署环境的真实路径即可
- 或者在/etc/init.d/下新建
vim /etc/init.d/redis
#!/bin/sh #chkconfig: 2345 80 90 # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. REDISPORT=6379 #端口号,这是默认的,如果你安装的时候不是默认端口号,则需要修改 REDISPATH=/usr/local/bin/ #redis-server启动脚本的所在目录,你如果忘了可以用find / -name redis-server 或whereis redis-server找到 EXEC=${REDISPATH}/redis-server CLIEXEC=${REDISPATH}/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid #在redis.conf中可找到该路径 CONF="${REDISPATH}/redis.conf" #redis.conf的位置, 如果不和redis-server在同一目录要修改成你的redis.conf所在目录 case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac
设置可执行权限:chmod 777 /etc/init.d/redis
启动redis:/etc/init.d/redis start
- 将redis_init_script拷贝到/etc/init.d目录下重命名为redis
cp /data/redis-5.0.5/utils/redis_init_script /etc/init.d/redis
- 设置开机启动
- 打开redis命令:
service redis start
- 关闭redis命令:
service redis stop
- 设为开机启动:
chkconfig redis on
- 设为开机关闭:
chkconfig redis off