命令行-如何使用多个先验值编写脚本来启动应用程序?

分享于 

3分钟阅读

ubuntu

  繁體

问题:

为了测试webhooks,我需要运行ngrok http https://localhost:3000,其结果与以下内容相同--


ngrok by @inconshreveable (Ctrl+C to quit)



Session Status online 


Account myemail@gmail.com (Plan: Free) 


Version 2.3.35 


Region United States (us) 


Web Interface http://127.0.0.1:4040 


Forwarding http://2f22e8990cbe.ngrok.io -> https://localhost:3000 


Forwarding https://2f22e8990cbe.ngrok.io -> https://localhost:3000 



Connections ttl opn rt1 rt5 p50 p90 


 13973 1 0.00 0.07 0.00 0.00 



然后我更改一个配置文件以容纳新主机(随机使用ngrok)


# development.rb


config.hosts << '2f22e8990cbe.ngrok.io' # (tunnel address from above)



然后保存文件,并启动web服务器。


$ rails server puma --bind 'ssl://127.0.0.1:3000?key=/home/oaty/.ssh/localhost.key&cert=/home/oaty/.ssh/localhost.crt'




我想把这些命令编写成一个脚本。


答案1:

就像这样:


newhost=$(ngrok http https://localhost:3000 | 


 grep -E '^Forwardings+https://' | 


 grep -E -o '[0-9a-f]{12}.ngrok.io`)



if [[ -n "$newhost" ]] ; then


 echo "$newhost" >>config.hosts


 rails server ...(the rest)


else


 echo "Failure in ngrok" >&2


fi




答案2:

一个更精确的解决方法,

boot_ssl.sh


#!/bin/bash


sh -c 'ngrok http https://localhost:3000 &'


sleep 2


curl --silent http://127.0.0.1:4040/api/tunnels | jq '.tunnels[0].public_url' > .ngrok_ssl_host


rails s puma -b 'ssl://127.0.0.1:3000?key=/home/jess/.ssh/localhost.key&cert=/home/jess/.ssh/localhost.crt'



然后在config/development.rb文件中,: )


 NGROK_SSL_HOST = File.open('.ngrok_ssl_host').read.match(/[0-9a-z]{12}.ngrok.io/)[0]



 config.hosts << NGROK_SSL_HOST





COM  STA  COMM  PRE  Comma  命令行  
相关文章