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

相关推荐
聆风吟º1 天前
CANN开源项目深度实践:基于amct-toolkit实现自动化模型量化与精度保障策略
运维·开源·自动化·cann
Coder个人博客1 天前
Linux6.19-ARM64 mm mmu子模块深入分析
大数据·linux·车载系统·系统架构·系统安全·鸿蒙系统
子兮曰1 天前
OpenClaw入门:从零开始搭建你的私有化AI助手
前端·架构·github
吴仰晖1 天前
使用github copliot chat的源码学习之Chromium Compositor
前端
1024小神1 天前
github发布pages的几种状态记录
前端
较劲男子汉1 天前
CANN Runtime零拷贝传输技术源码实战 彻底打通Host与Device的数据传输壁垒
运维·服务器·数据库·cann
Doro再努力1 天前
Vim 快速上手实操手册:从入门到生产环境实战
linux·编辑器·vim
wypywyp1 天前
8. ubuntu 虚拟机 linux 服务器 TCP/IP 概念辨析
linux·服务器·ubuntu
风流倜傥唐伯虎1 天前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Doro再努力1 天前
【Linux操作系统10】Makefile深度解析:从依赖推导到有效编译
android·linux·运维·服务器·编辑器·vim