Press "Enter" to skip to content

WSl下的Ubuntu配置服务

最近在尝试配置wsl玩,安装了一个ubuntu系统。

我在服务器上配置了一个frp的服务端,想做到开机启动,配置成一个服务。

下载下来的frp包里面有个systemd目录,里面有个frps@service,我单纯的以为我只要把这个玩意复制到/etc/systemd/system/frp@service下面就行了。

尝试复制过去,然后执行 sudo service frps start,报错frps: unrecognized service

ε=(´ο`*)))唉,网上一通搜,有说 systemctl start frps的,有说需要 systemctl daemonreload的。

我装的是wsl啊,没有这玩意。

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

查资料说,wsl不玩systemctl,这个玩不行,直接用service起服务。

然后一通资料查,好像搞明白一点了。

大概情况是这样的,systemctl 和 service 是两套玩法,以前早期玩的是 service来管理服务的,后来搞了个systemctl。

然后systemctl的服务脚本都在 /etc/systemd/system下面,那个service的脚本在/etc/init.d/下面。

那简单了,搞一个frps的 init.d的服务启动脚本就行了呗。

增加文件:/etc/init.d/frps

## File: /etc/init.d/frps
#!/bin/sh
#
# frps:    FRP-Server Daemon
#
# description:  FRP-Server Daemon
PID_FILE=/run/frps.pid
CONFIG_FILE=/etc/frp/frps.ini
FRP_SERVER=/usr/bin/frps
start()
{
    if [ ! -f $PID_FILE ]; then
        echo -n $"Starting FRP server..."
        nohup $FRP_SERVER -c $CONFIG_FILE < /dev/null > /dev/null 2> /dev/null &
        echo $! > $PID_FILE
        echo ""
    else
        PID=$(cat $PID_FILE)
        if [ ! -f /proc/$PID/cmdline ]; then

            echo -n $"Starting FRP server..."
            nohup $FRP_SERVER -c $CONFIG_FILE < /dev/null > /dev/null 2> /dev/null &
            echo $! > $PID_FILE
            echo ""
        else
            echo "FRP server is already running..."
        fi
    fi;
}
stop()
{
    if [ -f $PID_FILE ]; then
        echo -n $"Shutting down FRP server..."
        kill -9 $(cat $PID_FILE)
        rm -f $PID_FILE
        echo ""
    else
        echo "FRP server is not running..."
    fi;
}
status()
{
    if [ -f $PID_FILE ]; then
        PID=$(cat $PID_FILE)
        if [ -f /proc/$PID/cmdline ]; then
            echo "FRP server is running..."
        else
            echo "FRP server is not running..."
            rm -f $PID_FILE
        fi
    else
        echo "FRP server is not running..."
    fi;
}
[ -f $FRP_SERVER ] || exit 1
# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        stop
        sleep 3
        start
        ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac
exit 0

别忘了给这个文件执行权限

chmod a+x /etc/init.d/frps

然后在试下命令?

sudo service frps start

搞定!

参考资料:

https://gist.github.com/fenying/7684afbe24f20e07201fa790aec1511c

One Comment

  1. Molean
    Molean 2021年8月31日

    dalao nb!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注