我尝试使用以下命令在后台运行ngrok:
./ngrok -subdomain test -config=ngrok.cfg 80 &
进程正在运行:
[1] 3866
子域名无法工作。
它可以使用:
./ngrok -subdomain test -config=ngrok.cfg 80
有人知道这里出了什么问题吗?
谢谢。
如前所述,您可以在后台运行ngrok。
./ngrok http 8080 > /dev/null &
接下来,您可以使用curl和例如jq
命令行JSON处理工具。
export WEBHOOK_URL="$(curl http://localhost:4040/api/tunnels | jq ".tunnels[0].public_url")"
您的URL将从$WEBHOOK_URL环境变量中访问,并且您可以在任何地方使用它。
如此所述 在这里
ngrok -log=stdout 80 > /dev/null &
简单易懂: ngrok http 80 --log=stdout > ngrok.log &
转化了技巧。
访问本地机器上的 http://localhost:4040/status,或在这里查看更多信息:运行在后台时查看随机 ngrok URL。
这是我写的一个小bash脚本,它在后台运行ngrok
。然后,它尝试通过调用一个curl
命令(针对http://127.0.0.1:4040/api/tunnels)和一个sed
命令(提取ngrok公共URL),来设置NGROK_PUBLIC_URL
变量。这一切都在一个循环内运行,直到NGROK_PUBLIC_URL
具有有效值,因为通常需要2或3秒钟才能建立ngrok隧道。
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 ]"
脚本将端口作为可选的命令行参数,例如。$ . start-ngrok.sh 1234
Run NGROK in background on port [ 1234 ]
Extracting ngrok public url ...
NGROK_PUBLIC_URL => [ 75d5faad.ngrok.io ]
...但如果没有提供端口,则会在端口8080
上运行...
$ . start-ngrok.sh
Run NGROK in background on port [ 8080 ]
Extracting ngrok public url ...
NGROK_PUBLIC_URL => [ 07e7a373.ngrok.io ]
NGROK_PUBLIC_URL
现在包含公共网址,即
$ echo $NGROK_PUBLIC_URL
07e7a373.ngrok.io
这可以在你的应用程序中被访问/使用。
注意: 这个脚本需要被
起源引入(. start-ngrok.sh
或source start-ngrok.sh
)。这是因为它正在设置一个环境变量,如果在新的shell中正常运行(即./start-ngrok.sh
),该变量将不可用。有关更多信息,请参见https://superuser.com/q/176783。
你还可以创建一个小脚本,使用pkill
/kill
等来停止后台的ngrok
进程: -
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"
nohup
(无挂起)命令的作用 - nohup ngrok http ${LOCAL_PORT} &>/dev/null &
- 更多详情请参见 https://en.wikipedia.org/wiki/Nohup。 - bobmarksiekill -9 "$(pgrep ngrok)"
。 - Bogdan Stoicapgrep
,谢谢分享! - bobmarksiengrok > /dev/null &
足够使用。yum install screen
命令。screen
,之后输入带参数的ngrok
命令。screen -r
稍后重新连接它们。./ngrok http 5000 > /dev/null &
,然后使用curl localhost:4040/status
检查URL。nohup
建议。 - Old Panda运行 ./ngrok http (端口号) &,这将将ngrok隧道作为后台进程运行。通常,ngrok会打开一个显示分配URL的窗口,但由于我们正在使用nohup命令,则不可见。
因此,然后运行 curl http://127.0.0.1:4040/api/tunnels,以查看ngrok分配的URL。
curl --silent
(或curl -s
)和jq -r
来减少输出信息。因此,在双引号内键入以下内容:$(curl -s http://localhost:4040/api/tunnels | jq -r '.tunnels[0].public_url')
。(如果需要,请将此内容放入答案中;现在作为注释留下。) - lindesngrok http --bind-tls=true --log=stdout 8080 > /dev/null &
- Juliusz Gonerajq: error: Could not open file .tunnels[0].public_url: No such file or directory
- alper