如何在 Debian 中关闭或停止后立即运行命令?

发布于 2024-11-03 14:42:56 字数 144 浏览 1 评论 0原文

我正在使用一台装有 Debian 的嵌入式计算机。我已经设法在启动之前运行一个命令并播放“铃声”来告诉它已准备好工作,例如尝试连接到服务。

问题是,当系统停止时,我需要敲响铃声(或运行任何命令/程序),以便安全地拔掉电源。是否有任何运行脚本在停止后立即运行?

I'm working with an embedded computer that has a Debian on it. I already manage to run a command just before it has booted and play the "bell" to tell that is ready to work, and for example try to connect to a service.

The problem is that I need to play the bell (or run any command/program) when the system is halted so is safe to un-plug the power. Is there any runscript that run just after halt?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-11-10 14:42:56

如果您查看 /etc/init.d,您将看到一个名为 halt 的脚本。我非常确定,当在 0 或 6 以外的运行级别中调用 /sbin/halt 时,它会调用 /sbin/shutdown 来运行此脚本(除非调用带有 -n 标志)。那么也许您可以将自己的挂钩添加到该脚本中?显然,这将是在最后一次停止之前,但之后没有任何运行,所以也许没关系。

另一种选择是利用这样一个事实:所有正在运行的进程都会发送一个 SIGTERM,然后(大约一秒后)发送一个 SIGKILL。因此,您可以编写一个简单的守护进程,让它一直待在那里,直到收到 SIGTERM,此时它会“ping”并终止。

If you have a look in /etc/init.d, you'll see a script called halt. I'm pretty certain that when /sbin/halt is called in a runlevel other than 0 or 6, it calls /sbin/shutdown, which runs this script (unless called with an -n flag). So maybe you could add your own hook into that script? Obviously, it would be before the final halt was called, but nothing runs after that, so maybe that's ok.

Another option would be to use the fact that all running processes get sent a SIGTERM followed (a second or so later) by a SIGKILL. So you could write a simple daemon that just sat there until given a SIGTERM, at which point it went "ping" and died.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文