How to run ngrok in background?

91.5k views Asked by At

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.

22

There are 22 answers

9
martinenzinger On BEST ANSWER

as explained here

ngrok -log=stdout 80 > /dev/null &
0
vkarpov15 On

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.

0
Beto Aveiga On

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

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

5
Chandler Swift On

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

3
Old Panda On

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

1
Abhinivesh Vijayan On
./ngrok http 80 -log=stdout > /dev/null &
4
bobmarksie On

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"
1
Chirag Shah On

Use below script for ngrok2

nohup ngrok http 3000 &

This will write logs to file nohup.out

3
Poonam On

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
1
400ZP On

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

0
Tung Tran On
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

0
Quang Nguyen On
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

0
G. Kilic On

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
0
Jerry Chong On

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 &
1
Oliv On

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

0
JiboOne On

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/

0
Ro. On

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"
5
Artem Zinoviev On

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.

2
Kyrylo Malakhov On

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

1
Liudas Šumskas On

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

0
Sagar Gupta On

nohup ./ngrok http 80 &

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

2
Hemanth Kondapalli On

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