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

相关推荐
憧憬成为web高手2 小时前
ACTF 12307复现
前端·bootstrap·html
2401_868534782 小时前
分析RTOS与Linux有什么区别
linux·运维·服务器
wordbaby3 小时前
Axios 上传大文件崩溃:鸿蒙 RNOH 下 XHR 返回空响应头引发的"假失败"
前端·react native
玛丽莲茼蒿3 小时前
Linux/Unix学习笔记(四)—— 进程管理
linux·学习·unix
wordbaby3 小时前
React Native 列表分页实战:下拉刷新与上拉加载的工程化方案
前端·react native
Peace3 小时前
【Nginx】
linux·运维·nginx
wordbaby4 小时前
脱离 Tab 栏的艺术:React Native 全屏子页面的导航架构实践
前端·react native·harmonyos
网络与设备以及操作系统学习使用者4 小时前
Linux与Windows核心差异深度解析
linux·运维·网络·windows·学习
陈随易4 小时前
Redis 8.8发布,一定要更新
前端·后端·程序员
wordbaby4 小时前
React Native 新架构落地鸿蒙:跨三端政务级应用的工程实践与深度复盘
前端·react native·harmonyos