CentOS 7 环境下ping加时间戳实时输出到文件
1.服务器环境
[root@lw ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
2.撰写脚本
[root@lw ~]# cat ping.test.sh
#(1)-i 1 间隔1s
#(2)awk '{ print $0"\t" strftime("%D_%H:%M:%S",systime()) } ' 时间戳标记
ping 8.8.8.8 -i 1 | awk '{ print $0"\t" strftime("%D_%H:%M:%S",systime()) } '
3.授予脚本执行权限
[root@lw ~]# chmod +x /root/ping.test.sh
4.运行脚本
[root@lw ~]# setsid unbuffer /root/ping.test.sh >> ping.test.txt
PS:(1)setsid 放后台运行命令;(2)unbuffer 实时输出命令 【 yum -y install expect】

本文介绍在CentOS7环境中,如何使用shell脚本实时ping目标地址并附带时间戳,将结果输出到文件中。具体步骤包括创建脚本、设置权限、后台运行及实时输出配置。
532

被折叠的 条评论
为什么被折叠?



