Advertisement
srchulo

catalyst nginx fastcgi /etc/init.d script

May 30th, 2013
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.41 KB | None | 0 0
  1.  #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          application-catalyst-foo
  4. # Required-Start:    $local_fs $network
  5. # Required-Stop:     $local_fs $network
  6. # Default-Start:     2 3 4 5
  7. # Default-Stop:      0 1 6
  8. # Short-Description: Starts the FastCGI app server for the "Myapp" site
  9. # Description:       The FastCGI application server for the "Myapp" site
  10. ### END INIT INFO
  11.  
  12. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  13. SITE_HOME=/home/user/myapp
  14. DAEMON=$SITE_HOME/script/myapp_fastcgi.pl
  15. NAME=myapp_fastcgi
  16. DESC="'myapp' app server"
  17. USER=user
  18.  
  19. #new stuff
  20. MAX_PORT=8999
  21. MIN_PORT=8000
  22. PORT_FILE=/root/ports/$NAME.port
  23. NGINX_CONF=/etc/nginx/nginx.conf
  24. NGINX_PID=`cat /var/run/nginx.pid`
  25.  
  26. test -f $DAEMON || exit 0
  27.  
  28. #get port
  29. if [[ -f $PORT_FILE ]]; then
  30.     LAST_PORT=`cat $PORT_FILE`
  31.     PORT_TO_USE=$(($LAST_PORT + 1))
  32. else
  33.     echo "Must create $PORT_FILE with current port used for FastCGI in $NGINX_CONF"
  34.     echo "Port must be between $MIN_PORT and $MAX_PORT"
  35.     exit
  36. fi
  37.  
  38. # Reset so me don't go up forever
  39. if [[ $PORT_TO_USE -gt $MAX_PORT ]]; then
  40.     PORT_TO_USE=$MIN_PORT
  41. fi
  42.  
  43. OPTS="-l :$PORT_TO_USE -n 6"
  44. NEW_PID_FILE="/var/run/$NAME.$PORT_TO_USE.pid"
  45. OLD_PID_FILE="/var/run/$NAME.$LAST_PORT.pid"
  46.  
  47. if [[ -f $OLD_PID_FILE ]]; then
  48.     OLD_PID=`cat $OLD_PID_FILE`
  49. fi
  50.  
  51. set -e
  52.  
  53. start_daemon()
  54. {
  55.  echo "Starting $DESC: $NAME"
  56.  
  57.  if [[ ! -f $OLD_PID_FILE || $RELOAD -eq 1 ]]; then
  58.     echo "IN HERE DAWG"
  59.     sed -i "s/$LAST_PORT/$PORT_TO_USE/g" $NGINX_CONF
  60.  
  61.     start-stop-daemon --start --quiet --pidfile $NEW_PID_FILE -d $SITE_HOME \
  62.     --exec /usr/bin/perl --startas $DAEMON --chuid $USER --background --make-pid -- $OPTS
  63.  
  64.     echo $PORT_TO_USE > $PORT_FILE
  65.  fi
  66.  
  67.  if [[ $RELOAD -eq 1 ]]; then
  68.     sleep 5
  69.  fi
  70.  
  71.  echo "Reloading nginx with pid $NGINX_PID."
  72.  kill -HUP $NGINX_PID
  73. }
  74.  
  75.  
  76. stop_daemon()
  77. {
  78.  echo -n "Stopping $DESC: "
  79.  if [[ -f $OLD_PID_FILE ]]; then
  80.     start-stop-daemon --stop --signal TERM --pidfile $OLD_PID_FILE
  81.     rm $OLD_PID_FILE
  82.  fi
  83.  echo "$NAME."
  84. }
  85.  
  86. reload_daemon()
  87. {
  88.  RELOAD=1
  89.  echo "Reloading $DESC: $NAME."
  90.  start_daemon
  91.  stop_daemon
  92. }
  93.  
  94. case "$1" in
  95.  start)
  96.  start_daemon
  97.  ;;
  98.  stop)
  99.  stop_daemon
  100.  ;;
  101.  reload)
  102.  reload_daemon
  103.  ;;
  104.  restart|force-reload)
  105.  stop_daemon
  106.  sleep 5
  107.  start_daemon
  108.  ;;
  109.  *)
  110.  N=/etc/init.d/$NAME
  111.  echo "Usage: $N {start|stop|reload|restart|force-reload}" >&2
  112.  exit 1
  113.  ;;
  114. esac
  115.  
  116. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement