run.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #!/bin/bash
  2. SETT_DAEMON_MODE=1
  3. SETT_DAEMON_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
  4. # Get real directory if symlink used
  5. if [ -L "${BASH_SOURCE[0]}" ]; then
  6. realfile=`readlink -f "${BASH_SOURCE[0]}"`
  7. SETT_DAEMON_PATH="$(dirname $realfile)"
  8. fi
  9. SETT_DAEMON_FOLDER_LOGS="$SETT_DAEMON_PATH/logs"
  10. SETT_DAEMON_LOGS_WORK_FILE="$SETT_DAEMON_FOLDER_LOGS/all.log"
  11. SETT_DAEMON_PID_FILE="$SETT_DAEMON_PATH/pid"
  12. SETT_OS_TYPE="linux"
  13. # Additional funcs
  14. get_os() {
  15. OS_TYPE=`uname -a | grep Darwin`
  16. if [ "$OS_TYPE" != "" ]; then
  17. eval "$1='mac'"
  18. else
  19. eval "$1='linux'"
  20. fi
  21. }
  22. check_util() {
  23. util_name="$1"
  24. # Skip for mac
  25. if [ "$util_name" = "wget" ] && [ "$SETT_OS_TYPE" = "mac" ]; then
  26. return
  27. fi
  28. check=`whereis $util_name`
  29. if [ "$check" = "" ]; then
  30. echo "Error: '$util_name' is not found... Fix error and try again!"
  31. exit
  32. fi
  33. }
  34. get_util() {
  35. util_name="$1"
  36. if [ "$util_name" = "wget" ] && [ "$SETT_OS_TYPE" = "mac" ]; then
  37. eval "$2='wget'"
  38. return
  39. fi
  40. if [ "$SETT_OS_TYPE" = "mac" ]; then
  41. resp=`whereis $util_name | awk {'print $1'}`
  42. eval "$2='$resp'"
  43. return
  44. else
  45. resp=`whereis $util_name | awk {'print $2'}`
  46. eval "$2='$resp'"
  47. return
  48. fi
  49. eval "$2=''"
  50. return
  51. }
  52. is_pid_runned() {
  53. resp=`ps aux | grep "start" | grep $1`
  54. if [ "$resp" != "" ]; then
  55. eval "$2='1'"
  56. else
  57. eval "$2='0'"
  58. fi
  59. return
  60. }
  61. # Get OS type
  62. get_os SETT_OS_TYPE
  63. # Check utils
  64. check_util "cd"
  65. check_util "cp"
  66. check_util "mkdir"
  67. check_util "chmod"
  68. check_util "touch"
  69. check_util "cat"
  70. check_util "kill"
  71. check_util "rm"
  72. check_util "wget"
  73. check_util "unzip"
  74. # Get real path of each util
  75. get_util "cd" UTIL_CD
  76. get_util "cp" UTIL_CP
  77. get_util "mkdir" UTIL_MKDIR
  78. get_util "chmod" UTIL_CHMOD
  79. get_util "touch" UTIL_TOUCH
  80. get_util "cat" UTIL_CAT
  81. get_util "kill" UTIL_KILL
  82. get_util "rm" UTIL_RM
  83. get_util "wget" UTIL_WGET
  84. get_util "unzip" UTIL_UNZIP
  85. check() {
  86. if [ ! -d "$SETT_DAEMON_FOLDER_LOGS" ]; then
  87. $UTIL_MKDIR "$SETT_DAEMON_FOLDER_LOGS"
  88. $UTIL_CHMOD 0755 "$SETT_DAEMON_FOLDER_LOGS"
  89. fi
  90. }
  91. log_str() {
  92. process=$1
  93. shift
  94. log_date=$(date '+%F %T');
  95. log_string="[$log_date] [$$]: $*"
  96. echo "$log_string" >> "$SETT_DAEMON_LOGS_WORK_FILE"
  97. }
  98. loop() {
  99. # Include user scripts
  100. if [ -d "$SETT_DAEMON_PATH/scripts" ]; then
  101. for user_script in $SETT_DAEMON_PATH/scripts/*.sh
  102. do
  103. . $user_script
  104. done
  105. fi
  106. }
  107. start() {
  108. check
  109. if [ -f "$SETT_DAEMON_PATH/xyzcopy.sh" ]; then
  110. sleep 1
  111. $UTIL_RM "$SETT_DAEMON_PATH/xyzcopy.sh"
  112. fi
  113. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  114. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  115. is_pid_runned "$_pid" rbool
  116. if [ "$rbool" = "1" ]; then
  117. log_str "0" "Daemon already running with pid = $_pid"
  118. echo "Daemon already running with pid = $_pid"
  119. exit 0
  120. fi
  121. fi
  122. cd /
  123. if [ "$SETT_DAEMON_MODE" = 1 ]; then
  124. exec >> $SETT_DAEMON_LOGS_WORK_FILE
  125. exec 2>> $SETT_DAEMON_LOGS_WORK_FILE
  126. exec < /dev/null
  127. (
  128. while [ 1 ]
  129. do
  130. loop
  131. sleep 1
  132. done
  133. exit 0
  134. )&
  135. echo $! > $SETT_DAEMON_PID_FILE
  136. else
  137. while [ 1 ]
  138. do
  139. loop
  140. sleep 1
  141. done
  142. exit 0
  143. fi
  144. }
  145. stop() {
  146. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  147. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  148. `$UTIL_KILL $_pid`
  149. rt="$?"
  150. if [ "$rt" = "0" ]; then
  151. log_str "0" "Daemon stoped"
  152. echo "Daemon stoped"
  153. `$UTIL_RM $SETT_DAEMON_PID_FILE`
  154. else
  155. log_str "0" "Error stop daemon"
  156. echo "Error stop daemon"
  157. fi
  158. else
  159. log_str "0" "Daemon is not runned"
  160. echo "Daemon is not runned"
  161. fi
  162. }
  163. status() {
  164. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  165. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  166. is_pid_runned "$_pid" rbool
  167. if [ "$rbool" = "1" ]; then
  168. echo "Daemon is runned with pid = $_pid"
  169. else
  170. echo "Daemon is not runned"
  171. fi
  172. else
  173. echo "Daemon is not runned"
  174. fi
  175. }
  176. update() {
  177. # Get daemon name
  178. SETT_ITERATOR=1
  179. SETT_BUFF_NAME=""
  180. SETT_DAEMON_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
  181. while IFS='.' read -ra ARRAY; do
  182. for i in "${ARRAY[@]}"; do
  183. if [ "$SETT_ITERATOR" -lt "${#ARRAY[@]}" ]; then
  184. SETT_BUFF_NAME=$SETT_BUFF_NAME$i
  185. fi
  186. SETT_ITERATOR=$((SETT_ITERATOR+1))
  187. done
  188. done <<< "$SETT_DAEMON_NAME"
  189. if [ "$SETT_BUFF_NAME" != "" ]; then
  190. SETT_DAEMON_NAME="$SETT_BUFF_NAME"
  191. fi
  192. # Get daemon status
  193. SETT_DAEMON_STATUS=`$0 status`
  194. if [ "$SETT_DAEMON_STATUS" != "Daemon is not runned" ]; then
  195. SETT_DAEMON_STATUS="1"
  196. else
  197. SETT_DAEMON_STATUS="0"
  198. fi
  199. echo "Downloading..."
  200. log_str "0" "Downloading..."
  201. $UTIL_MKDIR "$SETT_DAEMON_PATH/update" > /dev/null
  202. $UTIL_WGET -q -O "$SETT_DAEMON_PATH/update/daemon.zip" "https://github.com/vladimirok5959/bash-empty-daemon/releases/download/latest/daemon.zip" > /dev/null
  203. echo "Extracting..."
  204. log_str "0" "Extracting..."
  205. $UTIL_UNZIP -o "$SETT_DAEMON_PATH/update/daemon.zip" -d "$SETT_DAEMON_PATH/update" > /dev/null
  206. echo "Updating..."
  207. log_str "0" "Updating..."
  208. $UTIL_CP -f "$0" "$SETT_DAEMON_PATH/xyzcopy.sh"
  209. if [ "$SETT_DAEMON_STATUS" = "1" ]; then
  210. $0 stop
  211. fi
  212. echo "Updating almost completed"
  213. log_str "0" "Updating almost completed"
  214. # Complete
  215. $SETT_DAEMON_PATH/xyzcopy.sh xyzcopy "$SETT_DAEMON_NAME" "$SETT_DAEMON_STATUS"&
  216. }
  217. xyzcopy() {
  218. if [ ! -d "$SETT_DAEMON_PATH/update" ]; then
  219. log_str "0" "Something wrong, updating not completed!"
  220. exit
  221. fi
  222. # Delay before replace
  223. sleep 1
  224. SETT_DAEMON_NAME="$2"
  225. SETT_DAEMON_STATUS="$3"
  226. log_str "0" "Replacing started..."
  227. $UTIL_CP -f "$SETT_DAEMON_PATH/update/run.sh" "$SETT_DAEMON_PATH/$SETT_DAEMON_NAME.sh"
  228. log_str "0" "Clean..."
  229. $UTIL_RM -rd "$SETT_DAEMON_PATH/update"
  230. log_str "0" "Updating completed"
  231. # Run daemon if was runned
  232. if [ "$SETT_DAEMON_STATUS" = "1" ]; then
  233. $SETT_DAEMON_PATH/$SETT_DAEMON_NAME.sh start
  234. fi
  235. }
  236. usage() {
  237. echo "$0 (once|start|stop|status)"
  238. }
  239. case $1 in
  240. "once")
  241. SETT_DAEMON_MODE=0
  242. start
  243. ;;
  244. "start")
  245. start
  246. ;;
  247. "stop")
  248. stop
  249. ;;
  250. "status")
  251. status
  252. ;;
  253. "update")
  254. update
  255. ;;
  256. "xyzcopy")
  257. xyzcopy $1 $2 $3
  258. ;;
  259. *)
  260. usage
  261. ;;
  262. esac
  263. exit