Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frp怎样开机启动和后台运行? #176

Closed
piiiiiiiiiiiiiii opened this issue Dec 14, 2016 · 13 comments
Closed

frp怎样开机启动和后台运行? #176

piiiiiiiiiiiiiii opened this issue Dec 14, 2016 · 13 comments

Comments

@piiiiiiiiiiiiiii
Copy link

piiiiiiiiiiiiiii commented Dec 14, 2016

两个问题:
1.在centos上如何开机运行?
2.怎样后台运行,而不用一直开着putty

谢谢


已经解决:

二更@20210519
现在下载的frp打包文件目录内,已经写好了后台运行启动文件,比如服务器端,只需要把frps.service复制一份到/etc/systemd/system目录下即可。

如果想自己手写,参照如下:

使用systemd配置开机自启,适用于 centos7 Ubuntu 16 或 debian 8。

vi /etc/systemd/system/frps.service 新建此文件,并写入以下内容
:
[Unit]
Description=frps daemon

[Service]
Type=simple
ExecStart=/usr/bin/frps -c /etc/frps/frps.ini

[Install]
WantedBy=multi-user.target

启动并设为开机自启。
$ systemctl start frps
$ systemctl enable frps

参照:lcbk.net/9766.html

------------centos6.5及以下---------------------
vi /etc/rc.local
在最下面加一行/usr/sbin/frp/frps -c /usr/sbin/frp/frps.ini
其中 /usr/sbin/frp是程序放置的目录,重启ok

@lnzoro
Copy link

lnzoro commented Dec 14, 2016

nohup 后台可以用

@wxyzh
Copy link

wxyzh commented Dec 14, 2016

supervisor
rc.local
systemd
nohup

@piiiiiiiiiiiiiii
Copy link
Author

楼上能详细说说吗? @lnzoro @wxyzh

@fatedier
Copy link
Owner

@impig33 建议这一类的问题可以通过搜索引擎自行解决。

@piiiiiiiiiiiiiii
Copy link
Author

piiiiiiiiiiiiiii commented Dec 14, 2016

二更@20210519
现在下载的frp打包文件目录内,已经写好了后台运行启动文件,比如服务器端,只需要把frps.service复制一份到/etc/systemd/system目录下即可。

如果想自己手写,参照如下:

已经解决:
使用systemd配置开机自启,适用于 centos7 Ubuntu 16 或 debian 8。

vi /etc/systemd/system/frps.service 新建此文件,并写入以下内容
:
[Unit]
Description=frps daemon

[Service]
Type=simple
ExecStart=/usr/bin/frps -c /etc/frps/frps.ini

[Install]
WantedBy=multi-user.target

启动并设为开机自启。
$ systemctl start frps
$ systemctl enable frps

参照:lcbk.net/9766.html

------------centos6.5及以下---------------------
vi /etc/rc.local
在最下面加一行/usr/sbin/frp/frps -c /usr/sbin/frp/frps.ini
其中 /usr/sbin/frp是程序放置的目录,重启ok

.

@piiiiiiiiiiiiiii
Copy link
Author

太好用了,使用特权模式服务端免配置,太好用了,谢谢作者。

@vc5
Copy link

vc5 commented Jun 2, 2017

贴一下我之前无法开机自启frpc的解决办法,原因可能是因为系统开机并未准备好网络,或者其它依赖

sudo vim /etc/systemd/system/frpc.service

按如下修改

[Unit]
Description=frpc daemon
After=syslog.target  network.target
Wants=network.target

[Service]
Type=simple
ExecStart=/usr/sbin/frp/frpc -c /etc/frp/frpc.ini
Restart= always
RestartSec=1min
ExecStop=/usr/bin/killall frpc


[Install]
WantedBy=multi-user.target

使用sudo systemctl enable frpc.service启用

@piiiiiiiiiiiiiii piiiiiiiiiiiiiii changed the title 怎样开机启动和后台运行? frp怎样开机启动和后台运行? Jun 2, 2017
@fatedier
Copy link
Owner

fatedier commented Jun 2, 2017

@vc5 👍

也可以试试 0.11.0 frpc 新增的 login_fail_exit 参数设置为 false,这样启动时没连上服务器就不会自动退出了,而是每隔30s自动重连。

@leke2015
Copy link

leke2015 commented Jul 8, 2017

http://free-e.net/109 这篇文章能帮助到您。

@the-eric-kwok
Copy link

the-eric-kwok commented Dec 7, 2017

If you don't have systemd or your kernel don't support systemd, this is my init.d service file:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          frpc
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: frpc service deamon
# Description:       frpc service daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/frpc
DAEMON_OPTS=-c\ /etc/frp/frpc.ini
NAME=frpc
USER=eric

set -e
. /lib/lsb/init-functions

