简体   繁体   English

每5分钟检查一次连接并将结果写入LINUX中的文件(无ping)的脚本

[英]Script to check connection every 5 minutes and write result to file (without ping) in LINUX

I need to check my connection to a spesific port every 5 minutes, currently i can't use ping command, so i need other alternative to do this.I want to execute this command in shell script 我需要每5分钟检查一次与特定端口的连接,目前我无法使用ping命令,因此我需要其他替代方法来执行此操作。我想在Shell脚本中执行此命令

Can someone help me to show some example for this case? 有人可以帮我举一些例子吗?

port=80
ip=8.8.8.8
checkIntervalSecs=5
timeoutSecs=1
while true ; do
    if $(nc -z -v -w$timeoutSecs $ip $port &>/dev/null); then
         echo "Server is up!"
    else
         echo "Server is down!"
    fi
    sleep $checkIntervalSecs
done

This runs until you kill it. 直到您杀死它为止。 For an explanation of the nc command, it is basically taken from SO question @IporSircer suggested . 对于nc命令的解释,它基本上来自SO问题@IporSircer建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM