2018-08-26 16:11:25 -05:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
#
|
|
|
|
# wmstartup service common functions
|
|
|
|
# Copyright (C) 2018 salt <salt@lap-th-e560-0>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
#
|
|
|
|
|
|
|
|
service_name="unnamed_service"
|
|
|
|
service_process="true"
|
2018-08-27 11:51:06 -05:00
|
|
|
service_kill_on_reload="true"
|
|
|
|
service_flags=""
|
2018-08-26 16:11:25 -05:00
|
|
|
|
2018-08-27 13:09:40 -05:00
|
|
|
# Basic logging service. Do not override unless necessary
|
2018-08-26 16:11:25 -05:00
|
|
|
function svc_log() {
|
|
|
|
if [ -z ${1+x} ]; then return 1; fi
|
2018-09-27 20:23:15 -05:00
|
|
|
out=1
|
|
|
|
if ! [ -z ${2+x} ]; then out="$2"; fi
|
|
|
|
col_reset="\e[0m"
|
|
|
|
col_svcname="\e[94m"
|
|
|
|
col_message="\e[39m"
|
|
|
|
if ! [ "$out" -eq "1" ]; then col_message="\e[31m"; fi
|
|
|
|
if [ "$out" -eq "0" ]; then
|
|
|
|
out=1
|
|
|
|
col_message="\e[37m"
|
|
|
|
fi
|
|
|
|
dtf_log "${col_svcname}${service_name}${col_reset}: ${col_message}${1}${col_reset}" >&${out}
|
2018-08-26 16:11:25 -05:00
|
|
|
}
|
|
|
|
|
2018-08-27 13:09:40 -05:00
|
|
|
# Basic pre-start checks. Stick extra checks in prestart-extra
|
2018-08-26 16:11:25 -05:00
|
|
|
function prestart() {
|
2018-09-27 20:23:39 -05:00
|
|
|
if isup; then
|
2018-09-27 20:23:15 -05:00
|
|
|
svc_log "Already running" 0
|
2018-08-27 11:51:06 -05:00
|
|
|
return 1
|
|
|
|
fi
|
2018-08-26 16:11:25 -05:00
|
|
|
if ! which $service_process > /dev/null 2>&1; then
|
2018-09-27 20:23:15 -05:00
|
|
|
svc_log "Could not find associated binary: \"${service_process}\"" 2
|
2018-08-26 16:11:25 -05:00
|
|
|
return 1
|
|
|
|
fi
|
2018-08-27 11:51:06 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:09:40 -05:00
|
|
|
# Template function. If it fails, the service will not be started
|
2018-08-27 11:51:06 -05:00
|
|
|
function prestart-extra() {
|
2018-08-26 16:11:25 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:09:40 -05:00
|
|
|
# Basic start function. Override if you have special startup functionality
|
2018-08-26 16:11:25 -05:00
|
|
|
function start() {
|
|
|
|
svc_log "Starting"
|
2018-08-27 11:51:06 -05:00
|
|
|
$service_process $service_flags > /dev/null 2>&1 &
|
2018-08-26 16:11:25 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:09:40 -05:00
|
|
|
# Template function. Override for post-startup tasks
|
|
|
|
# Can also be used for blocking
|
|
|
|
function start-extra() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Basic stop function. Kills the task and implements a SIGKILL timer
|
2018-08-26 16:11:25 -05:00
|
|
|
function stop() {
|
|
|
|
svc_log "Stopping"
|
|
|
|
killall $service_process &
|
|
|
|
for i in {1..100}; do
|
|
|
|
if ! isup; then break; fi
|
|
|
|
sleep 0.01
|
|
|
|
if (( $i > 99 )); then
|
|
|
|
svc_log "Stopping with prejudice"
|
|
|
|
killall -9 $service_process
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:09:40 -05:00
|
|
|
# Basic process detection function. Returns 0 if the process exists.
|
2018-08-26 16:11:25 -05:00
|
|
|
function isup() {
|
|
|
|
pgrep "$service_process" > /dev/null 2>&1
|
2018-08-27 11:51:06 -05:00
|
|
|
isup-extra $?
|
2018-08-26 16:11:25 -05:00
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:09:40 -05:00
|
|
|
# Template function. Gets passed the result of a simple pgrep for the process
|
2018-08-27 11:51:06 -05:00
|
|
|
function isup-extra() {
|
|
|
|
return $1
|
|
|
|
}
|
|
|
|
|