运行关机 shutdown 命令时。能不能自动关闭某些服务

2020-11-27 09:45:56 +08:00
 zunxiongchen
A 自定义了一个脚本在 /etc/init.d/test,在 A 运行 service test stop,就会 ssh 到 B 机器,shutdown 关机 B 机器。
有没有可能在 A 运行 shutdown 时自关也去跑 service test stop, 从而关闭 A 就是关闭 A+B
2305 次点击
所在节点    Linux
10 条回复
collery
2020-11-27 09:57:26 +08:00
可以
kyuuseiryuu
2020-11-27 09:58:59 +08:00
alias down=ssh userA:passA@hostA "service test stop" && shutdown
f6x
2020-11-27 10:02:06 +08:00
楼上方法好.
直接写进 init.d 里网络情况和结果异常都没法处理.
shily
2020-11-27 10:02:34 +08:00
Let me Google it for you

谷歌第一个链接是 https://opensource.com/life/16/11/running-commands-shutdown-linux 就提到了。

runlevel 3 就是关机
no1xsyzy
2020-11-27 12:58:46 +08:00
init.d 估计不行, #2 反转了控制,未必可行(比如 A 在内网?)
systemd 倒是可以保证在网络退出前先完成 stop
julyclyde
2020-11-27 19:14:04 +08:00
跨机器啊……这样做的话,B 那里感觉就是莫名其妙被关了
是不是不太好?
zunxiongchen
2020-12-01 11:56:13 +08:00
@kyuuseiryuu @f6x 你顾虑的对。我参考下
zunxiongchen
2020-12-01 11:57:10 +08:00
@julyclyde B 机器就是 DB server 而已。服务于 A 的 web 应用,A,B 想要同步关
zunxiongchen
2020-12-01 11:57:45 +08:00
@no1xsyzy 是。我用的 systemd
no1xsyzy
2020-12-01 13:10:11 +08:00
关闭时运行脚本 https://unix.stackexchange.com/a/41756
确保运行脚本前不把网络卸载 https://unix.stackexchange.com/a/294054

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/729734

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX