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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:42:07  来源:igfitidea点击:

How to check if a server is running

linuxbashshellunixping

提问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

One good solution is to use MRTG (a simple graphing tool for *NIX) with ping-probe script. look it up on Google.

一个好的解决方案是将 MRTG(一种用于 *NIX 的简单图形工具)与 ping-probe 脚本一起使用。在谷歌上查找。

read thisfor start.

阅读本文作为开始。

Sample Graph:

示例图:

Sample Graph

示例图

回答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

卷曲

man curl

男人卷曲

You can use curland check the http_responsefor 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 :]
  • 200is the normal success http_response
  • 403is a redirect e.g. maybe to a login page so also accetable and most probably means the service runs correctly

  • -sSilent or quiet mode.

  • -LDefines the Location
  • -wIn which format you want to display the the response
    -> %{http_code}\nwe only want the http_code
  • -othe output file
    -> /dev/nullredirect any output to /dev/null so it isn't written to stdout or the checkvariable. 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_response
  • 403是一个重定向,例如可能是登录页面,所以也可以访问,很可能意味着服务运行正常

  • -s静音或安静模式。

  • -L定义位置
  • -w您希望以哪种格式显示响应
    ->%{http_code}\n我们只需要 http_code
  • -o输出文件
    ->/dev/null将任何输出重定向到 /dev/null,因此它不会写入 stdout 或check变量。通常你会在 http_response 之前获得完整的 html 源代码,所以你也必须保持沉默。


nc

数控

man nc

曼恩

While curlto me seems the best option for Webservices since it is really checking if the service's webpage works correctly,

虽然curl对我来说似乎是 Webservices 的最佳选择,因为它确实在检查服务的网页是否正常工作,

nccan 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 22for SSH.

这里的优势是例如1秒的时间卷曲可能需要更长的时间失败的设定超时,当然你也可以检查它们是不是像端口的网页服务22SSH

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]
  • -4force IPv4 (or -6for IPv6)
  • -dDo not attempt to read from stdin
  • -zOnly listen, don't send data
  • -wtimeout
    If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. (In this case ncwill exit 1 -> failure.)
  • (optional) -nIf you only use an IP: Do not do any DNS or service lookups on any specified addresses, hostnames or ports.

  • &> /dev/nullDon't print out any output of the command

  • HOST= [您的主机的 ip 或 dns-name]
  • PORT= [不是可选的端口]
  • -4强制 IPv4(或-6用于 IPv6)
  • -d不要尝试从标准输入读取
  • -z只听,不发送数据
  • -wtimeout
    如果连接和 stdin 空闲时间超过 timeout 秒,则连接将被静默关闭。(在这种情况下nc将退出 1 -> 失败。)
  • (可选)-n如果您只使用 IP:不要对任何指定的地址、主机名或端口进行任何 DNS 或服务查找。

  • &> /dev/null不要打印出命令的任何输出