CentOS系统消息部署指南
在CentOS系统中,“message部署”可根据需求分为即时消息推送、系统日志配置、桌面通知设置及消息队列(MQ)搭建四大类,以下是具体方法:
echo "这是一条系统广播消息" | wallwrite username "这是给你的私信"mesg y;禁止接收:mesg ncurl -X POST -H "Content-Type: application/json" -d '{"msg":"测试消息"}' https://your-webhook-urllibnotify和d-bus)。sudo yum install libnotify d-busnotify-send "标题" "这是一条桌面通知"sudo yum install pushover-cliecho "测试消息" | pushover -t YOUR_API_TOKENsudo yum install telegram-sendtelegram-send -t YOUR_BOT_TOKEN "Hello from CentOS"sudo vi /etc/rsyslog.conf*.* /var/log/messages
auth,authpriv.* /var/log/secure
重启服务生效:sudo systemctl restart rsyslogsudo yum install dunstsudo systemctl start dunst && sudo systemctl enable dunst~/.config/dunst/dunstrc(如调整通知超时时间timeout = 5000)以RabbitMQ(常用消息队列中间件)为例,步骤如下:
sudo yum install -y erlang/etc/yum.repos.d/rabbitmq.repo,内容如下:[rabbitmq-server]
name=RabbitMQ repository for CentOS/$basearch
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/$releasever/$basearch
gpgcheck=1
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
enabled=1
或使用官方YUM源:sudo yum install -y https://github.com/rabbitmq/erlang-rpm/releases/download/v23.3.4.11/erlang-23.3.4.11-1.el7.x86_64.rpmsudo yum install -y https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/x86_64/rabbitmq-server-3.8.24-1.el7.noarch.rpmsudo yum install -y rabbitmq-serversudo systemctl start rabbitmq-serversudo systemctl enable rabbitmq-serversudo rabbitmq-plugins enable rabbitmq_managementhttp://<服务器IP>:15672,默认用户名/密码均为guest(仅限本地访问)rabbitmqadmin工具(需先安装)。my_queue队列):echo "Hello, RabbitMQ!" | sudo rabbitmqadmin publish exchange=amq.default routing_key=my_queue
sudo rabbitmqadmin get queue=my_queueimport pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='my_queue', durable=True)
channel.basic_publish(exchange='', routing_key='my_queue', body='Hello from Python!', properties=pika.BasicProperties(delivery_mode=2)) # 消息持久化
print(" [x] Sent 'Hello from Python!'")
connection.close()
消费者(接收消息):import pika
def callback(ch, method, properties, body):
print(f" [x] Received {body}")
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='my_queue', durable=True)
channel.basic_consume(queue='my_queue', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
以上方法覆盖了CentOS系统常见的消息部署需求,可根据实际场景选择合适的方式(如即时通知用wall/notify-send,长期日志用rsyslog,异步通信用RabbitMQ)。