home/.config/dtfscripts/common_service.sh

78 lines
1.7 KiB
Bash
Raw Normal View History

#! /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"
service_kill_on_reload="true"
service_flags=""
# Basic logging service. Do not override unless necessary
function svc_log() {
if [ -z ${1+x} ]; then return 1; fi
dtf_log "$service_name: $1"
}
# Basic pre-start checks. Stick extra checks in prestart-extra
function prestart() {
if pgrep $service_process > /dev/null 2>&1; then
svc_log "Already running"
return 1
fi
if ! which $service_process > /dev/null 2>&1; then
svc_log "Could not find process"
return 1
fi
return 0
}
# Template function. If it fails, the service will not be started
function prestart-extra() {
return 0
}
# Basic start function. Override if you have special startup functionality
function start() {
svc_log "Starting"
$service_process $service_flags > /dev/null 2>&1 &
return 0
}
# 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
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 $?
}
# Basic process detection function. Returns 0 if the process exists.
function isup() {
pgrep "$service_process" > /dev/null 2>&1
isup-extra $?
return $?
}
# Template function. Gets passed the result of a simple pgrep for the process
function isup-extra() {
return $1
}