一、拿nginx来举例设置开机自启, 其他的服务都一样
1、添加开机自启文件
[root@bogon ~]# cat /lib/systemd/system/nginx.service
#!/bin/bash
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2、设置nginx服务开机自启
[root@bogon ~]# systemctl enable nginx
二、以redis为例
注意:首先开启持久化存储
1. 创建服务
vim /lib/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target
[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf
[Install]
WantedBy=multi-user.target
2. 创建软链接 # 其实这一步不用做
创建软链接是为了下一步系统初始化时自动启动服务
ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service
3. 刷新配置
systemctl daemon-reload
4. 启动、重启、停止
启动redis
systemctl start redis
重启redis
systemctl restart redis
停止redis
systemctl stop redis
5. 开机自启动
redis服务加入开机启动
systemctl enable redis
禁止开机启动
systemctl disable redis
6. 查看状态
查看状态
systemctl status redis