添加 CentOS 6 和 7 关机时显示的消息

添加 CentOS 6 和 7 关机时显示的消息

当发出重新启动或关闭命令时,我看到:

[kbrandt@ny-kbrandt01: ~] sudo reboot
[sudo] password for kbrandt:

Broadcast message from [email protected]
    (/dev/pts/3) at 14:50 ...

The system is going down for reboot NOW!

我可以看到这个特定的字符串是二进制文件的一部分:

[kbrandt@ny-kbrandt01: ~] strings /sbin/shutdown | grep NOW
The system is going down for power off NOW!
The system is going down for halt NOW!
The system is going down for maintenance NOW!
The system is going down for reboot NOW!

但是有谁知道是否有办法可以在不修改二进制文件的情况下添加提醒消息以使我们的监控系统内的主机静音?

答案1

A非常小心阅读shutdown(8)手册页(即前几次我查看时没有找到任何内容)发现可以在命令行上提供自定义消息。

例如:

# shutdown -r +15 "We're rebooting for unicorns. Silence monitoring please."
Shutdown scheduled for Tue 2014-11-25 10:17:53 EST, use 'shutdown -c' to cancel.
# 
Broadcast message from root@saurok (Tue 2014-11-25 10:02:53 EST):

We're rebooting for unicorns. Silence monitoring please.
The system is going down for reboot at Tue 2014-11-25 10:17:53 EST!

值得注意的是,如果您的环境中有 EL7,如果您希望看到此消息,我建议您提前 1 分钟安排关机,而不是“立即”关机,因为根据我的经验,用户可能会在收到墙之前被注销(由于 systemd 关闭和启动系统的速度非常快)。

值得注意的是,如果您想要更快的重启,请在重启之前设置 kexec,以跳过服务器自我测试硬件的无聊的 1 到 15 分钟...

答案2

您可以创建一个写入墙的服务。该服务将启动并创建“锁定”文件,然后您将在重新启动或关闭时收到消息(对于 CentOS 7,过程可能有所不同,因为它使用 systemd):

脚本(可能会更好):

[root@ny-kbrandt01 init.d]# cat reminder
#!/bin/bash
# chkconfig: 2345 99 01
# description: My test service

if [[ $1 == "start" ]]; then
        touch /var/lock/subsys/reminder
fi

if [[ $1 == "stop" || $1 == "halt" ]]; then
        wall "Please silence in bosun so Kyle doesn't turn into more of a nutbag"
fi

并确保使用 chkconfig 添加它:

[root@ny-kbrandt01 init.d]# chkconfig --add reminder

问题是这个版本的扩展性不佳,因为它具有“自动静音”功能,因为我们不想对非管理员启动的重启执行此操作。

答案3

man shutdown

NAME
   shutdown - bring the system down

SYNOPSIS
   shutdown [OPTION]...  TIME [MESSAGE]

相关内容