新增 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]

相關內容