使用

# 使用

# 启动
sh frps.sh start
sh frps.sh stop
sh frps.sh restart
sh frps.sh status


frps.sh脚本 == frps.sh脚本作为服务进行运行



# 将frps.sh脚本作为服务进行运行
# 1. frps.sh的头3行注释添加下面3行
#!/bin/bash
#
#description: 内网穿透服务工具
#chkconfig:2345 99 99

#将frps.sh 移动到  /etc/init.d
cp frps脚本文件   /etc/init.d/
cd /etc/init.d/
#脚本后缀取消掉,好看点
mv frps.sh frps
#修改脚本权限
chmod 777 frps
#将frps脚本文件作为服务
chkconfig --add frps
#查看所有服务
chkconfig --list
#服务运行
service frps 脚本内的几个参数

frps.sh

#!/bin/bash

if [ ! $1 ]; then
    echo '缺乏参数:restart|start|stop|status 中的任何一个'
    exit 1
fi

getFrpPID () {
  frpPID=$(netstat -nlp | grep frp | awk '{print $7}' | awk -F "/" '{print $1}' | awk '!a[$0]++{print $0}')
  return $frpPID
}

# 停止Frp进程
stopFrp () {
    frpPID=$(netstat -nlp | grep frp | awk '{print $7}' | awk -F "/" '{print $1}' | awk '!a[$0]++{print $0}')
    if [ ! "$frpPID" ]; then
        echo '当前无Frp内网穿透程序运行'
    else
        kill -9 "$frpPID"
        echo "关闭Frp内网穿透进程【$frpPID】"
    fi
}

# 启动Frp进程
startFrp () {
    if [ ! -d "/www/server/frp/frp_0.38.0_linux_386" ]; then
      echo '缺乏目录:/www/server/frp/frp_0.38.0_linux_386'
      exit 1
    fi
    frpPID=$(netstat -nlp | grep frp | awk '{print $7}' | awk -F "/" '{print $1}' | awk '!a[$0]++{print $0}')
    if [ !  $frpPID ]; then
   	 # 进入frp目录
	 cd /www/server/frp/frp_0.38.0_linux_386
	 # 守护进程运行frp
	 nohup ./frps -c ./frps.ini > nohup.out 2>&1 &
         echo '=====启动中,耐心等待====='
    	 sleep 4s
	 statusFrp
    else
         echo "当前已存有Frp【$frpPID】内网穿透进程,如果想重启请使用restart参数"
    fi
}

statusFrp () {
    frpPID=$(netstat -nlp | grep frp | awk '{print $7}' | awk -F "/" '{print $1}' | awk '!a[$0]++{print $0}')
    if [ ! $frpPID ]; then
        echo '当前无Frp内网穿透程序运行'
    else
        echo "Frp内网穿透程序正在进行中:进程PID【$frpPID】"
    fi
}


case $1 in
start)
    startFrp
    ;;
stop)
    stopFrp
    ;;
restart)
    stopFrp
    startFrp
    ;;
status)
    statusFrp
    ;;
*)
    echo '参数错误:请输入参数restart|start|stop|status 中的任何一个'
esac

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