方法一:采用Redis官方提供的自启动脚本,如果没有则采用第二种方法
# 1. 修改redis.conf daemonize 参数为 yes
daemonize yes 代表开启守护进程模式。此时是单进程多线程的模式,redis将在后台运行,并将pid写入redis.conf--pidfile文件中,此时redis将一直运行,除非手动kill
daemonize no 当前界面将进入redis的命令行界面,exit强制退出或者关闭连接工具(xshell等)都会导致redis进程退出
# 2. 设置启动脚本
cd /usr/local/redis/utils # 找到 install_server.sh 脚本
./install_server.sh # 执行该脚本,会提示你输入redis配置文件、日志文件、数据目录和redis-server的存储位置,如果不需要修改路径,直接回车即可;如果需要修改,则直接输入你想要修改的内容即可
#如:Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/src/redis-server
# 如果报错
This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
# 执行以下命令
vi ./install_server.sh
# 注释下面的代码即可
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]