Differences
This shows you the differences between two versions of the page.
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 |
---- | ---- | ||
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: | # Description: | ||
# 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=/ | prog_root=/ | ||
- | prog_start=start.sh | + | prog_script=run-program-name.sh |
- | prog_exec=${prog_root}/ | + | |
+ | # Location of config file and startup log file | ||
prog_config=${prog_root}/ | prog_config=${prog_root}/ | ||
prog_startuplog=${prog_root}/ | prog_startuplog=${prog_root}/ | ||
+ | |||
+ | # 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), | ||
prog_sleep=10 | prog_sleep=10 | ||
- | ## End of Application Variables ## | + | #### End of Application Variables #### |
+ | |||
+ | # Use init.d functions for echo status | ||
+ | source / | ||
check_required() { | check_required() { | ||
Line 48: | Line 60: | ||
continue | continue | ||
else | else | ||
- | echo " | + | echo -e " |
+ | 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}/ |
continue | continue | ||
else | else | ||
- | echo " | + | echo -e " |
+ | 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 " | + | echo -e " |
+ | 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} == " | if [[ ${isrunning} == " | ||
- | echo " | + | echo -e " |
- | | + | echo_passed |
+ | echo | ||
+ | | ||
else | else | ||
# Start program in the background and dettach from terminal | # Start program in the background and dettach from terminal | ||
- | echo " | + | echo " |
- | su - ${prog_user} -c "cd ${prog_root}; | + | su - ${prog_user} -c "cd ${prog_root}; |
# 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 " | echo -e " | ||
- | 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} == " | if [[ ${isrunning} == " | ||
# Terminate PID | # Terminate PID | ||
echo " | echo " | ||
kill -15 $(pgrep -u ${prog_user} -f ${pgrep_pattern}) | kill -15 $(pgrep -u ${prog_user} -f ${pgrep_pattern}) | ||
- | | + | |
sleep 5 | sleep 5 | ||
else | else | ||
- | echo " | + | echo -e " |
- | | + | echo_passed |
+ | echo | ||
+ | | ||
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} == " | if [[ ${isrunning} == " | ||
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 " | + | echo -e " |
+ | echo_success | ||
+ | echo | ||
echo " | echo " | ||
pgrep -l -u ${prog_user} -f ${pgrep_pattern} | pgrep -l -u ${prog_user} -f ${pgrep_pattern} | ||
- | | + | |
else | else | ||
- | echo " | + | echo -e " |
- | | + | echo_failure |
+ | echo | ||
+ | | ||
fi | fi | ||
} | } | ||
Line 130: | Line 155: | ||
## Main Starts Here ## | ## Main Starts Here ## | ||
- | # Always check required | + | # 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} |
</ | </ | ||
---- | ---- | ||