Getting cloudflared to run my tunnel as a service

I’ve finally resolved my issue from my last post and got cloudflared to run and make the tunnel.
The good news is when I run cloudflared tunnel run projects, my tunnel starts and I am able to get to the page through my browser. Hooray!

However, once I install it as a service, everything breaks down. I think the cause of the problem is that when starting the tunnel, it uses the config in /etc/cloudflared to launch the tunnel. Assuming it tries to find the file in the same directory, it’s missing. I copied the credentials file from ~/.cloudflared to /etc/cloudflared and it works when manually running it…

When I try starting the service, it crashes. systemctl status cloudflared gives me this:

● cloudflared.service - Cloudflare Tunnel
Loaded: loaded (/etc/systemd/system/cloudflared.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Thu 2020-12-03 16:20:01 UTC; 1s ago
Process: 3812 ExecStart=/usr/local/bin/cloudflared --config /etc/cloudflared/config.yml --no-autoupdate tunnel run (code=exited, status=1/FAILURE)
Main PID: 3812 (code=exited, status=1/FAILURE)

I am running Ubuntu Server 20.04 LTS on Arm32. This is running on a Raspberry Pi.

I was in that boat yesterday. Also with Ubuntu 20.04, but on a VPS.

For now, I’ve had to resort to running that cloudflared command with a & on the end to send it to background, then cron a script every ten minutes to make sure it’s running:

if [[ ! $(pgrep -f cloudflared) ]]; then
    cloudflared tunnel --hostname --url ssh://localhost:22 &
1 Like

I guess that’ll have to do for now… The only problem is that I have to run this every time the pi goes down, which I’m not entirely sure how stable everything is going to be (“everything” includes the electrical grid, too)

If the pi goes down, cron will restart that process within ten minutes of reboot. You can run it more frequently if you desire.

Is it normal to have an output when running in the background? It shouldn’t have an output, but running cloudflared tunnel run & gives me the same output as if I didn’t use the ampersand.

That’s normal. But if you log out, then back in, the process will remain, and will no longer output on your screen. You can also redirect output with > /dev/null 2>&1

1 Like

Ah, ok.

To clarify, how do I get it to run after a reboot?

A cron with @reboot which triggers the script should do the job

1 Like

My crontab for that script looks like this:
*/10 * * * * /root/ > /dev/null 2>&1

1 Like

You can also set up multiple crons. Actually the script just restarts the process if the service is not already running.

For ASAP retriggering the script after reboot I would recommend:

@reboot /root/ > /dev/null 2>&1

additionally you can set a recurring cron to restart the service if it would crash/go down. See: @reboot, which is something similar to “Autostart” at Windows.
You can go with soemthing what @sdayman wrote:

or even:

* * * * * /root/ > /dev/null 2>&1

As it anyway would check if the service is already running, due to:

But it would re-start the service a bit faster.

1 Like

Ok, this doesn’t solve the issue on having to move the credentials file around every time I reboot… I have to copy the file from ~/.cloudflared to /etc/cloudflared every time I reboot or the whole thing breaks…

If you have to move them everytime you do have 2 options I think:

  1. symlink them once (if possible)
  2. put the copy command into the script you are executing, so it happens everytime the script is getting executed

with this you can automize this

1 Like

Neither works

Edit: I have to run it in the same directory as the config…

Strange. If it works when you do it manually it should also work when doing it automized with a script.
You can chain commands with && to make sure the next command is just getting executed after the first one finished successfull.
What is the output/log of the command/script?

To debug you can change the crons to:

@reboot /root/ >> /log.txt 2>&1
*/10 * * * * /root/ >> /log.txt 2>&1

which will write ALL output to the file /log.txt which you later can view

May @sdayman can help and tell you how he automized the credential-problem.

Got it working! Just had to tweak the script to make it use cloudflared correctly and put the credentials file in the root directory. It no longer needs me to intervene!


Can you share your YAML file? (If not here… you can open a support ticket and post the number here).

I was finally able to get mine to work. I had to run the cloudflared service in legacy mode:

cloudflared service install --legacy

1 Like

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