Guest User

init script for Unicorn

a guest
Dec 1st, 2013
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.94 KB | None | 0 0
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          unicorn
  4. # Required-Start:    $local_fs $remote_fs
  5. # Required-Stop:     $local_fs $remote_fs
  6. # Default-Start:     2 3 4 5
  7. # Default-Stop:      0 1 6
  8. # Short-Description: unicorn initscript
  9. # Description:       unicorn
  10. ### END INIT INFO
  11.  
  12. set -e
  13. NAME=unicorn
  14. DESC="Unicorn web server"
  15.  
  16. . /lib/lsb/init-functions
  17.  
  18. if [ -f /etc/default/unicorn ]; then
  19.   . /etc/default/unicorn
  20. fi
  21.  
  22. DAEMON=/usr/bin/unicorn
  23. PID=${PID-/run/unicorn.pid}
  24.  
  25. run_by_init() {
  26.     ([ "${previous-}" ] && [ "${runlevel-}" ]) || [ "${runlevel-}" = S ]
  27. }
  28.  
  29. exit_with_message() {
  30.   if ! run_by_init; then
  31.     log_action_msg "$1 Not starting."
  32.   fi
  33.   exit 0
  34. }
  35.  
  36. check_config() {
  37.   if [ $CONFIGURED != "yes" ]; then
  38.     exit_with_message "Unicorn is not configured (see /etc/default/unicorn)."
  39.   fi
  40. }
  41.  
  42. check_app_root() {
  43.   if ! [ -d $APP_ROOT ]; then
  44.     exit_with_message "Application directory $APP_ROOT is not exist."
  45.   fi
  46. }
  47.  
  48. set -u
  49.  
  50. case "$1" in
  51.   start)
  52.         check_config
  53.         check_app_root
  54.  
  55.         log_daemon_msg "Starting $DESC" $NAME || true
  56.         if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $DAEMON -- $UNICORN_OPTS; then
  57.           log_end_msg 0 || true
  58.         else
  59.           log_end_msg 1 || true
  60.         fi
  61.           ;;
  62.   stop)
  63.         log_daemon_msg "Stopping $DESC" $NAME || true
  64.         if start-stop-daemon --stop --signal QUIT --quiet --oknodo --pidfile $PID; then
  65.           log_end_msg 0 || true
  66.         else
  67.           log_end_msg 1 || true
  68.         fi
  69.         ;;
  70.   force-stop)
  71.         log_daemon_msg "Forcing stop of $DESC" $NAME || true
  72.         if start-stop-daemon --stop --quiet --oknodo --pidfile $PID; then
  73.           log_end_msg 0 || true
  74.         else
  75.           log_end_msg 1 || true
  76.         fi
  77.         ;;
  78.   restart|force-reload)
  79.         log_daemon_msg "Restarting $DESC" $NAME || true
  80.         start-stop-daemon --stop --quiet --oknodo --pidfile $PID
  81.         sleep 1
  82.         if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $DAEMON -- $UNICORN_OPTS; then
  83.           log_end_msg 0 || true
  84.         else
  85.           log_end_msg 1 || true
  86.         fi
  87.         ;;
  88.   reload)
  89.         log_daemon_msg "Reloading $DESC" $NAME || true
  90.         if start-stop-daemon --stop --signal HUP --quiet --oknodo --pidfile $PID; then
  91.           log_end_msg 0 || true
  92.         else
  93.           log_end_msg 1 || true
  94.         fi
  95.         ;;
  96.   reopen-logs)
  97.         log_daemon_msg "Relopening log files of $DESC" $NAME || true
  98.         if start-stop-daemon --stop --signal USR1 --quiet --oknodo --pidfile $PID; then
  99.           log_end_msg 0 || true
  100.         else
  101.           log_end_msg 1 || true
  102.         fi
  103.         ;;
  104.   status)
  105.         status_of_proc -p $PID $DAEMON $NAME && exit 0 || exit $?
  106.         ;;
  107.   *)
  108.         log_action_msg "Usage: $0 <start|stop|restart|force-reload|reload|force-stop|reopen-logs|status>" || true
  109.         exit 1
  110.         ;;
  111. esac
Advertisement
Add Comment
Please, Sign In to add comment