onekey-udptools/chkconfig/service/udp2raw-server

95 lines
1.7 KiB
Bash

#!/bin/bash
#
# udp2raw startup script for the Udp2Raw Server
#
#
# chkconfig: 345 80 20
# description: start the udp2raw deamon
#
# Source function library
. /etc/rc.d/init.d/functions
prog=Udp2Raw-Server
#程序目录
HOME_DIR=/usr/local/udptools
#程序文件名
BIN_NAME=udp2raw_x86
#配置文件
CONFIG_FILE=$HOME_DIR/udp2raw-server.conf
#日志文件
LOG_FILE=$HOME_DIR/udp2raw-server.log
#Function
checkSet(){
#获取监听端口
SERVER_PORT=`cat $CONFIG_FILE | grep '\-l ' | awk -F ":" '{print $2}'`
#检查iptables规则
IPTALBES=`iptables -nvL | grep DROP | grep tcp | grep $SERVER_PORT`
if [ ! -n "$IPTALBES" ]; then
echo "Adding iptables rules."
#添加iptables规则
RULES=`$HOME_DIR/$BIN_NAME --conf-file $CONFIG_FILE -g | grep iptables |grep -v rule`
$RULES
fi
#赋权
setcap cap_net_raw+ep $HOME_DIR/$BIN_NAME
}
start(){
checkSet
#启动进程
sudo -u nobody -b $HOME_DIR/$BIN_NAME --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1
}
stop(){
#结束进程
PID=`ps aux|grep $BIN_NAME|grep $CONFIG_FILE|grep -v root|grep -v grep | awk '{print $2}'`
kill $PID >/dev/null 2>&1
}
status(){
PID=`ps aux|grep $BIN_NAME|grep $CONFIG_FILE|grep -v root|grep -v grep | awk '{print $2}'`
if [ ! -n "$PID" ]; then
echo "$prog is stopped."
else
echo "$prog is running. pid $PID"
fi
}
showLog(){
cat $LOG_FILE | tail -n 50
}
case "$1" in
start)
echo "Starting $prog..."
start
;;
stop)
echo "Stopping $prog..."
stop
;;
restart)
echo "Stopping $prog..."
stop
sleep 2
echo "Starting $prog..."
start
;;
status)
status
;;
log)
showLog
;;
*)
echo "Usage: $prog {start|stop|restart|status|log}"
;;
esac
exit 0