───────┬────────────────────────────────────────────────────────────────────────
       │ File: ./tor_restarting_hack.bash
───────┼────────────────────────────────────────────────────────────────────────
   1   │ #!/usr/bin/bash
   2   │ # The "#!/usr/bin/env bash" would depend on PATH, so it is not used here. 
   3   │ #==========================================================================
   4   │ # Initial author of this file: Martin.Vahi@softf1.com
   5   │ # This file is in public domain.
   6   │ #
   7   │ # Initial author of the template of this file: Martin.Vahi@softf1.com
   8   │ # The following line is a spdx.org license label line:
   9   │ # SPDX-License-Identifier: 0BSD
  10   │ #==========================================================================
  11   │ CMD_GREP="/home/mmmv/applications/GNU_grep/v_3_11/bin/grep"
  12   │ CMD_KILL="/usr/bin/kill"
  13   │ CMD_NICE="/usr/bin/nice"
  14   │ CMD_PRINTF="/usr/bin/printf"
  15   │ CMD_PS="/usr/bin/ps"
  16   │ CMD_SED="/home/mmmv/applications/GNU_sed/v_4_9/bin/sed" # Linux sed (GNU sed) and BSD sed differ
  17   │ CMD_SLEEP="/usr/bin/sleep"
  18   │ CMD_SYNC="/usr/bin/sync"
  19   │ CMD_UNAME="/usr/bin/uname"
  20   │ CMD_XARGS="/usr/bin/xargs"
  21   │ CMD_TOR="/home/mmmv/applications/Tor/v_0_4_8_15/bin/tor"
  22   │ #--------------------------------------------------------------------------
  23   │ func_throw(){
  24   │     #----------------------------------------------------------------------
  25   │     local S_CMD_OR_SOME_OTHER_TEXT="$1"
  26   │     local S_GUID="$2"
  27   │     #----------------------------------------------------------------------
  28   │     echo ""
  29   │     echo -e "\e[31mMissing\e[39m"
  30   │     echo -e "    $S_CMD_OR_SOME_OTHER_TEXT"
  31   │     echo "GUID=='$S_GUID'"
  32   │     echo ""
  33   │     exit 1
  34   │     #----------------------------------------------------------------------
  35   │ } # func_throw
  36   │ #--------------------------------------------------------------------------
  37   │ func_kill_tor(){
  38   │     local SI_SIGNAL="$1"
  39   │     local S_PID="`$CMD_PS -A | $CMD_GREP -E '[ ]tor\$' | $CMD_SED -e 's/^[[:blank:]]//g' | $CMD_SED -e 's/[[:blank:]].*\$//g'`"
  40   │     if [ "$S_PID" != "" ]; then
  41   │         $CMD_SYNC
  42   │         wait
  43   │         $CMD_XARGS $CMD_KILL -s $SI_SIGNAL $S_PID  &
  44   │         # The "&" at the previous line allows 
  45   │         #     "kill -s 9 $S_PID "  
  46   │         # to be called if the graceful exit takes too long.
  47   │     fi
  48   │     $CMD_SYNC
  49   │     wait
  50   │ } # func_kill_tor
  51   │ #--------------------------------------------------------------------------
  52   │ HOME="/home/www1" # so that cron also can run this script
  53   │ S_FP_WWW1_TORRC="$HOME/m_local/etc/torrc_veebiserver01_www1.conf"
  54   │ if [ -e "$CMD_GREP" ]; then
  55   │ if [ -e "$CMD_UNAME" ]; then
  56   │ if [ "`$CMD_UNAME -a | $CMD_GREP -i linux `" != "" ]; then
  57   │ if [ -e "$S_FP_WWW1_TORRC" ]; then
  58   │     if [ -e "$CMD_KILL" ]; then
  59   │     if [ -e "$CMD_NICE" ]; then
  60   │     if [ -e "$CMD_PRINTF" ]; then
  61   │     if [ -e "$CMD_PS" ]; then
  62   │     if [ -e "$CMD_SED" ]; then
  63   │     if [ -e "$CMD_SLEEP" ]; then
  64   │     if [ -e "$CMD_SYNC" ]; then
  65   │     if [ -e "$CMD_XARGS" ]; then
  66   │     if [ -e "$CMD_TOR" ]; then
  67   │     #----------------------------------------------------------------------
  68   │         $CMD_SYNC
  69   │         wait
  70   │         $CMD_SLEEP 4
  71   │         # /usr/bin/killall --signal 15 tor # graceful exit
  72   │         func_kill_tor "15"
  73   │         $CMD_SLEEP 3
  74   │         # /usr/bin/killall --signal  9 tor # kill process
  75   │         func_kill_tor "9"
  76   │         $CMD_PRINTF "" > /opt/partitions/RAM_20MiB/www1_tor.log
  77   │         $CMD_SLEEP 5
  78   │         $CMD_NICE -n 8 $CMD_TOR -f $S_FP_WWW1_TORRC  &
  79   │     #----------------------------------------------------------------------
  80   │     else func_throw "$CMD_TOR" "75703615-2ece-4c51-bd19-f103107049e7"
  81   │     fi
  82   │     else func_throw "$CMD_XARGS" "3e909297-3878-44bc-9049-f103107049e7"
  83   │     fi
  84   │     else func_throw "$CMD_SYNC" "d442efba-e492-481c-b719-f103107049e7"
  85   │     fi
  86   │     else func_throw "$CMD_SLEEP" "e1275270-1cc4-451c-b919-f103107049e7"
  87   │     fi
  88   │     else func_throw "$CMD_SED" "a3c302d3-a06b-42ae-9999-f103107049e7"
  89   │     fi
  90   │     else func_throw "$CMD_PS" "f48cb1e7-8af2-4540-8219-f103107049e7"
  91   │     fi
  92   │     else func_throw "$CMD_PRINTF" "45eac015-f205-4ebc-8339-f103107049e7"
  93   │     fi
  94   │     else func_throw "$CMD_NICE" "2e3ad5b4-7fa8-459f-be39-f103107049e7"
  95   │     fi
  96   │     else func_throw "$CMD_KILL" "52c84e88-0100-41f5-8f19-f103107049e7"
  97   │     fi
  98   │ else func_throw "$S_FP_WWW1_TORRC" "565d9e95-069f-4f5a-a359-f103107049e7"
  99   │ fi
 100   │ else func_throw "Linux operating system. This script has been customised to Linux." "aac14291-3590-43fe-8169-f103107049e7"
 101   │ fi
 102   │ else func_throw "$CMD_UNAME" "543d8720-ac15-4ea4-bb29-f103107049e7"
 103   │ fi
 104   │ else func_throw "$CMD_GREP" "5bac7db4-3efe-476d-bf28-f103107049e7"
 105   │ fi
 106   │ 
 107   │ #==========================================================================
 108   │ # S_VERSION_OF_THIS_FILE="91f301c5-ef7f-4894-a818-f103107049e7"
 109   │ #==========================================================================
───────┴────────────────────────────────────────────────────────────────────────