How to run ngrok in background?
Asked Answered
E

22

39

I tried running ngrok in the background with following command:

./ngrok -subdomain test -config=ngrok.cfg 80 &

the process is running:

[1] 3866

and the subdomain doesn't work.

It works with:

./ngrok -subdomain test -config=ngrok.cfg 80

Does anyone know what is going wrong here?

Thank you.

Erosive answered 27/11, 2014 at 3:21 Comment(0)
E
43

as explained here

ngrok -log=stdout 80 > /dev/null &
Erosive answered 27/11, 2014 at 5:33 Comment(9)
Does not work for ngrok2. Any ideas for the new version?Underline
@toas939 Oh, thanks man! Too much answers, and none of them is correct.Lightship
If you want to run this in a production environment and without having to open up your terminal, you can create and run ngrok as a service that will automatically start when your machine boots. Check here for instructions. ngrok.com/docs/ngrok-link#serviceAfricanize
@dave4jr: Is ngrok-link included in the ngrok client or does one have to install something else? The service command isn't recognized by ngrokDecanter
@Decanter Unfortunately it does cost a little extra, I forget how much our company paid for this but I don't remember it being a lot.Africanize
Okay. I asked because I already have a paid account but it still didn't work. Well, thanks anyway.Decanter
I want to ngrok a folder into the background... how can I do this? ./ngrok http file:///my-folder -log=stdout http > /dev/null & doesn't work :(Hasid
How can I get obtain its output?Gonzalez
As of version 2.0.24, ngrok http 80 --log=stdout > ngrok.log & did the trick.Heroics
C
49

as described previously you can run ngrok in background with

./ngrok http 8080 > /dev/null &

next you can use curl and for example jq a command-line JSON processor.

export WEBHOOK_URL="$(curl http://localhost:4040/api/tunnels | jq ".tunnels[0].public_url")"

your URL will be accessible from $WEBHOOK_URL env variable and you can use it anywhere.

