Created
May 12, 2013 18:10
-
-
Save pdeschen/5564411 to your computer and use it in GitHub Desktop.
CentOS service initd erb template. Ready to use in Opscode Chef or replace template variables with your own hard-coded value. Takes into account priority/nice, sudo user, and /etc/sysconfig/ Support start, stop, restart, console.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# <%=@service%> This shell script takes care of starting and stopping | |
# the <%=@service%> service. | |
# | |
# chkconfig: 2345 65 35 | |
# description: <%=@description%> | |
# | |
SERVICE=<%=@service%> | |
# Priority at which to run the service. See "man nice" for valid priorities. | |
# nice is only used if a priority is specified. | |
PRIORITY=<%=@priority%> | |
# User under which server will run | |
USERNAME=<%=@username%> | |
CMD=<%=@cmd%> | |
# used for starting service in console mode | |
CMD_CONSOLE=<%=@cmd_console%> | |
# Some global variables | |
export LANG=en_US | |
ARCH=`arch` | |
# Location of the pid file. | |
PIDDIR="/var/run/$SERVICE" | |
if [ ! -e $PIDDIR ]; then | |
mkdir -p $PIDDIR | |
fi | |
# Allow configuration overrides in /etc/sysconfig/$SERVICE | |
CONFIGFILE=/etc/sysconfig/$SERVICE | |
[ -x $CONFIGFILE ] && . $CONFIGFILE | |
# Get the fully qualified path to the script | |
case $0 in | |
/*) | |
SCRIPT="$0" | |
;; | |
*) | |
PWD=`pwd` | |
SCRIPT="$PWD/$0" | |
;; | |
esac | |
# Change spaces to ":" so the tokens can be parsed. | |
SCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'` | |
# Get the real path to this script, resolving any symbolic links | |
TOKENS=`echo $SCRIPT | sed -e 's;/; ;g'` | |
REALPATH= | |
for C in $TOKENS; do | |
REALPATH="$REALPATH/$C" | |
while [ -h "$REALPATH" ] ; do | |
LS="`ls -ld "$REALPATH"`" | |
LINK="`expr "$LS" : '.*-> \(.*\)$'`" | |
if expr "$LINK" : '/.*' > /dev/null; then | |
REALPATH="$LINK" | |
else | |
REALPATH="`dirname "$REALPATH"`""/$LINK" | |
fi | |
done | |
done | |
# Change ":" chars back to spaces. | |
REALPATH=`echo $REALPATH | sed -e 's;:; ;g'` | |
# Change the current directory to the location of the script | |
cd "`dirname "$REALPATH"`" | |
chown $USERNAME $PIDDIR | |
# Process ID | |
PIDFILE="$PIDDIR/$SERVICE.pid" | |
pid="" | |
# Resolve the location of the 'ps' command | |
PSEXE="/usr/bin/ps" | |
if [ ! -x $PSEXE ] | |
then | |
PSEXE="/bin/ps" | |
if [ ! -x $PSEXE ] | |
then | |
echo "Unable to locate 'ps'." | |
echo "Please report this with the location on your system." | |
exit 1 | |
fi | |
fi | |
# Build the nice clause | |
if [ "X$PRIORITY" = "X" ] | |
then | |
CMDNICE="" | |
else | |
CMDNICE="nice -$PRIORITY" | |
fi | |
getpid() { | |
if [ -f $PIDFILE ] | |
then | |
if [ -r $PIDFILE ] | |
then | |
pid=`cat $PIDFILE` | |
if [ "X$pid" != "X" ] | |
then | |
# Verify that a process with this pid is still running. | |
pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1` | |
if [ "X$pid" = "X" ] | |
then | |
# This is a stale pid file. | |
rm -f $PIDFILE | |
echo "Removed stale pid file: $PIDFILE" | |
fi | |
fi | |
else | |
echo "Cannot read $PIDFILE." | |
exit 1 | |
fi | |
fi | |
} | |
testpid() { | |
pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1` | |
if [ "X$pid" = "X" ] | |
then | |
# Process is gone so remove the pid file. | |
rm -f $PIDFILE | |
fi | |
} | |
console() { | |
echo "Running $SERVICE..." | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
exec su --preserve-environment --command="$CMDNICE $CMD_CONSOLE" $USERNAME | |
else | |
echo "$SERICE is already running." | |
exit 1 | |
fi | |
} | |
start() { | |
echo "Starting $SERVICE..." | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
exec su --preserve-environment --command="$CMDNICE $CMD" $USERNAME | |
else | |
echo "$SERVICE is already running." | |
exit 1 | |
fi | |
} | |
stopit() { | |
echo "Stopping $SERVICE..." | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
echo "$SERVICE was not running." | |
else | |
# Running so try to stop it. | |
su --command="kill $pid" $USERNAME | |
if [ $? -ne 0 ] | |
then | |
# An explanation for the failure should have been given | |
echo "Unable to stop $SERVICE." | |
exit 1 | |
fi | |
# Loop until it does. | |
savepid=$pid | |
CNT=0 | |
TOTCNT=0 | |
while [ "X$pid" != "X" ] | |
do | |
# Loop for up to 5 minutes | |
if [ "$TOTCNT" -lt "300" ] | |
then | |
if [ "$CNT" -lt "5" ] | |
then | |
CNT=`expr $CNT + 1` | |
else | |
echo "Waiting for $SERVICE to exit..." | |
CNT=0 | |
fi | |
TOTCNT=`expr $TOTCNT + 1` | |
sleep 1 | |
testpid | |
else | |
pid= | |
fi | |
done | |
pid=$savepid | |
testpid | |
if [ "X$pid" != "X" ] | |
then | |
echo "Timed out waiting for $SERVICE to exit." | |
echo " Attempting a forced exit..." | |
kill -9 $pid | |
fi | |
pid=$savepid | |
testpid | |
if [ "X$pid" != "X" ] | |
then | |
echo "Failed to stop $SERVICE." | |
exit 1 | |
else | |
echo "Stopped $SERVICE." | |
fi | |
fi | |
} | |
status() { | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
echo "$SERVICE is not running." | |
exit 1 | |
else | |
echo "$SERVICE is running ($pid)." | |
exit 0 | |
fi | |
} | |
case "$1" in | |
'console') | |
console | |
;; | |
'start') | |
start | |
;; | |
'stop') | |
stopit | |
;; | |
'restart') | |
stopit | |
start | |
;; | |
'status') | |
status | |
;; | |
*) | |
echo "Usage: $0 { console | start | stop | restart | status }" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@m-menon Yes, but init.d reads it as plain texts, and sees it, then parses it. It doesn't completely ignore commented text, only the bash interpreter does that.