peertube 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/sh
  2. APP_NAME="peertube"
  3. USER="peertube"
  4. GROUP="peertube"
  5. NODE_ENV="production"
  6. APP_DIR="/var/www/peertube/peertube-latest"
  7. NODE_APP="dist/server"
  8. KWARGS=""
  9. CONFIG_DIR="/var/www/peertube/config"
  10. PID_DIR="$APP_DIR/pid"
  11. PID_FILE="$PID_DIR/$APP_NAME.pid"
  12. LOG_DIR="/var/log/peertube"
  13. LOG_FILE="$LOG_DIR/$APP_NAME.log"
  14. NODE_EXEC=$(which node)
  15. ###############
  16. # REDHAT chkconfig header
  17. # chkconfig: - 58 74
  18. # description: peertube service script.
  19. ### BEGIN INIT INFO
  20. # Provides: peertube
  21. # Required-Start: $network $remote_fs $local_fs
  22. # Required-Stop: $network $remote_fs $local_fs
  23. # Default-Start: 2 3 4 5
  24. # Default-Stop: 0 1 6
  25. # Short-Description: start and stop peertube
  26. # Description: Node process for peertube
  27. ### END INIT INFO
  28. ###############
  29. USAGE="Usage: $0 {start|stop|restart|status} [--force]"
  30. FORCE_OP=false
  31. pid_file_exists() {
  32. [ -f "$PID_FILE" ]
  33. }
  34. get_pid() {
  35. echo "$(cat "$PID_FILE")"
  36. }
  37. is_running() {
  38. PID="$(get_pid)"
  39. [ -d /proc/$PID ]
  40. }
  41. start_it() {
  42. mkdir -p "$PID_DIR"
  43. chown $USER:$GROUP "$PID_DIR"
  44. mkdir -p "$LOG_DIR"
  45. chown $USER:$GROUP "$LOG_DIR"
  46. echo "Starting $APP_NAME ..."
  47. echo "cd $APP_DIR && NODE_ENV=$NODE_ENV NODE_CONFIG_DIR=$CONFIG_DIR $NODE_EXEC $APP_DIR/$NODE_APP $KWARGS 1>$LOG_FILE 2>&1 & echo \$! > $PID_FILE" | sudo -i -u $USER
  48. echo "$APP_NAME started with pid $(get_pid)"
  49. }
  50. stop_process() {
  51. PID=$(get_pid)
  52. echo "Killing process $PID"
  53. pkill -P $PID
  54. }
  55. remove_pid_file() {
  56. echo "Removing pid file"
  57. rm -f "$PID_FILE"
  58. }
  59. start_app() {
  60. if pid_file_exists
  61. then
  62. if is_running
  63. then
  64. PID=$(get_pid)
  65. echo "$APP_NAME already running with pid $PID"
  66. exit 1
  67. else
  68. echo "$APP_NAME stopped, but pid file exists"
  69. if [ $FORCE_OP = true ]
  70. then
  71. echo "Forcing start anyways"
  72. remove_pid_file
  73. start_it
  74. fi
  75. fi
  76. else
  77. start_it
  78. fi
  79. }
  80. stop_app() {
  81. if pid_file_exists
  82. then
  83. if is_running
  84. then
  85. echo "Stopping $APP_NAME ..."
  86. stop_process
  87. remove_pid_file
  88. echo "$APP_NAME stopped"
  89. else
  90. echo "$APP_NAME already stopped, but pid file exists"
  91. if [ $FORCE_OP = true ]
  92. then
  93. echo "Forcing stop anyways ..."
  94. remove_pid_file
  95. echo "$APP_NAME stopped"
  96. else
  97. exit 1
  98. fi
  99. fi
  100. else
  101. echo "$APP_NAME already stopped, pid file does not exist"
  102. exit 1
  103. fi
  104. }
  105. status_app() {
  106. if pid_file_exists
  107. then
  108. if is_running
  109. then
  110. PID=$(get_pid)
  111. echo "$APP_NAME running with pid $PID"
  112. else
  113. echo "$APP_NAME stopped, but pid file exists"
  114. fi
  115. else
  116. echo "$APP_NAME stopped"
  117. fi
  118. }
  119. case "$2" in
  120. --force)
  121. FORCE_OP=true
  122. ;;
  123. "")
  124. ;;
  125. *)
  126. echo $USAGE
  127. exit 1
  128. ;;
  129. esac
  130. case "$1" in
  131. start)
  132. start_app
  133. ;;
  134. stop)
  135. stop_app
  136. ;;
  137. restart)
  138. stop_app
  139. start_app
  140. ;;
  141. status)
  142. status_app
  143. ;;
  144. *)
  145. echo $USAGE
  146. exit 1
  147. ;;
  148. esac