Differences
This shows you the differences between two versions of the page.
linux_wiki:sysvinit_service_script [2015/08/21 07:14] billdozor [The Code] |
linux_wiki:sysvinit_service_script [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 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< | ||
- | * Move to / | ||
- | * Add to chkconfig< | ||
- | |||
- | ---- | ||
- | |||
- | ==== The Code ==== | ||
- | |||
- | <code bash sysvinit-service> | ||
- | #!/bin/sh | ||
- | # SysV-Init: Generic sysvinit script | ||
- | # Description: | ||
- | # 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=/ | ||
- | prog_script=run-progname.sh | ||
- | |||
- | # Location of config file and startup log file | ||
- | prog_config=${prog_root}/ | ||
- | prog_startuplog=${prog_root}/ | ||
- | |||
- | # Process grep pattern to determine if it is running | ||
- | pgrep_pattern=process-name | ||
- | |||
- | # How long to give the program to startup (in seconds), | ||
- | 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 " | ||
- | exit 4 | ||
- | fi | ||
- | |||
- | # Check if exec file exists and is executable | ||
- | if [[ -x ${prog_root}/ | ||
- | continue | ||
- | else | ||
- | echo " | ||
- | 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 " | ||
- | exit 6 | ||
- | fi | ||
- | |||
- | # See if process is already running. | ||
- | if [[ ${isrunning} == " | ||
- | echo " | ||
- | RETVAL=0 | ||
- | else | ||
- | # Start program in the background and dettach from terminal | ||
- | echo " | ||
- | su - ${prog_user} -c "cd ${prog_root}; | ||
- | |||
- | # Give the program time to startup | ||
- | echo " | ||
- | sleep ${prog_sleep} | ||
- | |||
- | # Update isrunning variable, then check status | ||
- | echo -e " | ||
- | check_isrunning | ||
- | status | ||
- | fi | ||
- | } | ||
- | |||
- | stop() { | ||
- | # See if process is running. | ||
- | if [[ ${isrunning} == " | ||
- | # Terminate PID | ||
- | echo " | ||
- | kill -15 $(pgrep -u ${prog_user} -f ${pgrep_pattern}) | ||
- | RETVAL=$? | ||
- | sleep 5 | ||
- | else | ||
- | echo " | ||
- | RETVAL=0 | ||
- | fi | ||
- | } | ||
- | |||
- | restart() { | ||
- | stop | ||
- | check_isrunning | ||
- | start | ||
- | } | ||
- | |||
- | status() { | ||
- | # Check process status | ||
- | if [[ ${isrunning} == " | ||
- | process_count=$(pgrep -l -u ${prog_user} -f ${pgrep_pattern} | wc -l) | ||
- | echo " | ||
- | echo " | ||
- | pgrep -l -u ${prog_user} -f ${pgrep_pattern} | ||
- | RETVAL=0 | ||
- | else | ||
- | echo " | ||
- | RETVAL=1 | ||
- | fi | ||
- | } | ||
- | |||
- | ## Main Starts Here ## | ||
- | |||
- | # Always check required and if process is running | ||
- | check_required | ||
- | check_isrunning | ||
- | |||
- | case ${1} in | ||
- | start) | ||
- | start | ||
- | ;; | ||
- | stop) | ||
- | stop | ||
- | ;; | ||
- | restart) | ||
- | restart | ||
- | ;; | ||
- | status) | ||
- | status | ||
- | ;; | ||
- | *) | ||
- | echo $" | ||
- | exit 2 | ||
- | esac | ||
- | |||
- | exit ${RETVAL} | ||
- | </ | ||
- | |||
- | ---- | ||