Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep ngrok running even when signing off of a server

Tags:

ngrok

tunnel

I have ngrok running on a server I remote into.

I start it by using the obvious, ngrok.exe http 80. The problem is that when I sign off on that particular server, ngrok will close out and I will lose my tunnel. Is there a way I can keep the ngrok tunnel running even when I have signed off the machine? I understand if the machine is shut down there is nothing I can do to keep the tunnel running, that is obvious. Any ideas?

Thanks in advance.

like image 999
Yusha Avatar asked Jun 04 '18 13:06

Yusha


People also ask

How much time does Ngrok last?

Problems using Ngrok One of the new limits is a session expiration time of 2 hours. When an Ngrok connection reaches this length it stops working, and the only way to restore it is by stopping and restarting the ngrok command, which causes a new randomly generated URL to be used.

How can I check my Ngrok status?

Check if ngrok is already running: Hit http://127.0.0.1:4040/status. If the above visual is not showing, ngrok is not running at all. If the above visual is not showing, ngrok is not running on PORT 4000.

What is Ngrok Authtoken?

The ngrok agent uses the authtoken (sometimes called tunnel credential) to log into your account when you start a tunnel. Copy the value and run this command to add the authtoken in your terminal.


1 Answers

As you've said If the machine is shutdown there will be no way keep the process running. There are a number of methods to do this. In each of these methods I'm assuming you already have the following config file:

config.yml

authtoken: <your-auth-token>
tunnels:
    default:
        proto: http
        addr: 80

Ngrok Link (Windows/Mac OS/Linux, Commercial)

With ngrok link simply run the following commands:

ngrok service install -config /path/to/your/config.yml
ngrok service start

You should then be able to manage ngrok as you would any other service running on your given operating system.

Nohup (Maco OS/Linux)

The nohup command normally comes installed by default on mac os and linux. To run the command as such:

nohup ngrok start --all --config="path/to/config.yml" &

Running in a screen should also achieve the same effect here.

Creating a Windows Service (Windows)

To create the service you will need to download a program for creating services from non service executables. Here I'm going to how to do this with NSSM (Non-Sucking Service Manager).

  1. Download the executable
  2. Open CMD and cd into the same directory as the nssm.exe
  3. Run to following command:

    nssm.exe install ngrok
    
  4. select the ngrok executable in the window that appears and add the following to the arguments, then press 'Install service'.

    start --all --config="C:\path\to\my\config.yml"
    
  5. The service can now be managed from service manager. To start it open an admin terminal and run the following:

    sc start ngrok
    

Creating a systemd service (Linux - systemd only)

Requires root.

  1. cd into /etc/systemd/system/

  2. Create the following file:

    ngrok.service

    [Unit]
    Description=Ngrok
    After=network.service
    
    [Service]
    type=simple
    User=<your_user_name>
    WorkingDirectory=/home/<your_user_name>
    ExecStart=/usr/bin/ngrok start --all --config="/path/to/config.yml"
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    
  3. Then run the following command to start and enable the service

    systemctl enable ngrok.service && systemctl start ngrok.service
    

sources:

https://ngrok.com/docs/ngrok-link#service

https://www.freedesktop.org/software/systemd/man/systemd.unit.html

https://nssm.cc/commands

like image 102
Hobbid Hobbin Avatar answered Sep 16 '22 17:09

Hobbid Hobbin