This is an old revision of the document!
Sysvinit Service Script
General Information
General system V init service script for process control.
Checklist
- Linux distro with SysVInit init system.
Implementation
- Download code.
- Modify application variables section.
- Make executable
chmod +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 chkconfig
chkconfig --add /etc/rc.d/init.d/myservice
The Code
- sysvinit-service
#!/bin/sh # SysV-Init: 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-progname.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 ## 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 "Error: Must have root privileges or be the ${prog_user} user." exit 4 fi # Check if exec file exists and is executable if [[ -x ${prog_root}/${prog_script} ]];then continue else echo "Error: ${prog_root}/${prog_script} is not executable or does not exist." 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 "Error: config file, ${prog_config}, not found!" exit 6 fi # See if process is already running. check_isrunning if [[ ${isrunning} == "0" ]];then echo "${prog} is already running." 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 "Nothing to stop...${prog} is not running." 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 "${prog} is running...found ${process_count} occurence(s)." echo "PID COMMAND" pgrep -l -u ${prog_user} -f ${pgrep_pattern} exit 0 else echo "${prog} is not running." 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}