run.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "mkdir"
  65. check_util "chmod"
  66. check_util "touch"
  67. check_util "cat"
  68. check_util "kill"
  69. check_util "rm"
  70. check_util "wget"
  71. check_util "unzip"
  72. # Get real path of each util
  73. get_util "mkdir" UTIL_MKDIR
  74. get_util "chmod" UTIL_CHMOD
  75. get_util "touch" UTIL_TOUCH
  76. get_util "cat" UTIL_CAT
  77. get_util "kill" UTIL_KILL
  78. get_util "rm" UTIL_RM
  79. get_util "wget" UTIL_WGET
  80. get_util "unzip" UTIL_UNZIP
  81. check() {
  82. if [ ! -d "$SETT_DAEMON_FOLDER_LOGS" ]; then
  83. $UTIL_MKDIR "$SETT_DAEMON_FOLDER_LOGS"
  84. $UTIL_CHMOD 0755 "$SETT_DAEMON_FOLDER_LOGS"
  85. fi
  86. }
  87. log_str() {
  88. process=$1
  89. shift
  90. log_date=$(date '+%F %T');
  91. log_string="[$log_date] [$$]: $*"
  92. echo "$log_string" >> "$SETT_DAEMON_LOGS_WORK_FILE"
  93. }
  94. loop() {
  95. # Include user scripts
  96. if [ -d "$SETT_DAEMON_PATH/scripts" ]; then
  97. for user_script in $SETT_DAEMON_PATH/scripts/*.sh
  98. do
  99. . $user_script
  100. done
  101. fi
  102. }
  103. start() {
  104. check
  105. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  106. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  107. is_pid_runned "$_pid" rbool
  108. if [ "$rbool" = "1" ]; then
  109. log_str "0" "Daemon already running with pid = $_pid"
  110. echo "Daemon already running with pid = $_pid"
  111. exit 0
  112. fi
  113. fi
  114. cd /
  115. if [ "$SETT_DAEMON_MODE" = 1 ]; then
  116. exec >> $SETT_DAEMON_LOGS_WORK_FILE
  117. exec 2>> $SETT_DAEMON_LOGS_WORK_FILE
  118. exec < /dev/null
  119. (
  120. while [ 1 ]
  121. do
  122. loop
  123. sleep 1
  124. done
  125. exit 0
  126. )&
  127. echo $! > $SETT_DAEMON_PID_FILE
  128. else
  129. while [ 1 ]
  130. do
  131. loop
  132. sleep 1
  133. done
  134. exit 0
  135. fi
  136. }
  137. stop() {
  138. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  139. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  140. `$UTIL_KILL $_pid`
  141. rt="$?"
  142. if [ "$rt" = "0" ]; then
  143. log_str "0" "Daemon stoped"
  144. echo "Daemon stoped"
  145. `$UTIL_RM $SETT_DAEMON_PID_FILE`
  146. else
  147. log_str "0" "Error stop daemon"
  148. echo "Error stop daemon"
  149. fi
  150. else
  151. log_str "0" "Daemon is not runned"
  152. echo "Daemon is not runned"
  153. fi
  154. }
  155. status() {
  156. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  157. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  158. is_pid_runned "$_pid" rbool
  159. if [ "$rbool" = "1" ]; then
  160. echo "Daemon is runned with pid = $_pid"
  161. else
  162. echo "Daemon is not runned"
  163. fi
  164. else
  165. echo "Daemon is not runned"
  166. fi
  167. }
  168. update() {
  169. echo "Update..."
  170. }
  171. usage() {
  172. echo "$0 (once|start|stop|status)"
  173. }
  174. case $1 in
  175. "once")
  176. SETT_DAEMON_MODE=0
  177. start
  178. ;;
  179. "start")
  180. start
  181. ;;
  182. "stop")
  183. stop
  184. ;;
  185. "status")
  186. status
  187. ;;
  188. "update")
  189. update
  190. ;;
  191. *)
  192. usage
  193. ;;
  194. esac
  195. exit