#!/bin/bash
#
# chkconfig:
# description: my_SERVICE_NAME is a my Service
# # common function
. /etc/init.d/functions # service bash flag
IVMS_SERVICE= CORE_DUMP_DIR=/var/my_core
DAEMON_COREFILE_LIMIT=unlimited
SVC_SYS_DIR=/etc/init.d SVC_PROG=my_SERVICE_PROG
SVC_NAME=my_SERVICE_NAME
SVC_DIR=my_SERVICE_DIR
SVC_LOCK_FILE=/var/lock/subsys/$SVC_PROG
SVC_PID_FILE=/var/run/$SVC_NAME.pid # install service, $ service name, $ executable name
InstallSvc()
{
if [ -z "$1" ]; then
echo $"service name is needed by myservice."
return -
fi SVC_NAME=$
SVC_PROG=${:-$SVC_NAME}
SVC_DIR=`pwd`
SVC_SYS_FILE=$SVC_SYS_DIR/$SVC_NAME if [ ! -f $SVC_DIR/$SVC_PROG ]; then
echo $"$SVC_DIR/$SVC_PROG does not exist."
return -
fi echo $"installing $SVC_NAME service, executable file $SVC_DIR/$SVC_PROG ..." # set suid_dumpable on
if [ -e /proc/sys/kernel/suid_dumpable ]; then
echo > /proc/sys/kernel/suid_dumpable
else
echo > /proc/sys/fs/suid_dumpable
fi # create core directory
if [ -d $CORE_DUMP_DIR ]; then
echo $"$CORE_DUMP_DIR/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
else
mkdir $CORE_DUMP_DIR
echo $"$CORE_DUMP_DIR/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
fi # config sysctl to enable core
grep "$CORE_DUMP_DIR/core-%e-%p-%t" /etc/sysctl.conf
if [ $? -ne ]; then
echo $"fs.suid_dumpable = 1" >> /etc/sysctl.conf
echo $"kernel.core_pattern = $CORE_DUMP_DIR/core-%e-%p-%t" >> /etc/sysctl.conf
fi # create service bash
sed -e "s%my_SERVICE=0%my_SERVICE=1%g" $ | \
sed -e "s%my_SERVICE_PROG%$SVC_PROG%g" | \
sed -e "s%my_SERVICE_NAME%$SVC_NAME%g" | \
sed -e "s%my_SERVICE_DIR%$SVC_DIR%g" > $SVC_SYS_FILE chmod u+x $SVC_SYS_FILE
chmod u+x $SVC_DIR/$SVC_PROG
chkconfig --add $SVC_NAME echo $"install $SVC_NAME service successfully."
return
} # uninstall service, $ service name
UninstallSvc()
{
if [ -z "$1" ]; then
echo $"service name is needed by myservice."
return -
fi SVC_NAME=$
SVC_SYS_FILE=$SVC_SYS_DIR/$SVC_NAME echo $"uninstalling $SVC_NAME service ..." # rm service bash
if [ -f $SVC_SYS_FILE ]; then
chkconfig --del $SVC_NAME
rm -f $SVC_SYS_FILE
else
warning $"$SVC_NAME service does not exist."
fi echo $"uninstall $SVC_NAME service successfully."
return
} # start service
StartSvc()
{
echo $"starting $SVC_NAME service ..." # set core unlimited, replaced by DAEMON_COREFILE_LIMIT
#ulimit -c unlimited # create lockfile, run program
touch $SVC_LOCK_FILE
cd $SVC_DIR
daemon --pidfile=$SVC_PID_FILE $SVC_DIR/$SVC_PROG -service
if [ $? -eq ]; then
# created by the program
#pidof $SVC_DIR/$SVC_PROG > $SVC_PID_FILE
echo $"start $SVC_NAME service successfully."
return
else
echo $"start $SVC_NAME service failure."
return -
fi
} # get service status
IsSvcRunning()
{
local pid
__pids_var_run $SVC_NAME $SVC_PID_FILE
[ -n "$pid" ] && return || return
} # stop service
StopSvc()
{
echo $"stopping $SVC_NAME service ..." # remove lockfile
rm -f $SVC_LOCK_FILE # wait for exit
local i RC
for (( i = ; i < ; i++ )); do
if IsSvcRunning; then
sleep
else
break
fi
done if [ $i -eq ] && IsSvcRunning; then
killproc -p $SVC_PID_FILE $SVC_NAME
RC=$?
else
rm -f $SVC_PID_FILE
RC=
fi if [ $RC -eq ]; then
echo $"stop $SVC_NAME service successfully."
return
else
echo $"stop $SVC_NAME service failure."
return -
fi
} # restart service
RestartSvc()
{
echo $"restarting $SVC_NAME service ..." StopSvc
if [ $? -eq ]; then
StartSvc
if [ $? -eq ]; then
echo $"restart $SVC_NAME service successfully."
return
fi
fi echo $"restart $SVC_NAME service failure."
return -
} RETVAL= if [ $IVMS_SERVICE -eq ]; then
# setup
case $ in
install | i)
InstallSvc $ $
RETVAL=$?
;;
uninstall | u)
UninstallSvc $
RETVAL=$?
;;
*)
echo $"Usage: $0 {i, install NAME [EXEC] | u, uninstall NAME}"
;;
esac
else
# service
case $ in
start | r)
StartSvc
RETVAL=$?
;;
stop | p)
StopSvc
RETVAL=$?
;;
restart | e)
RestartSvc
RETVAL=$?
;;
status | s)
status -p $SVC_PID_FILE $SVC_NAME
RETVAL=$?
;;
status2)
IsSvcRunning && echo $"$SVC_NAME service is running." || echo $"$SVC_NAME service has been stopped."
;;
*)
echo $$"Usage: $0 {start|stop|status|restart}"
;;
esac
fi exit $RETVAL