Linux 如何检查服务器是否正在运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15980247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a server is running
提问by David542
I want to use ping to check to see if a server is up. How would I do the following:
我想使用 ping 来检查服务器是否已启动。我将如何执行以下操作:
ping $URL
if [$? -eq 0]; then
echo "server live"
else
echo "server down"
fi
How would I accomplish the above? Also, how would I make it such that it returns 0 upon the first ping response, or returns an error if the first ten pings fail? Or, would there be a better way to accomplish what I am trying to do above?
我将如何完成上述操作?另外,我将如何使它在第一个 ping 响应时返回 0,或者如果前十个 ping 失败则返回错误?或者,是否有更好的方法来完成我在上面尝试做的事情?
回答by devang
You can use something like this -
你可以使用这样的东西 -
serverResponse=`wget --server-response --max-redirect=0 ${URL} 2>&1`
if [[ $serverResponse == *"Connection refused"* ]]
then
echo "Unable to reach given URL"
exit 1
fi
回答by A human being
Use the -c option with ping, it'll ping the URL only given number of times or until timeout
将 -c 选项与 ping 一起使用,它只会 ping URL 给定的次数或直到超时
if ping -c 10 $URL; then
echo "server live"
else
echo "server down"
fi
回答by hudolejev
Short form:
简写:
ping -c5 $SERVER || echo 'Server down'
Do you need it for some other script? Or are trying to hack some simple monitoring tool? In this case, you may want to take a look at Pingdom: https://www.pingdom.com/.
其他脚本需要它吗?或者试图破解一些简单的监控工具?在这种情况下,您可能需要查看 Pingdom:https: //www.pingdom.com/。
回答by voji
I using the following script function to check servers are online or not. It's useful when you want to check multiple servers. The function hide the ping output, and you can handle separately the server live or server down case.
我使用以下脚本函数来检查服务器是否在线。当您要检查多个服务器时,它很有用。该功能隐藏 ping 输出,您可以单独处理服务器在线或服务器关闭的情况。
#!/bin/bash
#retry count of ping request
RETRYCOUNT=1;
#pingServer: implement ping server functionality.
#Param1: server hostname to ping
function pingServer {
#echo Checking server:
ping -c $RETRYCOUNT > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo down
else
echo live
fi
}
#usage example, pinging some host
pingServer google.com
pingServer server1
回答by kevoroid
回答by derHugo
I'ld recommend not to use only ping
. It can check if a server is online in general but you can not check a specific service on that server.
我建议不要只使用ping
. 它通常可以检查服务器是否在线,但您无法检查该服务器上的特定服务。
Better use these alternatives:
更好地使用这些替代方案:
curl
卷曲
You can use curl
and check the http_response
for a webservice like this
您可以使用curl
并检查http_response
这样的网络服务
check=$(curl -s -w "%{http_code}\n" -L "${HOST}${PORT}/" -o /dev/null)
if [[ $check == 200 || $check == 403 ]]
then
# Service is online
echo "Service is online"
exit 0
else
# Service is offline or not working correctly
echo "Service is offline or not working correctly"
exit 1
fi
where
在哪里
HOST
= [ip or dns-name of your host]- (optional )
PORT
= [optional a port; don't forget to start with:
] 200
is the normal success http_response403
is a redirect e.g. maybe to a login page so also accetable and most probably means the service runs correctly-s
Silent or quiet mode.-L
Defines the Location-w
In which format you want to display the the response
->%{http_code}\n
we only want the http_code-o
the output file
->/dev/null
redirect any output to /dev/null so it isn't written to stdout or thecheck
variable. Usually you would get the complete html source code before the http_response so you have to silence this, too.
HOST
= [您的主机的 ip 或 dns-name]- (可选)
PORT
= [可选端口;不要忘记开始:
] 200
是正常的成功 http_response403
是一个重定向,例如可能是登录页面,所以也可以访问,很可能意味着服务运行正常-s
静音或安静模式。-L
定义位置-w
您希望以哪种格式显示响应
->%{http_code}\n
我们只需要 http_code-o
输出文件
->/dev/null
将任何输出重定向到 /dev/null,因此它不会写入 stdout 或check
变量。通常你会在 http_response 之前获得完整的 html 源代码,所以你也必须保持沉默。
nc
数控
While curl
to me seems the best option for Webservices since it is really checking if the service's webpage works correctly,
虽然curl
对我来说似乎是 Webservices 的最佳选择,因为它确实在检查服务的网页是否正常工作,
nc
can be used to rapidly check only if a specific port on the target is reachable (and assume this also applies to the service).
nc
仅可用于快速检查目标上的特定端口是否可访问(并假设这也适用于服务)。
Advantage here is the settable timeout of e.g. 1 second while curl might take a bit longer to fail, and of course you can check also services which are not a webpage like port 22
for SSH
.
这里的优势是例如1秒的时间卷曲可能需要更长的时间失败的设定超时,当然你也可以检查它们是不是像端口的网页服务22
的SSH
。
nc -4 -d -z -w 1 &{HOST} ${PORT} &> /dev/null
if [[ $? == 0 ]]
then
# Port is reached
echo "Service is online!"
exit 0
else
# Port is unreachable
echo "Service is offline!"
exit 1
fi
where
在哪里
HOST
= [ip or dns-name of your host]PORT
= [NOT optionalthe port]-4
force IPv4 (or-6
for IPv6)-d
Do not attempt to read from stdin-z
Only listen, don't send data-w
timeout
If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. (In this casenc
will exit 1 -> failure.)(optional)
-n
If you only use an IP: Do not do any DNS or service lookups on any specified addresses, hostnames or ports.&> /dev/null
Don't print out any output of the command
HOST
= [您的主机的 ip 或 dns-name]PORT
= [不是可选的端口]-4
强制 IPv4(或-6
用于 IPv6)-d
不要尝试从标准输入读取-z
只听,不发送数据-w
timeout
如果连接和 stdin 空闲时间超过 timeout 秒,则连接将被静默关闭。(在这种情况下nc
将退出 1 -> 失败。)(可选)
-n
如果您只使用 IP:不要对任何指定的地址、主机名或端口进行任何 DNS 或服务查找。&> /dev/null
不要打印出命令的任何输出