uwsgi-init.d 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env bash
  2. ### BEGIN INIT INFO
  3. # Provides: emperor
  4. # Required-Start: $all
  5. # Required-Stop: $all
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: uwsgi for wagtail
  9. # Description: uwsgi for wagtail
  10. ### END INIT INFO
  11. set -e
  12. PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
  13. DAEMON=/usr/local/bin/uwsgi
  14. RUN=/var/run/uwsgi
  15. CONFIG_DIR=/etc/uwsgi/vassals
  16. NAME=uwsgi
  17. DESC=emperor
  18. OWNER=root
  19. GROUP=root
  20. OP=$1
  21. [[ -x $DAEMON ]] || exit 0
  22. [[ -d $RUN ]] || mkdir $RUN && chown $OWNER.$GROUP $RUN
  23. do_pid_check()
  24. {
  25. local PIDFILE=$1
  26. [[ -f $PIDFILE ]] || return 0
  27. local PID=$(cat $PIDFILE)
  28. for p in $(pgrep $NAME); do
  29. [[ $p == $PID ]] && return 1
  30. done
  31. return 0
  32. }
  33. do_start()
  34. {
  35. local PIDFILE=$RUN/$NAME.pid
  36. local START_OPTS=" \
  37. --emperor $CONFIG_DIR \
  38. --pidfile $PIDFILE \
  39. --uid $OWNER --gid $GROUP \
  40. --daemonize /var/log/$NAME/uwsgi-emperor.log"
  41. if do_pid_check $PIDFILE; then
  42. $NAME $START_OPTS
  43. else
  44. echo "Already running!"
  45. fi
  46. }
  47. send_sig()
  48. {
  49. local PIDFILE=$RUN/$NAME.pid
  50. set +e
  51. [[ -f $PIDFILE ]] && kill $1 $(cat $PIDFILE) > /dev/null 2>&1
  52. set -e
  53. }
  54. wait_and_clean_pidfile()
  55. {
  56. local PIDFILE=$RUN/uwsgi.pid
  57. until do_pid_check $PIDFILE; do
  58. echo -n "";
  59. done
  60. rm -f $PIDFILE
  61. }
  62. do_stop()
  63. {
  64. send_sig -3
  65. wait_and_clean_pidfile
  66. }
  67. do_reload()
  68. {
  69. send_sig -1
  70. }
  71. case "$OP" in
  72. start)
  73. echo "Starting $DESC: "
  74. do_start
  75. echo "$NAME."
  76. ;;
  77. stop)
  78. echo -n "Stopping $DESC: "
  79. do_stop
  80. echo "$NAME."
  81. ;;
  82. reload)
  83. echo -n "Reloading $DESC: "
  84. do_reload
  85. echo "$NAME."
  86. ;;
  87. restart)
  88. echo "Restarting $DESC: "
  89. do_stop
  90. sleep 1
  91. do_start
  92. echo "$NAME."
  93. ;;
  94. *)
  95. N=/etc/init.d/$NAME
  96. echo "Usage: $N {start|stop|restart|reload}">&2
  97. exit 1
  98. ;;
  99. esac
  100. exit 0