#!/bin/bash # /etc/init.d/ngrok # Thanks Marcos Lin for generic deamon ngrok_bin="/opt/ngrok/ngrok" ngrok_config_file="$ngrok_bin.yml" ngrok_region="sa" process_name="$ngrok_bin" process_prams="start -log=stdout --config $ngrok_config_file --all --region=$ngrok_region" process_base="$(basename $process_name)" process_out="/var/log/$process_base.log" process_pid="/var/run/$process_base.pid" # # Check command line option. # action=$1 shift params=$process_prams help_needed () { echo "$0 " 1>&2 exit 1 } if [ -z "$action" ]; then echo "Error: missing required parameter" 1>&2 help_needed elif [[ ! "$action" =~ ^start|status|stop|restart$ ]]; then echo "Error: '$action' is not valid option" 1>&2 help_needed fi # # Check process # running_pid () { if [ -r "$process_pid" ]; then local pid=$(cat $process_pid) local proc=$(ps w -p $pid | grep $process_name) if [ -n "$proc" ]; then echo $pid else local pid_search=$(search_pid) if [ -n "$pid_search" ]; then echo $pid_search else # pid file exists but no matching process found. Delete the pid file rm "$process_pid" fi fi else local pid_search=$(search_pid) if [ -n "$pid_search" ]; then echo $pid_search fi fi } search_pid() { local pid=$(ps aux | grep $process_name | grep -v grep | awk "{ print \$2 }") if [ -n "$pid" ]; then # Write data if [ ! -r "$process_pid" ]; then echo $pid > $process_pid fi # Return PID found echo $pid fi } kill_process () { local pid=$1 local proc=$pid local retries=1 local max_retries=4 local retries_force=$(($max_retries-1)) echo -n "Stopping process $pid " while [ $retries -lt $max_retries -a -n "$proc" ]; do echo -n "." # Force kill if last retry if [ $retries -lt $retries_force ]; then kill $pid > /dev/null else kill -9 $pid > /dev/null fi # Check if process was killed proc=$(running_pid) if [ -z "$proc" ]; then if [ -r "$process_pid" ]; then rm $process_pid fi retries=$max_retries else retries=$((retries+1)) sleep 1 fi done echo '' if [ -n "$proc" ]; then echo "Error: failed to kill process running as pid $pid" 1>&2 return 1 fi } get_tunnels() { eval $(cat $process_out | grep 'msg="starting web service"' | tail -1) echo "Web inspect: $(tput setaf 2 2>/dev/null)http://$addr$(tput sgr0 2>/dev/null)" cat $process_out | grep 'msg="started tunnel"' | while read tunnel;do eval $tunnel if [ ! -z "$url" ]; then echo "Tunnel: $(tput setaf 2 2>/dev/null)$url$(tput sgr0 2>/dev/null) -> $(tput setaf 1 2>/dev/null)$addr$(tput sgr0 2>/dev/null)" fi done } # # Actions # pid_running=$(running_pid) start () { if [ -z "$pid_running" ]; then # source ./venv/bin/activate nohup $process_name $params &> $process_out & sleep 1 local running_pid=$(search_pid) if [ -n "$running_pid" ]; then echo $! > $process_pid echo "Process started with pid $(cat $process_pid)" else echo "Error: Process failed to start" cat $process_out return 1 fi else echo "Process already running as pid $pid_running" fi if [ -n "$pid_running" -o -z "$pid_running" ]; then get_tunnels fi } status () { if [ -z "$pid_running" ]; then echo "Process not running" else echo "Process running as pid $pid_running" get_tunnels fi } stop () { if [ -n "$pid_running" ]; then kill_process $pid_running local kill_process_result=$? echo "t=$(date '+%Y-%m-%dT%H:%M:%S%z') lvl=info msg=\"tunnel session stop callled\"" >> $process_out return $kill_process_result else echo "No process running" return 1 fi } restart () { stop if [ $? -ne 0 ]; then return 1 fi pid_running=$(running_pid) start } # Execute action $action