36

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.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
martinenzinger
  • 2,166
  • 2
  • 17
  • 20

22 Answers22

42

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.

Artem Zinoviev
  • 869
  • 12
  • 32
  • This is by far the best solution for programatically accessing it! – the1dv Apr 26 '18 at 01:55
  • 1
    did you check it? – Tebe Jan 19 '19 at 16:56
  • 5
    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.) – lindes May 21 '19 at 06:18
  • 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 &` – Juliusz Gonera Feb 18 '20 at 00:42
  • `jq: error: Could not open file .tunnels[0].public_url: No such file or directory` – alper May 19 '21 at 21:52
40

as explained here

ngrok -log=stdout 80 > /dev/null &
martinenzinger
  • 2,166
  • 2
  • 17
  • 20
  • 2
    Does not work for ngrok2. Any ideas for the new version? – IvanD Jan 13 '16 at 07:09
  • @toas939 Oh, thanks man! Too much answers, and none of them is correct. – Kirby Apr 12 '18 at 13:52
  • 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. https://ngrok.com/docs/ngrok-link#service – dave4jr Oct 06 '18 at 00:30
  • @dave4jr: Is ngrok-link included in the ngrok client or does one have to install something else? The service command isn't recognized by ngrok – AndyFaizan Nov 15 '18 at 14:59
  • 1
    @AndyFaizan 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. – dave4jr Nov 22 '18 at 06:14
  • Okay. I asked because I already have a paid account but it still didn't work. Well, thanks anyway. – AndyFaizan Nov 22 '18 at 12:30
  • 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 :( – Ryan H Oct 21 '20 at 19:05
  • How can I get obtain its output? – alper May 19 '21 at 21:46
  • As of version 2.0.24, ngrok http 80 --log=stdout > ngrok.log & did the trick. – rlib Jul 22 '21 at 13:49
19

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

Kyrylo Malakhov
  • 1,256
  • 12
  • 13
  • 4
    `nohup ngrok http 80 --log=stdout > ngrok.log &` seems like safer in case ssh connection is dead – alper May 19 '21 at 21:50
15

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

Community
  • 1
  • 1
Oliv
  • 10,221
  • 3
  • 55
  • 76
  • 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. – Tim Malone Jun 27 '17 at 22:38
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"
bobmarksie
  • 3,282
  • 1
  • 41
  • 54
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.

Chandler Swift
  • 175
  • 1
  • 9
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

Liudas Šumskas
  • 131
  • 1
  • 6
7
curl http://127.0.0.1:4040/api/tunnels 

and you will see the public_url infomation .

Here's example . https://i.stack.imgur.com/V0905.png

Tung Tran
  • 439
  • 6
  • 8
6

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

Old Panda
  • 1,466
  • 2
  • 15
  • 30
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

Hemanth Kondapalli
  • 1,272
  • 12
  • 7
  • updated before, but no longer works. Failed to connect to localhost port 4040: Connection refused. see https://stackoverflow.com/a/48841928/5766054 answer – Stanislau Buzunko Jul 02 '19 at 17:47
  • but this still does not run in the background. It kills when i close the terminal – Poonam May 03 '20 at 19:35
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
G. Kilic
  • 111
  • 1
  • 5
5

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
Poonam
  • 166
  • 2
  • 14
4

Use below script for ngrok2

nohup ngrok http 3000 &

This will write logs to file nohup.out

  • 1
    nohup.out will not then contain sufficient information to know the API tunnel in use. See Hemanth's response. – Blake Feb 08 '18 at 00:42
2

nohup ./ngrok http 80 &

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

1

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

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

Beto Aveiga
  • 3,466
  • 4
  • 27
  • 39
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

Quang Nguyen
  • 134
  • 1
  • 6
0

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

400ZP
  • 26
  • 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! – Bogdan Stoica Jun 03 '20 at 07:59
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.

vkarpov15
  • 3,614
  • 24
  • 21
0
./ngrok http 80 -log=stdout > /dev/null &
  • Please add some explanation to your answer such that others can learn from it - to me, this looks like a duplicate of the existing answers – Nico Haase Mar 08 '21 at 15:28
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 &
Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
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"
Ro.
  • 1,525
  • 1
  • 14
  • 17
0

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/

starball
  • 20,030
  • 7
  • 43
  • 238
JiboOne
  • 1,438
  • 4
  • 22
  • 55