1、脚本内容

放置在/lib/systemd/system目录下,由于我这里启动的是服务端,我将该脚本文件命名为:frps.service,完整路径为:/lib/systemd/system/frps.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=frps service
After=network.target syslog.target
Wants=network.target
[Service]
Type=simple
Restart=always #当程序退出时,自动重启。
#最好使用非root用户启动
User=frp
Group=frp
#启动服务的命令(此处写你的frps的实际安装目录)
ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.ini
ExecStop=/bin/kill $MAINPID
[Install]
WantedBy=multi-user.target

2、systemctl命令控制

1
2
3
4
5
systemctl enable frps #设置开机自动启动
systemctl disable frps #取消开机自动启动
systemctl start frps #开启frps服务
systemctl stop frps #关闭frps服务
systemctl restart frps #重启frps服务

3、也可以自己写一个脚本,实现frp开机自动启动,或者断线重连

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
while true
do
result=`ps aux|grep frps.ini|grep -v grep|wc -l`
if [ $result = '0' ]; then
echo "未运行,开始运行"
nohup /usr/local/frp/frps -c /usr/local/frp/frps.ini > /dev/null 2>&1 &
else
echo "运行中"
fi
sleep 15
done

将上面脚本保存,我保存的位置为:/usr/bin/frpwatch

然后在 /etc/rc.local中添加运行命令:

1
nohup /usr/bin/frpwatch > /dev/null 2>&1 &