首先,找到redis-server,redis.conf位置,前者是可执行文件,后者不是。
[root@localhost ~]# which redis-server
/usr/local/bin/redis-server
[root@localhost redis-5.0.5]# sudo find / -name redis.conf
/home/cec6/redis-3.2.8/redis.conf
webdis相同
[root@localhost ~]# sudo find / -name webdis
/root/webdis-master/webdis[root@localhost webdis-master]# sudo find / -name webdis.json
/root/webdis-master/webdis.json
然后,开始写文件:
1. 创建一个新的Systemd服务文件。这通常是一个位于`/etc/systemd/system/`目录下的`.service`文件。
sudo vi /etc/systemd/system/redis.service
这里vi的常用操作有
i:插入
esc:退出插入
:+ wq + enter : 保存
内容如下:
[Unit]
Description=Redis In-Memory Data Store
After=network.target[Service]
ExecStart=/usr/local/bin/redis-server /path/to/your/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
User=redis
Group=redis
Restart=always[Install]
WantedBy=multi-user.target
注意ExecStart那里要写,redis-server的实际位置,还有redis.conf的实际位置
2. 对于Webdis,同上:
[Unit]
Description=Webdis web server for Redis
After=network.target[Service]
ExecStart=/path/to/webdis/webdis /path/to/webdis.json
Restart=always[Install]
WantedBy=multi-user.target
3. 一旦你创建了这些服务文件,你需要告诉Systemd关于这些新服务。你可以使用`
systemctl daemon-reload
命令来完成这个操作。
4. 现在你可以使用`systemctl enable`命令来设置这些服务开机自启,例如:
sudo systemctl enable redis
sudo systemctl enable webdis
5. 最后,你可以现在就启动这些服务,或者重启你的系统来测试这些服务是否会在系统启动时自动运行:
sudo systemctl start redis
sudo systemctl start webdis
最后可以通过
sudo reload
来重启,若为生产机器一定注意其他是否都已保存。