Cantonese answered 17/2, 2018 at 13:39 Comment(5)
This is by far the best solution for programatically accessing it!Picked
did you check it?Concertmaster
I might suggest curl --silent (or curl -s) and jq -r to have quieter output. So: $(curl -s http://localhost:4040/api/tunnels | jq -r '.tunnels[0].public_url') inside the double quotes. (Feel free to pull this into the answer; leaving it as a comment for now.)Babylon
This is not deterministic if you have more than one tunnel. A simple workaround is to force just one tunnel by using only HTTPS: ngrok http --bind-tls=true --log=stdout 8080 > /dev/null &Passionate
jq: error: Could not open file .tunnels[0].public_url: No such file or directoryGonzalez
E
43

as explained here

ngrok -log=stdout 80 > /dev/null &
Erosive answered 27/11, 2014 at 5:33 Comment(9)
Does not work for ngrok2. Any ideas for the new version?Underline
@toas939 Oh, thanks man! Too much answers, and none of them is correct.Lightship
If you want to run this in a production environment and without having to open up your terminal, you can create and run ngrok as a service that will automatically start when your machine boots. Check here for instructions. ngrok.com/docs/ngrok-link#serviceAfricanize
@dave4jr: Is ngrok-link included in the ngrok client or does one have to install something else? The service command isn't recognized by ngrokDecanter
@Decanter Unfortunately it does cost a little extra, I forget how much our company paid for this but I don't remember it being a lot.Africanize
Okay. I asked because I already have a paid account but it still didn't work. Well, thanks anyway.Decanter
I want to ngrok a folder into the background... how can I do this? ./ngrok http file:///my-folder -log=stdout http > /dev/null & doesn't work :(Hasid
How can I get obtain its output?Gonzalez
As of version 2.0.24, ngrok http 80 --log=stdout > ngrok.log & did the trick.Heroics
G
26

Easy: ngrok http 80 --log=stdout > ngrok.log & did the trick.

Geoid answered 15/1, 2020 at 17:2 Comment(2)
nohup ngrok http 80 --log=stdout > ngrok.log & seems like safer in case ssh connection is deadGonzalez
In 2024, top 2 answers did not work. Only this one did.Evelinevelina
P
16

Visit http://localhost:4040/status on your local machine, or see more here: View random ngrok URL when run in background.

Peking answered 14/11, 2016 at 7:33 Comment(1)
Or sign up for a free account on the ngrok site and connect your local install to it - then you can view your active tunnels online.Splore
D
13

Here's a little bash script I wrote which runs ngrok in the background. It then tries to sets a NGROK_PUBLIC_URL variable by calling a curl command (against http://127.0.0.1:4040/api/tunnels) followed by a sed command (which extracts the ngrok public URL). This all runs inside a loop until NGROK_PUBLIC_URL has a valid value as it normally takes ngrok 2 or 3 seconds for it's tunnels to become established.

start-ngrok.sh

#!/bin/sh

# Set local port from command line arg or default to 8080
LOCAL_PORT=${1-8080}

echo "Start ngrok in background on port [ $LOCAL_PORT ]"
nohup ngrok http ${LOCAL_PORT} &>/dev/null &

echo -n "Extracting ngrok public url ."
NGROK_PUBLIC_URL=""
while [ -z "$NGROK_PUBLIC_URL" ]; do
  # Run 'curl' against ngrok API and extract public (using 'sed' command)
  export NGROK_PUBLIC_URL=$(curl --silent --max-time 10 --connect-timeout 5 \
                            --show-error http://127.0.0.1:4040/api/tunnels | \
                            sed -nE 's/.*public_url":"https:..([^"]*).*/\1/p')
  sleep 1
  echo -n "."
done

echo
echo "NGROK_PUBLIC_URL => [ $NGROK_PUBLIC_URL ]"

The script takes a port as an optional command line argument i.e.

$ . start-ngrok.sh 1234

Run NGROK in background on port [ 1234 ]
Extracting ngrok public url ...
NGROK_PUBLIC_URL => [ 75d5faad.ngrok.io ]

... but will run on port 8080 if the port isn't supplied ...

$ . start-ngrok.sh 

Run NGROK in background on port [ 8080 ]
Extracting ngrok public url ...
NGROK_PUBLIC_URL => [ 07e7a373.ngrok.io ]

The NGROK_PUBLIC_URL now contains the public url i.e.

$ echo $NGROK_PUBLIC_URL
07e7a373.ngrok.io

This can be accessed / used in your applications.

Note: This script needs to be sourced (. start-ngrok.sh OR source start-ngrok.sh). This is because it is setting an environment variable which wont be available if run normally in a new shell (i.e. ./start-ngrok.sh). See https://superuser.com/q/176783 for more info.

You can also create a little script using pkill / kill etc to stop the background ngrok process: -

stop-ngrok.sh

#!/bin/sh

echo "Stopping background ngrok process"
kill -9 $(ps -ef | grep 'ngrok' | grep -v 'grep' | awk '{print $2}')
echo "ngrok stopped"
Diachronic answered 26/8, 2019 at 10:11 Comment(4)
but this still does not run in the background. It kills when i close the terminalAestival
It doesn't on mine - that's what the nohup (no hangup) command does - nohup ngrok http ${LOCAL_PORT} &>/dev/null & - see en.wikipedia.org/wiki/Nohup for more details.Diachronic
To kill the proces you could have issued a simple: kill -9 "$(pgrep ngrok)"Novak
Very nice, never used pgrep before, thanks for sharing!Diachronic
W
11

In Ngrok 2, -log is neither necessary nor available (though you can control log levels in the configuration file). ngrok > /dev/null & is sufficient.

Whitworth answered 21/1, 2016 at 23:37 Comment(5)
But how do I know the URL to access my service?Peking
If you want to do that, you'll have to run it in the foreground (AFAIK). Background running is primarily helpful for running as a service, when you'd more than likely know your domain.Whitworth
See my answer, there is a way.Peking
Also Artem's answer, https://mcmap.net/q/398652/-how-to-run-ngrok-in-background (with a comment from me to slightly modify it.)Babylon
@Oliv: The easiest way of checking the URL is to log in into the Ngrok web page and using the Dashboard.Tammitammie
G
9

If you want to use multiple shell windows or run any service in the background from a single SSH session that the simplest way is to use screen.

To install on the Centos Linux use yum install screen

Then start like any other command screen, after that type ngrok command within a parameters.

Detaching is the most powerful part of screen. Screen allows you to detach from a window and reattach later.

If your network connection fails, screen will automatically detach your session! You can detach from the window using “Ctrl-a” “D”.

This will drop you back into your shell.

All screen windows are still there and you can re-attach to them later using screen -r

Garbo answered 14/6, 2017 at 9:22 Comment(1)
It is generally considered a bad answer if you can't use your answer without the link.Tarazi
J
7
curl http://127.0.0.1:4040/api/tunnels 

and you will see the public_url infomation .

Here's example . https://i.sstatic.net/V0905.png

Jovi answered 27/12, 2018 at 9:17 Comment(0)
F
6

Run ./ngrok http 5000 > /dev/null & then curl localhost:4040/status to check url

Formulaic answered 27/9, 2017 at 2:43 Comment(3)
need correction , nohup ./ngrok http 5000 > /dev/null & so when we close terminal. process will not get killEstray
but this still does not run in the backgroundAestival
@Aestival Sorry I didn't put it in my answer. But please check out the nohup suggestion as the above comment says.Formulaic
F
6

Run ./ngrok http (port number) & This runs the ngrok tunnel as a background process. Ngrok usually opens a windown showing the assigned URL but since we are using the nohup command this is not visible.

Thus, then run curl http://127.0.0.1:4040/api/tunnels too see the URL assigned by ngrok

Florida answered 22/1, 2018 at 21:12 Comment(2)
updated before, but no longer works. Failed to connect to localhost port 4040: Connection refused. see https://mcmap.net/q/398652/-how-to-run-ngrok-in-background answerSemester
but this still does not run in the background. It kills when i close the terminalAestival
A
6

so i tried everything but the best answer to this which works in the year 2020 is:

how to create public local host(8080):

ngrok -log=stdout 80 > ngrok.log &

then

curl http://127.0.0.1:4040/api/tunnels
Aestival answered 3/5, 2020 at 22:16 Comment(3)
Can confirm, best answer for 2020.Inhalation
doesn't work with ngrok==2.3.35 Incorrect Usage: flag provided but not defined: -logIndividual
use ngrok --log=stdout 80 > ngrok.log & insteadAcidity
A
5

try to run as service. and check it from ngrok website. I tried for ngrok version 2.2.8 on a Raspberry pi 3.

ngrok.service as

[Unit]
Description=Share local port(s) with ngrok
After=syslog.target network.target

[Service]
Type=simple
Restart=always
RestartSec=1min
StandardOutput=null
StandardError=null
ExecStart=/usr/local/sbin/ngrok start --log /var/log/ngrok.log --config /etc/ngrok.yml --all
ExecStop=/usr/bin/killall ngrok

[Install]
WantedBy=multi-user.target

configuration file: ngrok.yml authtoken:

tunnels:
  <your Tunel Name >:
    proto: http
    addr: 80
Adduce answered 16/4, 2018 at 22:3 Comment(0)
P
4

Use below script for ngrok2

nohup ngrok http 3000 &

This will write logs to file nohup.out

Pharr answered 9/2, 2017 at 9:19 Comment(1)
nohup.out will not then contain sufficient information to know the API tunnel in use. See Hemanth's response.Waxler
F
2

nohup ./ngrok http 80 &

Hit localhost:4040 to get the public URL assigned by ngrok

Filament answered 17/6, 2019 at 15:51 Comment(0)
M
1

You can use screen for that. Here is an example of how I use it:

screen -d -m ~/./ngrok http test.cc:8080

Mediation answered 15/5, 2018 at 23:7 Comment(0)
B
1

Ngrok now has its own solution for it, which is "installing as a service."

This will start the service on machine boot and restart the service automatically in case of any failure. There is an official document that explains it precisely: https://ngrok.com/docs/secure-tunnels/ngrok-agent/installing-as-a-service/

Bellanca answered 25/8, 2023 at 10:2 Comment(0)
S
0
nohup /usr/local/bin/ngrok --config /etc/ngrok.yml http 8080 &

If you link with ngrok account, then you can view the public url in ngrok site, do not need to curl the local url Public url https://dashboard.ngrok.com/status

Sepoy answered 4/1, 2019 at 0:1 Comment(0)
C
0

You can use cotunnel. It is ngrok alternative. The cotunnel installation script creates service and works itself on the background.

Collage answered 9/5, 2020 at 16:26 Comment(1)
I think is kinda too much to say it's an alternative... It tries to be but it's way too far away from ngrok as in the flexibility. Plus, honestly, I don't like the ideea that someone is able to ssh into my server without any user/pass directly from their site. From a security perspective, this sucks big time! Installed, checked, uninstalled. Period!Novak
S
0

Another alternative is using the ngrok npm module. If you have Node installed, you can run ngrok and print out the URL as shown below:

require('ngrok').connect({ proto: 'http', port: 3000 }).then(url => {
  console.log(url);
});

Not strictly running it in the background, but I find this to be the easiest approach for my requirements: starting ngrok and a web server that's aware of its ngrok url in a single command.

Salify answered 13/7, 2020 at 18:5 Comment(0)
C
0
./ngrok http 80 -log=stdout > /dev/null &
Ching answered 6/3, 2021 at 14:18 Comment(1)
Please add some explanation to your answer such that others can learn from it - to me, this looks like a duplicate of the existing answersChamade
B
0

In Windows Batch Script (.bat), if you want to execute next command without waiting ngrok, use the following command with start /b:

start /b ngrok http 80 --log=stdout > ngrok.log &
Brandenbrandenburg answered 15/7, 2021 at 5:25 Comment(0)
N
0

Two steps:

  1. First set console_ui to false in your config file (.ngrok2/ngrok.yml)

  2. Run command like this

    $ ./ngrok start demo &
    

Extra: if you started this via ssh and you want it to continue running even if you disconnect run it like this

nohup ./ngrok start demo &

Example of my config file

    authtoken: XXXXX
    region: us
    console_ui: false
    web_addr: localhost:4040


    tunnels:    
      demo:
        proto: http
        addr: 9090
        hostname: demo.mysite.com
        inspect: false
        auth: "demo:secret"
Nonrecognition answered 26/10, 2021 at 13:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.