linux_wiki:sysvinit_service_script

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
linux_wiki:sysvinit_service_script [2015/08/21 07:14]
billdozor [The Code]
linux_wiki:sysvinit_service_script [2015/10/01 22:58]
billdozor [The Code]
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 # The service name
 prog=service-name prog=service-name
Line 40: Line 39:
 # Directory the program is located in and the start script name # Directory the program is located in and the start script name
 prog_root=/home/${prog_user} prog_root=/home/${prog_user}
-prog_script=run-progname.sh+prog_script=run-program-name.sh
  
 # Location of config file and startup log file # Location of config file and startup log file
Line 51: Line 50:
 # How long to give the program to startup (in seconds),before checking status # 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 ####
  
 check_required() { check_required() {
Line 58: Line 57:
    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
Line 66: Line 66:
    continue    continue
  else  else
-   echo "Error: ${prog_root}/${prog_script} 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 80: Line 81:
         # 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_script} &> ${prog_startuplog} &"    su - ${prog_user} -c "cd ${prog_root}; nohup ./${prog_script} &> ${prog_startuplog} &"
   
Line 97: Line 103:
    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 106: Line 111:
 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 120: Line 128:
 restart() { restart() {
  stop  stop
- check_isrunning 
  start  start
 } }
Line 126: Line 133:
 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 140: Line 152:
 ## 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 162: Line 173:
 esac esac
  
-exit ${RETVAL}+exit ${exit_status}
 </code> </code>
  
 ---- ----
  
  • linux_wiki/sysvinit_service_script.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)