linux_wiki:sysvinit_service_script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
linux_wiki:sysvinit_service_script [2015/08/19 22:11]
billdozor created
linux_wiki:sysvinit_service_script [2019/05/25 23:50] (current)
Line 6: Line 6:
  
 **Checklist** **Checklist**
-  * Linux distro with SysVInit init system.+  * Distro(s): Enterprise Linux 5/6
  
 ---- ----
Line 24: Line 24:
 <code bash sysvinit-service> <code bash sysvinit-service>
 #!/bin/sh #!/bin/sh
-SysV-Init: Generic sysvinit script+Title: Generic sysvinit script
 # Description: General service script to control a process with sysvinit # 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 # Check Config => Starts on run levels 2,3,4,5. Start Priority => 80, Stop Priority => 20
 # chkconfig: 2345 80 20 # chkconfig: 2345 80 20
-# 
  
-## Application Variables - Customize Here ##+#### Application Variables - Customize Here #### 
 +# The service name
 prog=service-name prog=service-name
 +
 +# User that will run the service
 prog_user=service-username prog_user=service-username
 prog_uid=$(id -u ${prog_user}) prog_uid=$(id -u ${prog_user})
 +
 +# Directory the program is located in and the start script name
 prog_root=/home/${prog_user} prog_root=/home/${prog_user}
-prog_start=start.sh +prog_script=run-program-name.sh 
-prog_exec=${prog_root}/${prog_start}+ 
 +# Location of config file and startup log file
 prog_config=${prog_root}/settings.conf prog_config=${prog_root}/settings.conf
 prog_startuplog=${prog_root}/startup.log prog_startuplog=${prog_root}/startup.log
 +
 +# Process grep pattern to determine if it is running
 pgrep_pattern=process-name pgrep_pattern=process-name
 +
 +# How long to give the program to startup (in seconds),before checking status
 prog_sleep=10 prog_sleep=10
-## End of Application Variables ##+#### End of Application Variables #### 
 + 
 +# Use init.d functions for echo status 
 +source /etc/init.d/functions
  
 check_required() { check_required() {
Line 48: Line 60:
    continue    continue
  else  else
-   echo "Error: Must have root privileges or be the ${prog_user} user."+   echo -e "Error: Must have root privileges or be the ${prog_user} user.\c" 
 +          echo_failure
    exit 4    exit 4
  fi  fi
   
  # Check if exec file exists and is executable  # Check if exec file exists and is executable
- if [[ -x ${prog_exec} ]];then+ if [[ -x ${prog_root}/${prog_script} ]];then
    continue    continue
  else  else
-   echo "Error: ${prog_exec} is not executable or does not exist."+   echo -e "Error: ${prog_root}/${prog_start} is not executable or does not exist.\c" 
 +          echo_failure
    exit 5    exit 5
  fi  fi
Line 70: Line 84:
         # If config file is missing, exit with error         # If config file is missing, exit with error
  if [[ ! -f ${prog_config} ]];then  if [[ ! -f ${prog_config} ]];then
-   echo "Error: config file, ${prog_config}, not found!"+   echo -e "Error: config file, ${prog_config}, not found!\c" 
 +          echo_failure 
 +          echo
    exit 6    exit 6
  fi  fi
  
  # See if process is already running.  # See if process is already running.
 + check_isrunning
  if [[ ${isrunning} == "0" ]];then  if [[ ${isrunning} == "0" ]];then
-   echo "${prog} is already running." +   echo -e "${prog} is already running.\c" 
-   RETVAL=0+          echo_passed 
 +          echo 
 +   exit 0
  else  else
    # Start program in the background and dettach from terminal    # Start program in the background and dettach from terminal
-   echo "Starting ${prog}..." +   echo "Starting ${prog}...(startup log: ${prog_startuplog})
-   su - ${prog_user} -c "cd ${prog_root}; nohup ./${prog_start} &> ${prog_startuplog} &"+   su - ${prog_user} -c "cd ${prog_root}; nohup ./${prog_script} &> ${prog_startuplog} &"
   
    # Give the program time to startup    # Give the program time to startup
Line 87: Line 106:
    sleep ${prog_sleep}    sleep ${prog_sleep}
  
-   # Update isrunning variable, then check status+   # Check status
    echo -e "\n"    echo -e "\n"
-   check_isrunning 
    status    status
  fi  fi
Line 96: Line 114:
 stop() { stop() {
  # See if process is running.  # See if process is running.
 + check_isrunning
  if [[ ${isrunning} == "0" ]];then  if [[ ${isrunning} == "0" ]];then
    # Terminate PID    # Terminate PID
    echo "Stopping ${prog}..."    echo "Stopping ${prog}..."
    kill -15 $(pgrep -u ${prog_user} -f ${pgrep_pattern})    kill -15 $(pgrep -u ${prog_user} -f ${pgrep_pattern})
-   RETVAL=$?+   exit_status=$?
    sleep 5    sleep 5
  else   else
-   echo "Nothing to stop...${prog} is not running." +   echo -e "Nothing to stop...${prog} is not running.\c" 
-   RETVAL=0+          echo_passed 
 +          echo 
 +   exit 0
  fi  fi
 } }
Line 110: Line 131:
 restart() { restart() {
  stop  stop
- check_isrunning 
  start  start
 } }
Line 116: Line 136:
 status() { status() {
  # Check process status  # Check process status
 + check_isrunning
  if [[ ${isrunning} == "0" ]];then  if [[ ${isrunning} == "0" ]];then
    process_count=$(pgrep -l -u ${prog_user} -f ${pgrep_pattern} | wc -l)    process_count=$(pgrep -l -u ${prog_user} -f ${pgrep_pattern} | wc -l)
-   echo "${prog} is running...found ${process_count} occurence(s)."+   echo -e "${prog} is running...found ${process_count} occurence(s).\c" 
 +          echo_success 
 +          echo
    echo "PID  COMMAND"    echo "PID  COMMAND"
    pgrep -l -u ${prog_user} -f ${pgrep_pattern}    pgrep -l -u ${prog_user} -f ${pgrep_pattern}
-   RETVAL=0+   exit 0
  else  else
-   echo "${prog} is not running." +   echo -e "${prog} is not running.\c" 
-   RETVAL=1+          echo_failure 
 +          echo 
 +   exit 1
  fi  fi
 } }
Line 130: Line 155:
 ## Main Starts Here ## ## Main Starts Here ##
  
-# Always check required and if process is running+# Always check required
 check_required check_required
-check_isrunning 
  
 case ${1} in case ${1} in
Line 152: Line 176:
 esac esac
  
-exit ${RETVAL}+exit ${exit_status}
 </code> </code>
  
 ---- ----
  
  • linux_wiki/sysvinit_service_script.1440036691.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)