Cloudflare Tunnel, /etc/init.d/cloudflared not doing anything

Hi all,
I have followed this post by @tmomas however found that its not working as expected.

The option /etc/init.d/cloudflared start do not starting tunnel (tried on 2 devices and different tunnels).

Option /etc/init.d/cloudflared status return active with no instances

Because of that, the tunnel is not starting and you cannot go to next poing of migration.

To do that you need to run tunnel using cloudflared tunnel run but this command is not working as a service.

There is nothing in logs indicating why /etc/init.d/cloudflared not starting tunnel.


Second part is adding elements to sysctl -w to /etc/init.d/cloudflared.

The better approach is described in a comment to this post:

1. Create a file with this name: 99-fix-buffer-and-ping.conf in the folder: /etc/sysctl.d/
2. Add this inside and save the file.:
net.core.rmem_max=2500000
net.ipv4.ping_group_range=0 2147483647
3. Execute this command from SSH:sysctl -p /etc/sysctl.d/99-fix-buffer-and-ping.conf

as this sorting issue for generic cloudflared run.


Can somebody investigate why /etc/init.d/cloudflared not doing anything?

Checked issue on 22.03.5 and 22.03.0

I found temporary solution by reinstalling cloudflared service.

/etc/init.d/cloudflared stop
mv /etc/init.d/cloudflared /etc/init.d/cloudflared.old
cloudflared service install
/etc/init.d/cloudflared start

The problem is that when its started its working in background but you cannot use /etc/init.d/cloudflared stop to stop it, as installed file is not compatible with OpenWrt as it come from Red Hat.

To stop it you need to kill it manually kill -9 <PID>.


But, I found out the issue.

the ps command in OpenWrt do not have same funcionality as in other systems, so in /etc/init.d/cloudflared I replaced the following:

is_running() {
    [ -f "$pid_file" ] && ps $(get_pid) > /dev/null 2>&1
}

to

is_running() {
    [ -f "$pid_file" ] && ps | grep $(get_pid) | grep -v grep > /dev/null 2>&1
}

An stop, status and restart started working.

Usage: /etc/init.d/cloudflared {start|stop|restart|status}

Hope that help in further development.

I have tried various init files. I've tried the init file from the wiki as well as those found on various blogs and none of them worked well.
I wrote this simple init file but it works great. Hope this helps someone

#!/bin/sh /etc/rc.common

START=99
STOP=90

config_file="/etc/cloudflared/config.yml"
stdout_log="/var/log/cloudflared.log"
stderr_log="/var/log/cloudflared.err"

start() {
    echo "Starting cloudflared server..."
    
    /usr/bin/cloudflared --autoupdate-freq 24h0m0s --config $config_file tunnel run >$stdout_log 2>$stderr_log &
    
}

stop() {
    echo "Stopping cloudflared..."
    killall cloudflared
}

restart() {
    stop
    start
}

My init script doesn't look good, and I tried to rewrite it with a procd:

#!/bin/sh /etc/rc.common

START=50
USE_PROCD=1

start_service() {
    procd_open_instance
    procd_set_param command /usr/bin/cloudflared --config=/etc/cloudflared/config.yml tunnel run
    procd_set_param respawn
    procd_set_param stdout 1
    procd_set_param keep 1
    procd_close_instance
}

stop_service() {
    procd_send_signal /usr/bin/cloudflared 15
    procd_wait_for_service_stopped /usr/bin/cloudflared
}

reload_service() {
    procd_send_signal /usr/bin/cloudflared 1
}

boot() {
    start_service
}

shutdown() {
    stop_service
}

looks good, but I probably made a mistake and after starting I'm stuck with error:

daemon.info procd: Instance cloudflared::instance1 s in a crash loop 6 crashes, 0 seconds since last crash

Any ideas?

I investigated the problem and realized that the problem is in these parameters.

    procd_set_param respawn
    procd_set_param keep 1

Removed them and the service works.

Here is my /etc/init.d/cloudflared script modified for OpenWrt from original CloudFlare source made for RedHat.

#!/bin/sh
# For RedHat and cousins:
# chkconfig: 2345 99 01
# description: cloudflared
# processname: /usr/bin/cloudflared
### BEGIN INIT INFO
# Provides:          /usr/bin/cloudflared
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: cloudflared
# Description:       cloudflared agent
### END INIT INFO
name=$(basename $(readlink -f $0))
cmd="/usr/bin/cloudflared --pidfile /var/run/$name.pid --autoupdate-freq 24h0m0s --config /etc/cloudflared/config.yml tunnel run"
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
[ -e /etc/sysconfig/$name ] && . /etc/sysconfig/$name
get_pid() {
    cat "$pid_file"
}
is_running() {
    [ -f "$pid_file" ] && ps | grep $(get_pid) | grep -v grep > /dev/null 2>&1
}
case "$1" in
    start)
        if is_running; then
            echo "Already started"
        else
            echo "Starting $name"
            $cmd >> "$stdout_log" 2>> "$stderr_log" &
            echo $! > "$pid_file"
        fi
    ;;
    stop)
        if is_running; then
            echo -n "Stopping $name.."
            kill $(get_pid)
            for i in {1..10}
            do
                if ! is_running; then
                    break
                fi
                echo -n "."
                sleep 1
            done
            echo
            if is_running; then
                echo "Not stopped; may still be shutting down or shutdown may have failed"
                exit 1
            else
                echo "Stopped"
            $cmd >> "$stdout_log" 2>> "$stderr_log" &
            echo $! > "$pid_file"
        fi
    ;;
    stop)
        if is_running; then
            echo -n "Stopping $name.."
            kill $(get_pid)
            for i in {1..10}
            do
                if ! is_running; then
                    break
                fi
                echo -n "."
                sleep 1
            done
            echo
            if is_running; then
                echo "Not stopped; may still be shutting down or shutdown may have failed"
                exit 1
            else
                echo "Stopped"
                if [ -f "$pid_file" ]; then
                    rm "$pid_file"
                fi
            fi
        else
            echo "Not running"
        fi
    ;;
    restart)
        $0 stop
        if is_running; then
            echo "Unable to stop, will not attempt to start"
            exit 1
        fi
        $0 start
    ;;
    status)
        if is_running; then
            echo "Running"
        else
            echo "Stopped"
            exit 1
        fi
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac
exit 0

~
1 Like

opkg update && opkg install procps-ng-ps
This resolve the "ps" issue

Not using cloudflared, but could it be an invalid config yaml which is crashing cloudflared? Have you tried running it manually (/usr/bin/cloudflared --config=/etc/cloudflared/config.yml tunnel run) to see what happens?

Thank you but tried and this didn't help.

By doing this

/etc/init.d/cloudflared stop
mv /etc/init.d/cloudflared /etc/init.d/cloudflared.old
cloudflared service install
/etc/init.d/cloudflared start

Then a simple modification of start script by replacing ps $(get_pid) with ps | grep $(get_pid) | grep -v grep solve the issue and adding all functionality.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.