Linux C服务需要在A服务和B服务都启动成功后才能启动

需求

  • C服务需要在A服务和B服务都启动成功后才能启动
服务编号 服务名
服务A nginx.service
服务B mashang.service
服务C redis.service

实验

  • 如果您想要 redis.service 在 nginx.service 和 mashang.service 都成功启动后才能启动,那么需要在 redis.service 的服务单元文件中使用 Wants= 或 Requires= 以及 After= 指令来定义这些依赖关系。
  • 希望 redis.service 在两个其他服务都成功启动后才启动,应该使用 Requires= 而不是 Wants=,因为 Requires= 会确保所有列出的服务都被启动,并且如果其中任何一个服务启动失败,redis.service 也会被视为启动失败。
bash 复制代码
vim /etc/systemd/system/redis

文件内容如下:

ini 复制代码
[Unit]
Description=Redis data structure server
After=network.target nginx.service mashang.service
Requires=nginx.service mashang.service

[Service]
Type=simple
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=on-failure

[Install]
WantedBy=multi-user.target

解析:

  • After=network.target nginx.service mashang.service 确保 redis.service 在网络目标、nginx.service 和 mashang.service 都启动之后再启动。
  • Requires=nginx.service mashang.service 表示 redis.service 需要 nginx.service 和 mashang.service 也被启动,如果其中任何一个服务启动失败,那么 redis.service 也会被视为启动失败。
    完成编辑后,重新加载 systemd 的配置并启动 redis.service(如果需要的话):
bash 复制代码
systemctl daemon-reload
systemctl start redis.service

如果您还希望 redis.service 在系统启动时自动启动,并且确保它在 nginx.service 和 mashang.service 之后启动,您可以使用 enable 命令:

bash 复制代码
systemctl enable redis.service

这样,当系统启动时,nginx.service 和 mashang.service 将首先被启动(如果它们被设置为开机自启),随后是 redis.service。如果 nginx.service 或 mashang.service 中任何一个启动失败,那么 redis.service 也不会被启动。

相关推荐
鸡鸭扣43 分钟前
Docker:3、在VSCode上安装并运行python程序或JavaScript程序
运维·vscode·python·docker·容器·js
A ?Charis1 小时前
k8s-对接NFS存储
linux·服务器·kubernetes
天宇&嘘月2 小时前
web第三次作业
前端·javascript·css
小王不会写code2 小时前
axios
前端·javascript·axios
发呆的薇薇°3 小时前
vue3 配置@根路径
前端·vue.js
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
人工干智能4 小时前
科普:“Docker Desktop”和“Docker”以及“WSL”
运维·docker·容器
落笔画忧愁e4 小时前
FastGPT及大模型API(Docker)私有化部署指南
运维·docker·容器
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker