检测待处理的 Linux 关闭
由于我会尽快为我的 Ubuntu 服务器安装挂起的更新,因此我必须经常重新启动我的 Linux 服务器。我正在该服务器上运行一个网络应用程序,并想警告我的用户有关待重启的信息。现在,我手动执行此操作,在重新启动之前添加公告,给他们一些时间来完成工作,重新启动并删除公告。
我希望,shutdown -r +60
写入一个包含有关重新启动的所有信息的文件,我可以在每次访问时检查该文件。有这样的文件吗?出于性能原因,更喜欢虚拟文件系统中的文件,例如 /proc...
我正在运行 Ubuntu 10.04.2 LTS
Since I install pending updates for my Ubuntu server as soon as possible, I have to restart my linux server quite often. I'm running an webapp on that server and would like to warn my users about the pending restart. Right now, I do this manually, adding an announcement before the restart, give them some time to finish their work, restart and remove the announcement.
I hope, shutdown -r +60
writes an file with all the information about the restart, which I can check on every access. Is there such a file? Would prefer a file in a virtual file system like /proc for performance reasons...
I'm running Ubuntu 10.04.2 LTS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用 systemd,以下命令将显示计划的关闭信息。
输出示例:
正如 @Björn 的评论中所述,
USEC
是以微秒为单位的时间戳。您可以将其转换为人类友好的格式,删除最后 6 位数字并使用
date
,如下所示:If you are using systemd, the following command shows the scheduled shutdown info.
Example of output:
As remarked in a comment by @Björn,
USEC
is the timestamp in micro seconds.You can convert it to a human friendly format dropping the last 6 figures and using
date
like this:我能想到的最简单的解决方案是编写一个脚本来包装关闭命令,并在该脚本中创建一个您的 Web 应用程序可以检查的文件。
据我所知,关闭不会将文件写入底层文件系统,尽管它确实会触发关闭的广播消息警告,我想您可以编写一个程序来拦截..但上述解决方案似乎是最简单的。
脚本示例:
然后在您的网络应用程序中检查“somefile”。
您需要添加一个删除“somefile”的启动链接,否则当系统启动时它仍然存在,并且网络应用程序将始终告诉您的用户它即将关闭。
The easiest solution I can envisage means writing a script to wrap the shutdown command, and in that script create a file that your web application can check for.
As far as I know, shutdown doesn't write a file to the underlying files system, although it does trigger broadcast messages warning of the shutdown, which I suppose you could write a program to intercept .. but the above solution seems the easiest.
Script example:
then check for 'somefile' in your web app.
You'd need to add a startup link that erased the 'somefile' otherwise it would still be there when the system comes up and the web app would always be telling your users it was about to shut down.
您可以简单地检查正在运行的关闭进程:
You can simply check for running shutdown process:
对于较新的 Linux 发行版,您可能需要执行以下操作:
busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown
关闭工作的方法已更改
尝试过在:
- Debian 延伸 9.6
- Ubuntu 18.04.1 LTS
参考
For newer linux distributions versions you might need to do:
busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown
The method of how shutdown works has changed
Tried on:
- Debian Stretch 9.6
- Ubuntu 18.04.1 LTS
References
您可以编写一个守护程序,在捕获 SIGINT / SIGQUIT 信号时执行公告。
You could write a daemon that does the announcement when it catches the SIGINT / SIGQUIT signal.