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 也不会被启动。

相关推荐
未来龙皇小蓝3 分钟前
RBAC前端架构-01:项目初始化
前端·架构
威迪斯特8 分钟前
CentOS图形化操作界面:理论解析与实践指南
linux·运维·centos·组件·图形化·桌面·xserver
程序员agions12 分钟前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发12 分钟前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
一方热衷.14 分钟前
在线安装对应版本NVIDIA驱动
linux·运维·服务器
独自归家的兔17 分钟前
ubuntu系统安装dbswitch教程 - 备份本地数据到远程服务器
linux·运维·ubuntu
程序员猫哥_19 分钟前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
ONE_SIX_MIX19 分钟前
ubuntu 24.04 用rdp连接,桌面黑屏问题,解决
linux·运维·ubuntu
龙飞0520 分钟前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
*小海豚*21 分钟前
在linux服务器上DNS正常,但是java应用调用第三方解析域名报错
java·linux·服务器