run.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 body
  41. . "$SETT_DAEMON_PATH/loop.sh"
  42. }
  43. start() {
  44. check
  45. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  46. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  47. is_pid_runned "$_pid" rbool
  48. if [ "$rbool" = "1" ]; then
  49. log_str "0" "Daemon already running with pid = $_pid"
  50. echo "Daemon already running with pid = $_pid"
  51. exit 0
  52. fi
  53. fi
  54. cd /
  55. if [ "$SETT_DAEMON_MODE" = 1 ]; then
  56. exec >> $SETT_DAEMON_LOGS_WORK_FILE
  57. exec 2>> $SETT_DAEMON_LOGS_WORK_FILE
  58. exec < /dev/null
  59. (
  60. while [ 1 ]
  61. do
  62. loop
  63. sleep 1
  64. done
  65. exit 0
  66. )&
  67. echo $! > $SETT_DAEMON_PID_FILE
  68. else
  69. while [ 1 ]
  70. do
  71. loop
  72. sleep 1
  73. done
  74. exit 0
  75. fi
  76. }
  77. stop() {
  78. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  79. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  80. `$UTIL_KILL $_pid`
  81. rt="$?"
  82. if [ "$rt" = "0" ]; then
  83. log_str "0" "Daemon stoped"
  84. echo "Daemon stoped"
  85. `$UTIL_RM $SETT_DAEMON_PID_FILE`
  86. else
  87. log_str "0" "Error stop daemon"
  88. echo "Error stop daemon"
  89. fi
  90. else
  91. log_str "0" "Daemon is not runned"
  92. echo "Daemon is not runned"
  93. fi
  94. }
  95. status() {
  96. if [ -f "$SETT_DAEMON_PID_FILE" ]; then
  97. _pid=`$UTIL_CAT $SETT_DAEMON_PID_FILE`
  98. is_pid_runned "$_pid" rbool
  99. if [ "$rbool" = "1" ]; then
  100. echo "Daemon is runned with pid = $_pid"
  101. else
  102. echo "Daemon is not runned"
  103. fi
  104. else
  105. echo "Daemon is not runned"
  106. fi
  107. }
  108. usage() {
  109. echo "$0 (once|start|stop|status)"
  110. }
  111. case $1 in
  112. "once")
  113. SETT_DAEMON_MODE=0
  114. start
  115. ;;
  116. "start")
  117. start
  118. ;;
  119. "stop")
  120. stop
  121. ;;
  122. "status")
  123. status
  124. ;;
  125. *)
  126. usage
  127. ;;
  128. esac
  129. exit