start() {
        echo -n "Starting $NAME: "
        start-stop-daemon --start \
                --user $USER \
                -m --pidfile /var/run/$NAME.pid \
                -b --exec $DAEMON -- $DAEMON_OPTS || true
        echo "$NAME."
}

stop() {
        echo -n "Stopping $NAME: "
        start-stop-daemon --stop \
                --pidfile /var/run/$NAME.pid || true
        echo "$NAME."
}

status() {
        status_of_proc -p /var/run/$NAME.pid "$DAEMON" $NAME && exit 0 || exit $?
}

case "$1" in
start)
  start
        ;;
stop)
  stop
        ;;
status)
  status
        ;;
restart)
  stop
  sleep 2
  start
        ;;
*)
        echo "Usage: $NAME {start|stop|restart|status}"
        exit 1
        ;;
esac
exit 0

Hope this can help you.
Oh, by the way, you should put the binary into /usr/bin and put the config file into /etc/frp/

@ihipop
Copy link

ihipop commented Aug 24, 2018

This Version Allow run as nobody AND ports below 1024

/etc/systemd/system/frps.service

Systems that has AmbientCapabilities support (usually kernel >= linux4.3)

[Unit]
Description=FRP Server Daemon

[Service]
Type=simple
AmbientCapabilities=CAP_NET_BIND_SERVICE
ExecStart=/opt/bin/frps -c /opt/etc/frps.ini
Restart=always
RestartSec=2s
User=nobody
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

Systems that without AmbientCapabilities support,You need apt install libcap2-bin then

[Unit]
Description=FRP Server Daemon

[Service]
Type=simple
ExecStartPre=-/usr/sbin/setcap cap_net_bind_service=+ep /opt/bin/frps
ExecStart=/opt/bin/frps -c /opt/etc/frps.ini
Restart=always
RestartSec=2s
User=nobody
PermissionsStartOnly=true
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

/etc/systemd/system/frpc.service

[Unit]
Description=FRP Client Daemon
After=network.target
Wants=network.target

[Service]
Type=simple
ExecStart=/opt/bin/frpc -c /opt/etc/frpc.ini
Restart=always
RestartSec=20s
User=nobody
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

Config

systemctl daemon-reload
systemctl enable frpc
systemctl status frpc
systemctl enable frps
systemctl status frps

Also Archived at https://gist.github.com/ihipop/4dc607caef7c874209521b10d18e35af

@popy32
Copy link

popy32 commented Feb 13, 2019

centos 6.5 init.d script
centos 6.5 客户端环境下配置后台自启

inspire by
https://serverfault.com/a/869143
https://github.com/utobi/prometheus-rpm/blob/master/node_exporter/contrib/node_exporter.init

ensure binary in /usr/bin/frpc, and configure file in /etc/frp/frpc.ini
edit /etc/init.d/frpc and chkconfig add frpc

#!/bin/bash

# chkconfig: - 85 15
# # description: frp init script

RETVAL=0
PROG="frpc"
EXEC="/usr/bin/frpc"
LOCKFILE="/var/lock/subsys/$PROG"
OPTIONS="-c /etc/frp/frpc.ini"

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
else
  echo "/etc/rc.d/init.d/functions is not exists"
  exit 0
fi

start() {
  if [ -f $LOCKFILE ]
  then
    echo "$PROG is already running!"
  else
    echo -n "Starting $PROG: "
    #$EXEC $OPTIONS  &
    nohup $EXEC $OPTIONS >/dev/null 2>&1 &
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $LOCKFILE && success || failure
    echo
    return $RETVAL
  fi
}

stop() {
  echo -n "Stopping $PROG: "
  killproc $EXEC
  RETVAL=$?
  [ $RETVAL -eq 0 ] && rm -r $LOCKFILE && success || failure
  echo
}

restart ()
{
  stop
  sleep 1
  start
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status $PROG
    ;;
  restart)
    restart
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac
exit $RETVAL

in server with ubuntu 16.04, systemd is available
cat /etc/systemd/system/frps.service

[Unit]
Description=Frps Service
After=network.target
Wants=network.target

[Service]
Type=simple
PIDFile=/var/run/frps.pid
ExecStart=/usr/sbin/frp/frps -c /etc/frp/frps.ini
Restart=on-failure

[Install]
WantedBy=multi-user.target

@qiushile
Copy link

贴一下我之前无法开机自启frpc的解决办法,原因可能是因为系统开机并未准备好网络,或者其它依赖

sudo vim /etc/systemd/system/frpc.service

按如下修改

[Unit]
Description=frpc daemon
After=syslog.target  network.target
Wants=network.target

[Service]
Type=simple
ExecStart=/usr/sbin/frp/frpc -c /etc/frp/frpc.ini
Restart= always
RestartSec=1min
ExecStop=/usr/bin/killall frpc


[Install]
WantedBy=multi-user.target

使用sudo systemctl enable frpc.service启用

万分感谢!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants