run.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. # Additional funcs
  13. check_util() {
  14. util_name="$1"
  15. check=`whereis $util_name`
  16. if [ "$check" = "" ]; then
  17. echo "Error: '$util_name' is not found... Fix error and try again!"
  18. exit
  19. fi
  20. }
  21. get_util() {
  22. util_name="$1"
  23. IS_MAC_OS=`uname -a | grep Darwin`
  24. if [ "$IS_MAC_OS" != "" ]; then
  25. # Mac OS X
  26. resp=`whereis $util_name | awk {'print $1'}`
  27. eval "$2='$resp'"
  28. return
  29. else
  30. # Linux
  31. resp=`whereis $util_name | awk {'print $2'}`
  32. eval "$2='$resp'"
  33. return
  34. fi
  35. eval "$2=''"
  36. return
  37. }
  38. is_pid_runned() {
  39. resp=`ps aux | grep "start" | grep $1`
  40. if [ "$resp" != "" ]; then
  41. eval "$2='1'"
  42. else
  43. eval "$2='0'"
  44. fi
  45. return
  46. }
  47. # Check utils
  48. check_util "mkdir"
  49. check_util "chmod"
  50. check_util "touch"
  51. check_util "cat"
  52. check_util "kill"
  53. check_util "rm"
  54. # Get real path of each util
  55. get_util "mkdir" UTIL_MKDIR
  56. get_util "chmod" UTIL_CHMOD
  57. get_util "touch" UTIL_TOUCH
  58. get_util "cat" UTIL_CAT
  59. get_util "kill" UTIL_KILL
  60. get_util "rm" UTIL_RM
  61. check() {
  62. if [ ! -d "$SETT_DAEMON_FOLDER_LOGS" ]; then
  63. $UTIL_MKDIR "$SETT_DAEMON_FOLDER_LOGS"
  64. $UTIL_CHMOD 0755 "$SETT_DAEMON_FOLDER_LOGS"
  65. fi
  66. }
  67. log_str() {
  68. process=$1
  69. shift
  70. log_date=$(date '+%F %T');
  71. log_string="[$log_date] [$$]: $*"
  72. echo "$log_string" >> "$SETT_DAEMON_LOGS_WORK_FILE"
  73. }
  74. loop() {
  75. # Include user scripts
  76. if [ -d "$SETT_DAEMON_PATH/scripts" ]; then
  77. for user_script in $SETT_DAEMON_PATH/scripts/*.sh
  78. do
  79. . $user_script
  80. done
  81. fi
  82. }
  83. start() {
  84. check
  85. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  86. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  87. is_pid_runned "$_pid" rbool
  88. if [ "$rbool" = "1" ]; then
  89. log_str "0" "Daemon already running with pid = $_pid"
  90. echo "Daemon already running with pid = $_pid"
  91. exit 0
  92. fi
  93. fi
  94. cd /
  95. if [ "$SETT_DAEMON_MODE" = 1 ]; then
  96. exec >> $SETT_DAEMON_LOGS_WORK_FILE
  97. exec 2>> $SETT_DAEMON_LOGS_WORK_FILE
  98. exec < /dev/null
  99. (
  100. while [ 1 ]
  101. do
  102. loop
  103. sleep 1
  104. done
  105. exit 0
  106. )&
  107. echo $! > $SETT_DAEMON_PID_FILE
  108. else
  109. while [ 1 ]
  110. do
  111. loop
  112. sleep 1
  113. done
  114. exit 0
  115. fi
  116. }
  117. stop() {
  118. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  119. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  120. `$UTIL_KILL $_pid`
  121. rt="$?"
  122. if [ "$rt" = "0" ]; then
  123. log_str "0" "Daemon stoped"
  124. echo "Daemon stoped"
  125. `$UTIL_RM $SETT_DAEMON_PID_FILE`
  126. else
  127. log_str "0" "Error stop daemon"
  128. echo "Error stop daemon"
  129. fi
  130. else
  131. log_str "0" "Daemon is not runned"
  132. echo "Daemon is not runned"
  133. fi
  134. }
  135. status() {
  136. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  137. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  138. is_pid_runned "$_pid" rbool
  139. if [ "$rbool" = "1" ]; then
  140. echo "Daemon is runned with pid = $_pid"
  141. else
  142. echo "Daemon is not runned"
  143. fi
  144. else
  145. echo "Daemon is not runned"
  146. fi
  147. }
  148. usage() {
  149. echo "$0 (once|start|stop|status)"
  150. }
  151. case $1 in
  152. "once")
  153. SETT_DAEMON_MODE=0
  154. start
  155. ;;
  156. "start")
  157. start
  158. ;;
  159. "stop")
  160. stop
  161. ;;
  162. "status")
  163. status
  164. ;;
  165. *)
  166. usage
  167. ;;
  168. esac
  169. exit