Advertisement
tolikpunkoff

tinyproxy-startup-script

Nov 4th, 2016
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.97 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. PIDFILE="/home/provproxy/tinyproxy.pid"
  4. TINYPROXYCMD="/usr/sbin/tinyproxy"
  5. PIDVAL=0
  6. WTIMEOUT=30
  7. OK=0
  8.  
  9. CH_S[0]='-' #pseudographic items
  10. CH_S[1]='/'
  11. CH_S[2]='|'
  12. CH_S[3]='\'
  13. ITEM_ARR=0 #current item counter
  14.  
  15.  
  16. process_status()
  17. {
  18.     #poluchaem PID
  19.     if [ -e $PIDFILE ];then #esli file exist
  20.     PIDVAL=`cat $PIDFILE` #chitaem PID
  21.     TMPGREP=`ps -p $PIDVAL|grep "tinyproxy" -c` #esli process zapushen db 1 inache 0
  22.     if [ $TMPGREP -ge 1 ];then #process zapushen
  23.         return
  24.     else #pid-file levyi
  25.         rm $PIDFILE
  26.     fi
  27.     fi
  28.     PIDVAL=0
  29.     return
  30. }
  31.  
  32. status_proxy()
  33. {
  34.     process_status
  35.     if [ $PIDVAL -eq 0 ]; then
  36.     echo "Tinyproxy not running"
  37.     else
  38.     echo "Tinyproxy running [PID=$PIDVAL]"
  39.     fi
  40. }
  41.  
  42. start_proxy()
  43. {
  44.     process_status
  45.     if [ "$PIDVAL" -ne 0  ]; then
  46.     echo "Tinyproxy already work! [$PIDVAL]"
  47.     return 1
  48.     fi
  49.     $TINYPROXYCMD
  50.    
  51.     RETCODE=$?
  52.     if [ "$RETCODE" -ne 0 ]; then
  53.     echo "Not starting (code $RETCODE)"
  54.     return 1
  55.     fi
  56.    
  57.     echo -n "Starting proxy ("$WTIMEOUT" secounds): "
  58.     tput sc #save cursor position
  59.  
  60.     while [ $WTIMEOUT -ge 0 ]; do
  61.    
  62.     #print timeout and current pseudographic char
  63.     printf '%3s %s' $WTIMEOUT ${CH_S[ITEM_ARR]}
  64.     tput rc #restore cursor position
  65.     sleep 1
  66.     process_status
  67.     if [ $PIDVAL -ne 0 ]; then #zapustili
  68.             OK=1
  69.             break
  70.     fi
  71.  
  72.    
  73.     #decrease timeout and increase current item ctr.
  74.     let "WTIMEOUT=WTIMEOUT-1"
  75.     let "ITEM_ARR=ITEM_ARR+1"
  76.    
  77.     if [ $ITEM_ARR -eq 4 ];then
  78.         #if items ctr > number of array items
  79.         #starting with 0 item
  80.         let "ITEM_ARR=0"
  81.     fi
  82.        
  83.     done
  84.  
  85.     #next message starting with new string
  86.     printf '\n'
  87.    
  88.     if [ "$OK" -eq 1 ]; then
  89.     echo "Started [$PIDVAL]"
  90.     else
  91.     echo "Not started :("
  92.     fi
  93. }
  94.  
  95. stop_proxy()
  96. {
  97.     process_status
  98.     if [ $PIDVAL -eq 0 ]; then
  99.     echo "Tinyproxy not work!"
  100.     return 1
  101.     fi
  102.  
  103.     echo -n "Stopping proxy [$PIDVAL] in $WTIMEOUT secounds: "
  104.    
  105.     tput sc #save cursor position
  106.  
  107.     while [ $WTIMEOUT -ge 0 ]; do
  108.    
  109.     #print timeout and current pseudographic char
  110.     printf '%3s %s' $WTIMEOUT ${CH_S[ITEM_ARR]}
  111.     tput rc #restore cursor position
  112.     kill $PIDVAL
  113.     sleep 1
  114.     process_status
  115.     if [ $PIDVAL -eq 0 ]; then #ubit
  116.         OK=1
  117.         break
  118.     fi
  119.    
  120.    
  121.     #decrease timeout and increase current item ctr.
  122.     let "WTIMEOUT=WTIMEOUT-1"
  123.     let "ITEM_ARR=ITEM_ARR+1"
  124.    
  125.     if [ $ITEM_ARR -eq 4 ];then
  126.         #if items ctr > number of array items
  127.         #starting with 0 item
  128.         let "ITEM_ARR=0"
  129.     fi
  130.        
  131.     done
  132.  
  133.     #next message starting with new string
  134.     printf '\n'
  135.    
  136.     if [ "$OK" -eq 1 ]; then
  137.     echo "Stopped!"
  138.     else
  139.     echo "Not stopped :("
  140.     fi
  141. }
  142.  
  143. restart_proxy()
  144. {
  145.     stop_proxy
  146.     OK=0
  147.     start_proxy
  148. }
  149.  
  150. case "$1" in
  151.     start)
  152.     start_proxy
  153.     ;;
  154.     stop)
  155.     stop_proxy
  156.     ;;
  157.     restart)
  158.     restart_proxy
  159.     ;;
  160.     status)
  161.     status_proxy
  162.     ;;
  163.     *)
  164.     echo "Usage: $0 {start|stop|restart|status}"
  165. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement