1.建立服务文件
vim /usr/lib/systemd/system/redis.service
2 正式编写redis.service前先自己写一下在根目录中启动和关闭redis服务的命令,如下
[root@localhost ~]# /usr/local/bin/redis-server /usr/local/bin/wconfig/redis.conf
[root@localhost ~]# /usr/local/bin/redis-cli shutdown
3 正式编写redis.service
[Unit]
Description=Redis
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/bin/wconfig/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
User=root
Group=root
[Install]
WantedBy=multi-user.target
详情如下(正式配置文件中不要带注释,否则容易出错)
[Unit] #服务的说明
Description=Redis #服务的描述
After=network.target remote-fs.target nss-lookup.target #表示服务类别
[Service] #服务运行参数的设置
Type=forking #表示启动redis后是后台运行的形式
ExecStart=/usr/local/bin/redis-server /usr/local/bin/wconfig/redis.conf #是后台运行的形式(上面写的命令)
ExecStop=/usr/local/bin/redis-cli shutdown #关闭redis的命令(上面写的命令)
User=www #该命令作用的用户
Group=www #该命令作用的用户组
[Install]
WantedBy=multi-user.target #服务安装的相关设置,可设置为多用户
4 执行systemctl daemon-reload 命令使修改生效
systemctl daemon-reload
5 启动命令并查看进程,正常情况下会有相关进程
systemctl start redis.service
ps -ef | grep redis
补充
systemctl start redis.service:立刻启动后面接的 unit。
systemctl stop redis.service :立刻关闭后面接的 unit。
systemctl restart redis.service:立刻关闭后启动后面接的 unit,亦即执行 stop 再 start 的意思。
systemctl reload redis.service:不关闭 unit 的情况下,重新载入配置文件,让设置生效。
systemctl enable redis.service:设置下次开机时,后面接的 unit 会被启动。
systemctl disable redis.service:设置下次开机时,后面接的 unit 不会被启动。
systemctl status redis.service:目前后面接的这个 unit 的状态,会列出有没有正在执行、开机时是否启动等信息。
补充:其它nginx,tomcat,mysql服务的启动与此类似。