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

14 篇文章 0 订阅
9 篇文章 0 订阅

需求

  • C服务需要在A服务和B服务都启动成功后才能启动
服务编号服务名
服务Anginx.service
服务Bmashang.service
服务Credis.service

实验

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

文件内容如下:

[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(如果需要的话):
systemctl daemon-reload
systemctl start redis.service

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

systemctl enable redis.service

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

  • 12
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值