热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

“nc-z”的CentOS7替代方案-AlternativeinCentOS7for“nc-z”

Ihaveabashprogramthatchecksthatadaemoninagivenportisworking:我有一个bash程序,它检查给定端口中的守护进程

I have a bash program that checks that a daemon in a given port is working:

我有一个bash程序,它检查给定端口中的守护进程是否正常工作:

nc -z localhost $port > /dev/null
if [ "$?" != "0" ]
then
  echo The server on port $port is not working
  exit
fi

This program works perfectly in CentOS 6. However, it seems that CentOS 7 has changed the underlying implementation for nc command (CentOS 6 seems to use Netcat and CentOS 7 uses another thing called Ncat) and now the -z switch doesn't work:

这个程序在CentOS 6中非常好用。然而,CentOS 7似乎已经改变了nc命令的底层实现(CentOS 6似乎使用Netcat和CentOS 7使用另一种叫做Ncat的东西),而现在-z开关不起作用:

$ nc -z localhost 8080
nc: invalid option -- 'z'

Looking to the man nc page in CentOS 7 I don't see any clear alternative to -z. Any suggestion on how I should fix my bash program to make it work in CentOS 7?

在CentOS 7的男子nc页面上,我没有看到任何明显的替代-z。关于如何修改bash程序以使其在CentOS 7中工作的任何建议?

3 个解决方案

#1


2  

And a trimmed down version for what you really want:

你真正想要的是一个精简版:

#!/bin/bash
# Start command: nohup ./check_server.sh 2>&1 &

check_server(){ # Start shell function

checkHTTPcode=$(curl -sLf -m 2 -w "%{http_code}\n" "http://10.10.10.10:8080/" -o /dev/null)

if [ $checkHTTPcode -ne 200 ]
    then
        # Check failed. Do something here and take any corrective measure here if nedded like restarting server
        # /sbin/service httpd restart >> /var/log/check_server.log
        echo "$(date) Check Failed " >> /var/log/check_server.log

    else
        # Everything's OK. Lets move on.
        echo "$(date) Check OK " >> /var/log/check_server.log
fi
}

while true # infinite check
do
# Call function every 30 seconds
check_server
sleep 30

done

#2


1  

according this post, I used:

根据这篇文章,我使用了:

nc -w1 localhost $port 

( this work on debian too, but it's slower than nc -z )

(这种方法也适用于debian,但它比nc -z要慢)

#3


0  

I solved something similar with the following code:

我用下面的代码解决了类似的问题:

# Start command: nohup ./check_server.sh 2>&1 &
# 
FREQUENCY_CHECK=30
ENDPOINT=10.11.12.13
PORT=8080
# Avoid spamming yourself
EMAIL_LIMIT=3


check_server(){ # Start shell function

checkHTTPcode=$(curl -sLf -m 2 -w "%{http_code}\n" "http://$ENDPOINT:$PORT/" -o /dev/null)


if [ $checkHTTPcode -ne 200 ]

    then
        EMAIL_LIMIT=$((EMAIL_LIMIT-1))

        # Let's log. Life's boring
        echo "$(date) Check Failed " >> /var/log/check_server.log

    if [ $EMAIL_LIMIT -eq 0 ]
        then

        # Send last email..
        printf "%s\nCheck failed \n\n Last email to be sent. " | mail -s "Check Failed!" -S smtp=your.smtp.domain.here.com -S from="from@yourdomain.com" destination.email@yourdomain.com

        else

            if [ $EMAIL_LIMIT -ge 0 ]
                then
                # Send notification
                printf "%s\nCheck failed \n\n Notifications left: $EMAIL_LIMIT " | mail -s "Check Failed!" -S smtp=your.smtp.domain.here.com -S from="from@yourdomain.com" destination.email@yourdomain.com
            fi 

    fi

        # Take any corrective measure here if nedded.
        /sbin/service httpd restart >> /var/log/check_server.log

else
    # Let's log. Life's boring
    echo "$(date) Check OK " >> /var/log/check_server.log
fi
}


while true # infinite check
do
# Call function every 30 seconds
check_server
sleep $FREQUENCY_CHECK
done

推荐阅读
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • Hadoop2.6.0 + 云centos +伪分布式只谈部署
    3.0.3玩不好,现将2.6.0tar.gz上传到usr,chmod-Rhadoop:hadophadoop-2.6.0,rm掉3.0.32.在etcp ... [详细]
  • asp.net core 应用docke部署到centos7
    前言前期准备win10(不要安装hyper-V)VMware-Workstation-Pro15.0Xshell6(非必需)VS2019以上环境请自行安装都是默认安装没什么可说的不 ... [详细]
  • centos7+cdh5.10.0搭建
    一、选择环境:1.说明本次部署使用台机器,3台用于搭建CDH集群,1台为内部源。内部源机器是可以连接公网的,可以提前部署好内部源,本次部署涉及到的服务器的hosts配置如下:192.168 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
  • 本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。 ... [详细]
  • 本文介绍了在RHEL 7中的系统日志管理和网络管理。系统日志管理包括rsyslog和systemd-journal两种日志服务,分别介绍了它们的特点、配置文件和日志查询方式。网络管理主要介绍了使用nmcli命令查看和配置网络接口的方法,包括查看网卡信息、添加、修改和删除配置文件等操作。 ... [详细]
  • 这里我们采用两台Nginx服务器作为前端,一主一从,Keepalived实现状态监测,保证Nginx正常对外提供服务,即主Nginx服务进程死掉之后,keepalived能够通过其自身的检测机制 ... [详细]
  • 之前有一篇文章介绍过在ubuntu下安装selenium和firefox现在介绍下centos7注意以下都是下载的linux64位的软件,32位的请自己找下链接,现在使用的python的版 ... [详细]
  • centos7.2下编译安装php7.1.3步骤教程centos7编译安装php-7.1.3的步骤在官网下载php-7.1.3的php压缩包,centos7安装php-7.1.3的 ... [详细]
  • 1、创建数据库[root@controller~]#mysql-uroot-p-eCREATEDATABASEkeystone;Enterpassword:[root ... [详细]
author-avatar
八卦男1002_426
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有