|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | + |
| 4 | + |
| 5 | +# it's a service! |
| 6 | +# chkconfig: 2345 95 15 |
| 7 | +# description: PHP Listeners. |
| 8 | +### BEGIN INIT INFO |
| 9 | +# Provides: php_listeners |
| 10 | +# Required-Start: $all |
| 11 | +# Required-Stop: $all |
| 12 | +# Default-Start: 2 3 4 5 |
| 13 | +# Default-Stop: 0 1 6 |
| 14 | +# Short-Description: Start the php listeners. |
| 15 | +# Description: Start the php listeners. |
| 16 | +### END INIT INFO |
| 17 | + |
| 18 | + |
| 19 | +START_STOP_WAIT=5 |
| 20 | +# change the lines below to match your use case |
| 21 | +# a user that can read and write to the LISTENER_DIR |
| 22 | +LISTENER_USER="microservices" |
| 23 | +# the directory where the listeners are installed |
| 24 | +LISTENER_DIR="/opt/php_listeners" |
| 25 | + |
| 26 | +PID_SEARCH="pgrep -nf php.listener.php -u $LISTENER_USER" |
| 27 | + |
| 28 | + |
| 29 | +run_this_one() { |
| 30 | + /bin/su - $LISTENER_USER --shell=/bin/bash --command="$*" |
| 31 | +} |
| 32 | + |
| 33 | +start() { |
| 34 | + if [ `$PID_SEARCH` ]; then |
| 35 | + echo "PHP_Listeners already running! Try running with \"force-start\" if you know for certain it is not running." |
| 36 | + else |
| 37 | + force_start |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +force_start() { |
| 42 | + echo "> Starting..." |
| 43 | + run_this_one "$LISTENER_DIR/listener.sh $LISTENER_DIR" |
| 44 | + echo "Waiting for server startup to complete" |
| 45 | + sleep 2 |
| 46 | + startwait=$START_STOP_WAIT |
| 47 | + count=0 |
| 48 | + while [ $count -lt $startwait ]; do |
| 49 | + echo -n "." |
| 50 | + sleep 1 |
| 51 | + count=`expr $count + 1` |
| 52 | + PID=`$PID_SEARCH` |
| 53 | + if [ ! $PID = "" ]; then |
| 54 | + break |
| 55 | + fi |
| 56 | + done |
| 57 | + ps -p $PID &>/dev/null 2>/dev/null |
| 58 | + RETVAL=$? |
| 59 | + if [ $RETVAL = 0 ]; then |
| 60 | + echo "" |
| 61 | + echo "Listeners have started..." |
| 62 | + return 0 |
| 63 | + else |
| 64 | + echo "" |
| 65 | + echo "Listeners failed to start... Check your logs" |
| 66 | + return 1 |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +stop() |
| 71 | +{ |
| 72 | + PID=`$PID_SEARCH` |
| 73 | + if [ $PID ]; then |
| 74 | + echo "> Stopping..." |
| 75 | + kill $PID |
| 76 | + echo "Waiting for server shutdown to complete" |
| 77 | + sleep 2 |
| 78 | + kwait=$START_STOP_WAIT |
| 79 | + count=0; |
| 80 | + while [ $count -lt $kwait ]; do |
| 81 | + sleep 1 |
| 82 | + count=`expr $count + 1` |
| 83 | + PID=`$PID_SEARCH` |
| 84 | + if [ ! $PID = "" ]; then |
| 85 | + echo -n "." |
| 86 | + else |
| 87 | + break |
| 88 | + fi |
| 89 | + done |
| 90 | + echo "" |
| 91 | + if [ $count -eq $kwait ]; then |
| 92 | + echo "process is still running after $START_STOP_WAIT seconds, killing process" |
| 93 | + PID=`$PID_SEARCH` |
| 94 | + kill $PID |
| 95 | + sleep 5 |
| 96 | + |
| 97 | + # if it’s still running use kill -9 |
| 98 | + PID=`$PID_SEARCH` |
| 99 | + if [ $PID ]; then |
| 100 | + echo "process is still running, using kill -9" |
| 101 | + kill -9 $PID |
| 102 | + sleep 5 |
| 103 | + fi |
| 104 | + fi |
| 105 | + PID=`$PID_SEARCH` |
| 106 | + if [ $PID ]; then |
| 107 | + echo "process is still running, I give up" |
| 108 | + return 1 |
| 109 | + else |
| 110 | + echo "Listeners shutdown completed" |
| 111 | + return 0 |
| 112 | + fi |
| 113 | + else |
| 114 | + echo "Listeners are not currently running, you can start them with $0 start" |
| 115 | + fi |
| 116 | +} |
| 117 | + |
| 118 | +status() |
| 119 | +{ |
| 120 | + PID=`$PID_SEARCH` |
| 121 | + |
| 122 | + if [ $PID ]; then |
| 123 | + echo "Listeners are up and running with PID="$PID |
| 124 | + return 0 |
| 125 | + else |
| 126 | + echo "Listeners are not currently running, you can start them with $0 start" |
| 127 | + return 1 |
| 128 | + fi |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | +case "$1" in |
| 133 | + start) start ;; |
| 134 | + force-start) force_start ;; |
| 135 | + stop) stop ;; |
| 136 | + restart) stop ; start ;; |
| 137 | + status) status ;; |
| 138 | + *) |
| 139 | + echo "Usage: `basename $0` {start|stop|status|restart|force-start}" |
| 140 | + exit 2 |
| 141 | + ;; |
| 142 | +esac |
| 143 | + |
| 144 | +exit $? |
0 commit comments