Shell常用脚本:Frp内网穿透服务启动、关闭、重启、查看状态

18 篇文章 3 订阅

使用

# 使用

# 启动
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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值