run.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. # Include funcs
  13. . "$SETT_DAEMON_PATH/funcs.sh"
  14. check_util "mkdir"
  15. check_util "chmod"
  16. check_util "touch"
  17. check_util "cat"
  18. check_util "kill"
  19. check_util "rm"
  20. get_util "mkdir" UTIL_MKDIR
  21. get_util "chmod" UTIL_CHMOD
  22. get_util "touch" UTIL_TOUCH
  23. get_util "cat" UTIL_CAT
  24. get_util "kill" UTIL_KILL
  25. get_util "rm" UTIL_RM
  26. check() {
  27. if [ ! -d "$SETT_DAEMON_FOLDER_LOGS" ]; then
  28. $UTIL_MKDIR "$SETT_DAEMON_FOLDER_LOGS"
  29. $UTIL_CHMOD 0755 "$SETT_DAEMON_FOLDER_LOGS"
  30. fi
  31. }
  32. log_str() {
  33. process=$1
  34. shift
  35. log_date=$(date '+%F %T');
  36. log_string="[$log_date] [$$]: $*"
  37. echo "$log_string" >> "$SETT_DAEMON_LOGS_WORK_FILE"
  38. }
  39. loop() {
  40. # Include user scripts
  41. if [ -d "$SETT_DAEMON_PATH/scripts" ]; then
  42. for user_script in $SETT_DAEMON_PATH/scripts/*.sh
  43. do
  44. . $user_script
  45. done
  46. fi
  47. }
  48. start() {
  49. check
  50. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  51. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  52. is_pid_runned "$_pid" rbool
  53. if [ "$rbool" = "1" ]; then
  54. log_str "0" "Daemon already running with pid = $_pid"
  55. echo "Daemon already running with pid = $_pid"
  56. exit 0
  57. fi
  58. fi
  59. cd /
  60. if [ "$SETT_DAEMON_MODE" = 1 ]; then
  61. exec >> $SETT_DAEMON_LOGS_WORK_FILE
  62. exec 2>> $SETT_DAEMON_LOGS_WORK_FILE
  63. exec < /dev/null
  64. (
  65. while [ 1 ]
  66. do
  67. loop
  68. sleep 1
  69. done
  70. exit 0
  71. )&
  72. echo $! > $SETT_DAEMON_PID_FILE
  73. else
  74. while [ 1 ]
  75. do
  76. loop
  77. sleep 1
  78. done
  79. exit 0
  80. fi
  81. }
  82. stop() {
  83. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  84. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  85. `$UTIL_KILL $_pid`
  86. rt="$?"
  87. if [ "$rt" = "0" ]; then
  88. log_str "0" "Daemon stoped"
  89. echo "Daemon stoped"
  90. `$UTIL_RM $SETT_DAEMON_PID_FILE`
  91. else
  92. log_str "0" "Error stop daemon"
  93. echo "Error stop daemon"
  94. fi
  95. else
  96. log_str "0" "Daemon is not runned"
  97. echo "Daemon is not runned"
  98. fi
  99. }
  100. status() {
  101. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  102. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  103. is_pid_runned "$_pid" rbool
  104. if [ "$rbool" = "1" ]; then
  105. echo "Daemon is runned with pid = $_pid"
  106. else
  107. echo "Daemon is not runned"
  108. fi
  109. else
  110. echo "Daemon is not runned"
  111. fi
  112. }
  113. usage() {
  114. echo "$0 (once|start|stop|status)"
  115. }
  116. case $1 in
  117. "once")
  118. SETT_DAEMON_MODE=0
  119. start
  120. ;;
  121. "start")
  122. start
  123. ;;
  124. "stop")
  125. stop
  126. ;;
  127. "status")
  128. status
  129. ;;
  130. *)
  131. usage
  132. ;;
  133. esac
  134. exit