如何使用 Bash 检测 apt-get 是否需要重新启动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7039520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect whether apt-get requires a reboot using Bash?
提问by Roger
I am writing a bash script (for apt-get based OS's) that automates the installations process of various programs. In this process I run "apt-get -fy update" and "apt-get -fy upgrade" sometimes. In the process of upgrading, occasionally, a restart is required.
我正在编写一个 bash 脚本(用于基于 apt-get 的操作系统),它可以自动执行各种程序的安装过程。在这个过程中,我有时会运行“apt-get -fy update”和“apt-get -fy upgrade”。在升级过程中,偶尔需要重启。
My question: is there a way of testing if the system is asking for a restart after running "apt-get -fy upgrade"? I am trying to write the script for it to run from beginning to end without human any intervention.
我的问题:有没有办法测试系统在运行“apt-get -fy upgrade”后是否要求重新启动?我正在尝试编写脚本,让它在没有人为干预的情况下从头到尾运行。
Thank you.
谢谢你。
回答by Gcmalloc
Use the file /var/run/reboot-required which does exactly what you want. So we will have this:
使用文件 /var/run/reboot-required 完全符合您的要求。所以我们会有这个:
apt-get update && apt-get -fy upgrade && [ -f /var/run/reboot-required ] && shutdown -r now
回答by jw013
I don't recall whether apt-getactually gives you a predictably formatted message informing you whether a restart is necessary, but if it does you could just check the output, e.g. something like apt-get -fy update | grep -q 'fill in restart message pattern' && reboot.
我不记得是否apt-get真的给你一条可预测的格式化消息,通知你是否需要重启,但如果是这样,你可以检查输出,例如像apt-get -fy update | grep -q 'fill in restart message pattern' && reboot.
Another probably less reliable alternative is to use checkrestartfrom the debian-goodiespackage.
另一个可能不太可靠的替代方法是checkrestart从debian-goodies包中使用。
回答by Hassek
If you do a
如果你做一个
apt-get -fy update && shutdown -r now
it will respect the order and will update until finish and finally restart your server.
它将遵守顺序并更新直到完成并最终重新启动您的服务器。

