From 5f22b754593afdc7da24dacc5cf6133f07a990b7 Mon Sep 17 00:00:00 2001 From: Dfsaadsa Date: Fri, 19 May 2023 17:11:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cns.init | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 cns.init diff --git a/cns.init b/cns.init new file mode 100644 index 0000000..7c9101d --- /dev/null +++ b/cns.init @@ -0,0 +1,108 @@ +#!/bin/sh + +#chkconfig: 2345 99 99 +### BEGIN INIT INFO +# Provides: cns +# Required-Start: $all +# Required-Stop: $all +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Start cns daemon at boot time +# Description: Start cns daemon at boot time +### END INIT INFO +START=99 + +usage() +{ + if type systemctl && [ -z "$(systemctl --failed|grep -q 'Host is down')" ]; then + us="systemctl [start|stop|status|restart] cns.service" + else + if [ "$0" = '/etc/rc.common' ]; then + us="/etc/init.d/cns [start|stop|status|restart]"\ + else + us="$0 [start|stop|status|restart]" + fi + fi &>/dev/null + echo "Usage: + $us + Config file is [cns_install_dir]/cns.json" +} + +status() +{ + { + grep -q cns /proc/`cat "[cns_install_dir]/run.pid" 2>/dev/null`/comm 2>/dev/null && \ + echo "cns is running..." || \ + echo "cns is stopped..." + } 2>/dev/null +} + + +start() +{ + status | grep running && return 0 + echo -n "Starting cns:" + for server_port in [cns_tcp_port_list]; do + iptables -I INPUT -p 6 --dport $server_port -j ACCEPT + ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT + done 2>/dev/null + cd "[cns_install_dir]" + rm -f "[cns_install_dir]/run.pid" + ./cns -json=cns.json -daemon >/dev/null + count=0 + while [ ! -f "[cns_install_dir]/run.pid" ] && [ "$count" -le 10 ]; do + sleep 1 + count=$((count + 1)) + done + grep -q cns /proc/`cat [cns_install_dir]/run.pid 2>/dev/null`/comm && \ + echo -e "\033[60G[\033[32m OK \033[0m]" || \ + echo -e "\033[60G[\033[31mFAILED\033[0m]" +} + +stop() +{ + for server_port in [cns_tcp_port_list]; do + while iptables -D INPUT -p 6 --dport $server_port -j ACCEPT; do :;done + while ip6tables -D INPUT -p 6 --dport $server_port -j ACCEPT; do :;done + done 2>/dev/null + status | grep stopped && return 0 + echo -n "Stopping cns:" + kill `cat [cns_install_dir]/run.pid 2>/dev/null` 2>/dev/null + sleep 1 + grep -q cns /proc/`cat [cns_install_dir]/run.pid`/comm 2>/dev/null && \ + echo -e "\033[60G[\033[31mFAILED\033[0m]" || \ + echo -e "\033[60G[\033[32m OK \033[0m]" +} + +restart() +{ + stop + start +} + +systemdStart() +{ + status | grep running && return 0 + cd "[cns_install_dir]" + rm -f "[cns_install_dir]/run.pid" + for server_port in [cns_tcp_port_list]; do + iptables -I INPUT -p 6 --dport $server_port -j ACCEPT + ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT + done 2>/dev/null + ./cns -json=cns.json >/dev/null + count=0 + while [ ! -f "[cns_install_dir]/run.pid" ] && [ "$count" -le 10 ]; do + sleep 1 + count=$((count + 1)) + done +} + +systemdRestart() +{ + stop + systemdStart +} + +if [ -z "$initscript" ]; then + ${1:-usage} +fi