作者:剡亚军_191 | 来源:互联网 | 2023-07-16 14:34
通过编辑脚本来实现开机自启。
PS:Linux系统上已经安装了Redis,如未安装Redis可以参考这篇文章
Redis在Linux上的安装
首先,新建一个系统服务文件:
vi /etc/systemd/system/redis.service
![在这里插入图片描述](https://img5.php1.cn/3cdc5/92e2/42f/de533133bd52c5e8.png)
内容如下:
[Unit]
Description=redis-server
After=network.target[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/src/redis-6.2.6/redis.conf
PrivateTmp=true[Install]
WantedBy=multi-user.target
![在这里插入图片描述](https://img5.php1.cn/3cdc5/92e2/42f/d32a70ced258b3e3.png)
然后重载系统服务:
systemctl daemon-reload
![在这里插入图片描述](https://img5.php1.cn/3cdc5/92e2/42f/cc42ac2dee31681c.png)
然后我们可以使用下面的命令对Redis进行一系列操作:
systemctl start redis
systemctl stop redis
systemctl restart redis
systemctl status redis
![在这里插入图片描述](https://img5.php1.cn/3cdc5/92e2/42f/f81124cd147f5e35.png)
执行下面的命令,可以让redis开机自启:
systemctl enable redis
![在这里插入图片描述](https://img5.php1.cn/3cdc5/92e2/42f/0701020f7070fa2e.png)
开机后执行下面命令可以看到Redis开机自启
ps -ef | grep redis
![在这里插入图片描述](https://img5.php1.cn/3cdc5/92e2/42f/8d1e9ac0f54cc549.png)
使用以下命令可以停止Redis进程
kill -9 1961
![在这里插入图片描述](https://img5.php1.cn/3cdc5/92e2/42f/b1725c5ddf339d9c.png)
总结:这个方法适合有需要使用Redis频率高的场景,如果对系统性能有要求则不适用,因为后台运行需要消耗部分内存。