Simple service script for alpine

Alpine Linux rocks. However, I often use software that is not provided in the alpine repositories, such as frp and miniserve, which means I need to maintain these services manually. Previously I had been doing this by creating a new tmux session and running the program in it. Eventually, I felt I needed to try writing openrc service scripts for these software. Fortunately, I found a template that works well.

Taking the frp service as an example, I created a service script frpc-0 under /etc/init.d/ with the following content.

#!/sbin/openrc-run

pidfile="/run/$RC_SVCNAME.pid"
command="/root/frpc" # absolute path to the executable 
command_args="-c /root/frpc.ini"

depend() {
   need net
}

start() {
   ebegin "Starting frpc-0"
   start-stop-daemon --start --background \
        --exec $command \
        --make-pidfile --pidfile $pidfile \
        -- $command_args
   eend $?
}

stop() {
   ebegin "Stopping frpc-0"
   start-stop-daemon --stop \
        --exec $command \
        --pidfile $pidfile
   eend $?
}

reload() {
    ebegin "Reloading frpc-0"
    start-stop-daemon --stop \
        --exec $command \
        --pidfile $pidfile
    start-stop-daemon --start --background \
        --exec $command \
        --make-pidfile --pidfile $pidfile \
        -- $command_args
    eend $?
}

then,

# chmod 755 /etc/init.d/frpc-0
# rc-service frpc-0 start
# rc-update add frpc-0 default 

reference

https://blog.csdn.net/lbp0408/article/details/80473404

Oct 30, 2021

HOME