代码之家  ›  专栏  ›  技术社区  ›  Yusha

如何在注销服务器时保持ngrok运行

  •  3
  • Yusha  · 技术社区  · 7 年前

    我有 ngrok 在我远程访问的服务器上运行。

    我先用明显的, ngrok.exe http 80 . 问题是,当我在特定服务器上注销时,ngrok将关闭,我将丢失隧道。有没有一种方法可以让ngrok隧道保持运行,即使我已经关闭了机器?我明白,如果机器关闭了,我就没办法让隧道继续运转,这是显而易见的。有什么想法吗?

    提前谢谢。

    1 回复  |  直到 7 年前
        1
  •  17
  •   Hobbid Hobbin    7 年前

    正如您所说的,如果机器关闭,就无法保持进程运行。有很多方法可以做到这一点。在这些方法中,我假设您已经拥有以下配置文件:

    小精灵

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

    ngrok链接(windows/mac os/linux,商用)

    使用ngrok link只需运行以下命令:

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

    然后,您应该能够像管理给定操作系统上运行的任何其他服务一样管理ngrok。

    nohup(maco操作系统/linux)

    nohup命令通常默认安装在mac os和linux上。要按此方式运行命令:

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

    在屏幕上运行也应该达到同样的效果。

    创建Windows服务(Windows)

    要创建服务,您需要下载一个用于从非服务可执行文件创建服务的程序。下面我将介绍如何使用nssm(非吸吮服务管理器)来实现这一点。

    1. 下载可执行文件
    2. 打开CMD和CD到与nssm.exe相同的目录中
    3. 运行以下命令:

      nssm.exe install ngrok
      
    4. 在出现的窗口中选择ngrok可执行文件,并将以下内容添加到参数中,然后按“安装服务”。

      start --all --config="C:\path\to\my\config.yml"
      
    5. 现在可以从服务管理器管理服务。要启动它,请打开管理终端并运行以下命令:

      sc start ngrok
      

    创建systemd服务(仅限linux-systemd)

    需要根。

    1. cd进入/etc/systemd/system/

    2. 创建以下文件:

      NgRok.服务

      [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. 然后运行以下命令启动并启用服务

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

    资料来源:

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

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

    https://nssm.cc/commands