#!/bin/bash

function checkPort()
{
	ip_address=$1
	port=$2

	# 使用nc命令检测连接
	nc -zv -w 2 $ip_address $port

	# 检查nc命令的退出状态
	if [ $? -eq 0 ]; then
			result=true
	else
			result=false
	fi

	# 将结果赋值给变量
	is_active=$result

	# 打印最终结果

	# 根据连接状态输出不同的提示
	if [ "$is_active" = true ]; then
			echo 1
	else
			echo 0
	fi
}

res=$(checkPort $1 $2)
if [ "$res" -eq 1 ]; then
    echo "service on $1 $2  worked normally"
else
    echo "service  port:"$2 " is not actived, try to restart it"
    eval $3 #执行重启服务动作
fi

exit