wall 命令
基本介绍
wall(Write All)是 Linux 系统中用于向所有登录用户发送消息的命令。它可以向系统中所有登录用户的终端发送广播消息。wall是系统管理员向所有用户发送通知的重要工具。
资料合集:https://pan.quark.cn/s/6fe3007c3e95、https://pan.quark.cn/s/561de99256a5、https://pan.quark.cn/s/985f55b13d94、https://pan.quark.cn/s/d0fb20abd19a
语法
wall [OPTIONS] [MESSAGE]常用选项
基本选项
-n, --nobanner:不显示消息头部-t, --timeout=SECONDS:设置超时时间-h, --help:显示帮助信息-V, --version:显示版本信息
使用示例
1. 向所有用户发送消息
wall"System maintenance in 10 minutes"输出示例:
Broadcast message from root@hostname (pts/0) at 10:00 ... System maintenance in 10 minutes2. 从文件读取消息
wall<message.txt3. 不显示消息头部
wall-n"Emergency: System will reboot now!"输出示例:
Emergency: System will reboot now!4. 输入多行消息
wall Hello everyone!This is a reminder that we will be performing system maintenance tonight at10PM. Thank you, Admin结束输入:
按Ctrl+D结束消息输入。
5. 设置超时时间
wall-t5"Important announcement"6. 在脚本中使用 wall
#!/bin/bash# 发送维护通知send_maintenance_alert(){localmessage=$1echo"Sending maintenance alert to all users..."wall"$message"if[$?-eq0];thenecho"Alert sent successfully"elseecho"Failed to send alert"exit1fi}# 使用示例send_maintenance_alert"System maintenance scheduled for tonight at 10 PM."输出格式说明
带头部的消息
Broadcast message from root@hostname (pts/0) at 10:00 ... System maintenance in 10 minutes不带头部的消息
Emergency: System will reboot now!错误输出示例
wall: cannot send message工作原理
wall 执行流程
# 1. 用户执行 wall 命令wall"Maintenance alert"# 2. 读取消息内容# 3. 获取所有登录用户列表# 4. 向每个用户终端发送消息# 5. 输出发送结果相关文件
| 文件 | 说明 |
|---|---|
/var/run/utmp | 当前登录用户信息 |
/dev/tty* | 终端设备文件 |
实用技巧
发送定时维护通知
#!/bin/bash# 发送维护通知MESSAGE="=== SYSTEM MAINTENANCE NOTICE === Date:$(date+%Y-%m-%d)Time: 10:00 PM The system will be undergoing maintenance tonight. Please save your work and log out before 10 PM. Thank you, System Administration"echo"Sending maintenance notice..."wall"$MESSAGE"结合 cron 使用
# 添加到 cron 任务(每天 9 AM 发送提醒)# 编辑 cron 配置crontab-e# 添加以下内容09* * * wall"Good morning! Don't forget to check your emails."发送紧急消息
#!/bin/bash# 发送紧急消息send_emergency(){localmessage=$1# 发送紧急消息(不带头部,更醒目)wall-n"=== EMERGENCY ===$message"echo"Emergency message sent"}# 使用示例send_emergency"Network outage detected! Please save your work."检查消息是否发送成功
#!/bin/bashMESSAGE="Test message"# 发送消息并检查结果ifwall"$MESSAGE">/dev/null2>&1;thenecho"Message sent successfully"elseecho"Failed to send message"exit1fi相关命令
write:向单个用户发送消息mesg:控制终端消息权限talk:与其他用户进行对话users:显示当前登录用户who:显示当前登录用户w:显示当前登录用户及其活动
注意事项
- 权限要求:普通用户可以使用 wall,但某些系统可能限制
- 目标用户:消息发送给所有登录用户
- 消息权限:用户可以使用 mesg 禁止接收消息
- root 用户:root 用户可以向所有用户发送消息
- 结束输入:按
Ctrl+D结束消息输入 - 超时设置:可以使用 -t 选项设置超时时间
总结
wall是 Linux 系统中用于向所有登录用户发送广播消息的工具。它是系统管理员向所有用户发送通知的重要工具。wall可以发送单行消息、多行消息,也可以从文件读取消息内容。