====== Sysvinit Service Script ======
**General Information**
General system V init service script for process control.
**Checklist**
* Distro(s): Enterprise Linux 5/6
----
===== Implementation =====
* Download code.
* Modify application variables section.
* Make executablechmod +x sysvinit-service
* Move to /etc/rc.d/init.d/ (and rename to the service's name)mv sysvinit-service /etc/rc.d/init.d/myservice
* Add to chkconfigchkconfig --add /etc/rc.d/init.d/myservice
----
==== The Code ====
#!/bin/sh
# Title: Generic sysvinit script
# Description: General service script to control a process with sysvinit
# Check Config => Starts on run levels 2,3,4,5. Start Priority => 80, Stop Priority => 20
# chkconfig: 2345 80 20
#### Application Variables - Customize Here ####
# The service name
prog=service-name
# User that will run the service
prog_user=service-username
prog_uid=$(id -u ${prog_user})
# Directory the program is located in and the start script name
prog_root=/home/${prog_user}
prog_script=run-program-name.sh
# Location of config file and startup log file
prog_config=${prog_root}/settings.conf
prog_startuplog=${prog_root}/startup.log
# Process grep pattern to determine if it is running
pgrep_pattern=process-name
# How long to give the program to startup (in seconds),before checking status
prog_sleep=10
#### End of Application Variables ####
# Use init.d functions for echo status
source /etc/init.d/functions
check_required() {
# Check that we're a privileged user or the application user
if [[ $(id -u) = 0 || $(id -u) = ${prog_uid} ]]; then
continue
else
echo -e "Error: Must have root privileges or be the ${prog_user} user.\c"
echo_failure
exit 4
fi
# Check if exec file exists and is executable
if [[ -x ${prog_root}/${prog_script} ]];then
continue
else
echo -e "Error: ${prog_root}/${prog_start} is not executable or does not exist.\c"
echo_failure
exit 5
fi
}
check_isrunning() {
# Check to see if process is running: 0 = yes, 1 = no
process=$(pgrep -l -u ${prog_user} -f ${pgrep_pattern})
isrunning=$?
}
start() {
# If config file is missing, exit with error
if [[ ! -f ${prog_config} ]];then
echo -e "Error: config file, ${prog_config}, not found!\c"
echo_failure
echo
exit 6
fi
# See if process is already running.
check_isrunning
if [[ ${isrunning} == "0" ]];then
echo -e "${prog} is already running.\c"
echo_passed
echo
exit 0
else
# Start program in the background and dettach from terminal
echo "Starting ${prog}...(startup log: ${prog_startuplog})"
su - ${prog_user} -c "cd ${prog_root}; nohup ./${prog_script} &> ${prog_startuplog} &"
# Give the program time to startup
echo "Giving ${prog} ${prog_sleep} seconds to start, will then output status..."
sleep ${prog_sleep}
# Check status
echo -e "\n"
status
fi
}
stop() {
# See if process is running.
check_isrunning
if [[ ${isrunning} == "0" ]];then
# Terminate PID
echo "Stopping ${prog}..."
kill -15 $(pgrep -u ${prog_user} -f ${pgrep_pattern})
exit_status=$?
sleep 5
else
echo -e "Nothing to stop...${prog} is not running.\c"
echo_passed
echo
exit 0
fi
}
restart() {
stop
start
}
status() {
# Check process status
check_isrunning
if [[ ${isrunning} == "0" ]];then
process_count=$(pgrep -l -u ${prog_user} -f ${pgrep_pattern} | wc -l)
echo -e "${prog} is running...found ${process_count} occurence(s).\c"
echo_success
echo
echo "PID COMMAND"
pgrep -l -u ${prog_user} -f ${pgrep_pattern}
exit 0
else
echo -e "${prog} is not running.\c"
echo_failure
echo
exit 1
fi
}
## Main Starts Here ##
# Always check required
check_required
case ${1} in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo $"Usage: ${prog} {start|stop|restart|status}"
exit 2
esac
exit ${exit_status}
----