如何在 Debian 中关闭或停止后立即运行命令?
我正在使用一台装有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看
/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 calledhalt
. 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.