接上一篇:银河麒麟服务器v10 sp1 安装 redis_csdn_aspnet的博客-CSDN博客
将redis_init_script文件复制到/etc/init.d下,重命名为redisd:
root@xxx-pc:cp /usr/local/redis/redis-7.0.11/utils/redis_init_script /etc/init.d/redisd
内容如下:
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides: redis_6379
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
REDISPORT=6379
#EXEC=/usr/local/bin/redis-server
#CLIEXEC=/usr/local/bin/redis-cli
EXEC=/usr/local/redis/redis-7.0.11/src/redis-server
CLIEXEC=/usr/local/redis/redis-7.0.11/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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
给文件授权,此步看需要执行
root@xxx-pc:/usr/local/redis/redis-7.0.11# sudo chmod +x /etc/init.d/redisd
执行:
root@xxx-pc:/usr/local/redis/redis-7.0.11# sudo update-rc.d redisd defaults
root@xxx-pc:/usr/local/redis/redis-7.0.11# service redisd start
启动并没有成功:
经过一番查找发现:
Ubuntu18版本开始,systemd
被用于系统进程启动,d
表示守护进程daemon,也就是系统后台运行的服务进程,取代了之前的initd。
现代 Linux 都采用 systemd 来作为守护进程,例如在 Ubuntu 18.04 上它们都指向同一个 systemd
而 init 和 systemd 的区别如下:
init 和 systemd 都是 Linux 的 init 守护进程,但最好使用后者,因为它在现代的 Linux 发行版中很常用。init 使用 service,而 systemd 用 systemctl 管理 Linux 服务。
init 守护进程是 Linux 内核执行的第一个进程,它的进程 ID (PID) 始终为 1。它的目的是初始化、管理和跟踪系统服务和守护进程。换句话说,init 守护进程是系统上所有进程的父进程。
要创建一个服务,需要编写 shell 脚本,并存储在 /etc/init.d/ 目录下,通过 service 命令启动、停止、重新启动服务。例如如下的 /etc/init.d/myservice 脚本。
以下为一个 myservice.service 文件的样例:
[Unit]
Description=Some Description
Requires=syslog.target
After=syslog.target
[Service]
ExecStart=/usr/sbin/<command-to-start>
ExecStop=/usr/sbin/<command-to-stop>
[Install]
WantedBy=multi-user.target
根据样例修改,并定义文件名为redis-server.service:
[Unit]
Description=Redis server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/bin/bash -c '/usr/local/redis/redis-7.0.11/src/redis-server /usr/local/redis/redis-7.0.11/redis.conf'
ExecStop=/bin/bash -c '/usr/local/redis/redis-7.0.11/src/redis-cli shutdown'
Restart=always
PrivateTmp=true
[Install]
WantedBy=multi-user.target
以上路径修改为你自己的目录或路径即可。
root@xxx-pc:~# cp /home/xxx/redis-server.service /lib/systemd/system/redis-server.service
完成后按顺序执行:
sudo systemctl daemon-reload
sudo systemctl enable redis-server.service
sudo systemctl status redis-server.service
sudo systemctl start myservice.service
说明redis服务启动成功了。
systemctl说明:
查看某个服务的状态:
sudo systemctl status myservice.service
启动,停止,重启服务:
sudo systemctl start myservice.service
sudo systemctl stop myservice.service
sudo systemctl restart myservice.service
服务开机自启动,取消开机自启动:
sudo systemctl enable myservice.service
sudo systemctl disable myservice.service
重新加载某个服务,所有服务的配置文件:
sudo systemctl reload myservice.service
sudo systemctl daemon-reload
查看某个服务设置的参数:
sudo systemctl show myservice.service
杀死某个服务的所有子进程:
sudo systemctl kill myservice.service