Changeset - 9eb26b78a711
[Not reviewed]
0 0 3
Branko Majic (branko) - 4 years ago 2020-06-29 04:16:48
branko@majic.rs
Added the Emacs templates for init scripts for Debian/Gentoo init scripts and a template for bash scripts.
3 files changed with 418 insertions and 0 deletions:
0 comments (0 inline, 0 general)
templates/TEMPLATE.sh.tpl
Show inline comments
 
new file 100644
 
#!/bin/bash
 
#
 
# (>>>FILE<<<)
 
#
 
# Copyright (C) (>>>YEAR<<<), Branko Majic <branko@majic.rs>
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU General Public License for more details.
 
#
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 
program="(>>>FILE<<<)"
 
version="(>>>VERSION<<<)"
 

	
 
function usage() {
 
    cat <<EOF
 
$program $version, (>>>SHORT_DESC<<<)
 

	
 
Usage: $program [OPTIONS] (>>>ARGUMENTS<<<)
 

	
 
$program (>>>LONG_DESC<<<)
 

	
 
$program accepts the following options:
 

	
 
    -v        show script version and licensing information
 
    -h        show usage help
 

	
 

	
 
Please report bugs and send feature requests to <branko@majic.rs>.
 
EOF
 
}
 

	
 
function version() {
 
        cat <<EOF
 
$program, version $version
 

	
 
+-----------------------------------------------------------------------+
 
| Copyright (C) (>>>YEAR<<<), Branko Majic <branko@majic.rs>                    |
 
|                                                                       |
 
| This program is free software: you can redistribute it and/or modify  |
 
| it under the terms of the GNU General Public License as published by  |
 
| the Free Software Foundation, either version 3 of the License, or     |
 
| (at your option) any later version.                                   |
 
|                                                                       |
 
| This program is distributed in the hope that it will be useful,       |
 
| but WITHOUT ANY WARRANTY; without even the implied warranty of        |
 
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
 
| GNU General Public License for more details.                          |
 
|                                                                       |
 
| You should have received a copy of the GNU General Public License     |
 
| along with this program.  If not, see <http://www.gnu.org/licenses/>. |
 
+-----------------------------------------------------------------------+
 

	
 
EOF
 
}
 

	
 
# If no arguments were given, just show usage help.
 
if [[ -z $1 ]]; then
 
    usage
 
    exit 0
 
fi
 

	
 
# Parse the arguments
 
while getopts "vh" opt; do
 
    case "$opt" in
 
        v) version
 
           exit 0;;
 
        h) usage
 
           exit 0;;
 
        *) usage
 
           exit 1;;
 
    esac
 
done
 
i=$OPTIND
 
shift $(($i-1))
 

	
templates/init.d_debian.tpl
Show inline comments
 
new file 100644
 
#!/bin/bash
 

	
 
### BEGIN INIT INFO
 
# Provides:          (>>>PROVIDES<<<)
 
# Required-Start:    (>>>REQ_START<<<)
 
# Required-Stop:     (>>>REQ_STOP<<<)
 
# Default-Start:     (>>>DEFAULT_START<<<)
 
# Default-Stop:      (>>>DEFAULT_STOP<<<)
 
# Short-Description: (>>>SHORT_DESCRIPTION<<<)
 
# Description:       (>>>DESCRIPTION<<<)
 
### END INIT INFO
 

	
 

	
 
# Include the LSB library functions
 
. /lib/lsb/init-functions
 

	
 

	
 
# Setup static variables
 
configFile='(>>>CONFIG_FILE<<<)'
 
daemonExec='(>>>DAEMON_EXEC<<<)'
 
daemonArgs='(>>>DAEMON_ARGS<<<)'
 
daemonName="$(basename "$daemonExec")"
 
pidFile='/var/run/(>>>FILE<<<).pid'
 

	
 

	
 
#
 
# Checks if the environment is capable of running the script (such as
 
# availability of programs etc).
 
#
 
# Return: 0 if the environmnt is properly setup for execution of init script, 1
 
#         if not all conditions have been met.
 
#
 
function checkEnvironment() {
 
    # Verify that the necessary binaries are available for execution.
 
    local binaries=((>>>REQ_BINARIES<<<))
 

	
 
    for bin in "${binaries[@]}"; do
 
        if ! which "$bin" > /dev/null; then
 
            log_failure_msg "Binary '$bin' is not available. Please install \
 
package containing it."
 
            exit 5
 
        fi
 
    done
 

	
 
    # Verify that the necessary environment variables have been set.
 
    local envVars=((>>>REQ_ENV_VARIABLES<<<))
 
    
 
    for var in "${envVars[@]}"; do
 
        if printenv "$var" > /dev/null; then
 
            log_failure_msg "Environment variable '$var' is not set. Please \
 
check your global environment settings."
 
            exit 6
 
        fi
 
    done
 
}
 

	
 

	
 
#
 
# Checks if the configuration files are available and properly setup.
 
#
 
# Return: 0 if irssid if properly configured, 1 otherwise.
 
#
 
function checkConfig() {
 
    # Make sure the configuration file has been created
 
    if ! [[ -f $configFile ]]; then
 
        log_failure_msg "Please populate the configuration file '$configFile' \
 
before running."
 
        exit 6
 
    fi
 

	
 
    # Make sure the required options have been set
 
    local reqOptions=((>>>REQ_OPTIONS<<<))
 
    for option in "${reqOptions[@]}"; do
 
        if ! grep -q -e "^[[:blank:]]*$option=" "$configFile"; then
 
            log_failure_msg "Mandatory option '$option' was not specified in \
 
'$configFile'"
 
            exit 6
 
        fi
 
    done
 
}
 

	
 

	
 
#
 
# Loads the configuration file and performs any additional configuration steps.
 
#
 
function configure() {
 
    . "$configFile"
 
    daemonCommand="$daemonExec $daemonArgs"
 
}
 

	
 

	
 
#
 
# Checks if the daemon is already running or not. If the daemon is running, its
 
# process id is stored within variable 'pid'.
 
#
 
# Return: 0 if the daemon is running, 1 otherwise.
 
#
 
function checkRunning() {
 
    for pid in $(pidofproc "$daemonExec"); do
 
        ps $pid | grep -qe "$daemonCommand" && return 0
 
    done
 
    return 1
 
}
 

	
 

	
 
#
 
# Starts the daemon.
 
#
 
# Return: LSB-compliant code.
 
#
 
function start() {
 
    start-stop-daemon --start --quiet --oknodo --pidfile "$pidFile" \
 
        --make-pidfile --exec "$daemonExec" -- $daemonArgs
 
}
 

	
 

	
 
#
 
# Stops the daemon.
 
#
 
# Return: LSB-compliant code.
 
#
 
function stop() {
 
    start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile "$pidFile" \
 
        --exec "$daemonExec" -- $daemonArgs
 
}
 

	
 

	
 
#
 
# Checks the status of the running daemon.
 
#
 
# Return: LSB-compliant status code.
 
#
 
function status() {
 
    status_of_proc -p "$pidFile" "$daemonExec" "$(basename $daemonExec)"
 
}
 

	
 

	
 
#
 
# Starts the daemon using custom code.
 
#
 
# Return: LSB-compliant code.
 
#
 
function startCustom() {
 
    if checkRunning; then
 
        return 0
 
    elif start-stop-daemon --start --quiet --pidfile $pidFile --make-pidfile \
 
        --exec "$daemonExec" -- $daemonArgs; then
 
        return 0
 
    else
 
        return 1
 
    fi
 
}
 

	
 

	
 
#
 
# Stops the daemon using custom code.
 
#
 
# Return: LSB-compliant code.
 
#
 
function stopCustom() {
 
    if checkRunning; then
 
        # Try to stop the daemon using SIGTERM first
 
        count=0
 
        while checkRunning && (( $count < 5 )); do
 
            kill $pid
 
            let count=count+1
 
            sleep 1
 
        done
 

	
 
        # If SIGTERM fails, use SIGKILL
 
        if checkRunning; then
 
            kill -9 $pid
 
            rm '/var/run/(>>>FILE<<<).pid'
 
            return 0
 
        else
 
            rm '/var/run/(>>>FILE<<<).pid'
 
            return 0
 
        fi 
 
    else
 
        return 0
 
    fi
 
}
 

	
 

	
 
#
 
# Checks the status of the running daemon.
 
#
 
# Return: LSB-compliant status code.
 
#
 
function statusCustom() {
 
    if checkRunning; then
 
        log_success_msg "irssid ($pid) is running"
 
        return 0
 
    else
 
        log_success_msg "irssid is not running"
 
        return 3
 
    fi
 
}
 

	
 

	
 
checkEnvironment
 
checkConfig
 
configure
 

	
 
case "$1" in
 
    start)
 
        log_daemon_msg "Starting daemon" "(>>>FILE<<<)"
 
        start && log_end_msg 0 || log_end_msg $?
 
        ;;
 
    stop)
 
        log_daemon_msg "Stopping daemon" "(>>>FILE<<<)"
 
        stop && log_end_msg 0 || log_end_msg $?
 
        ;;
 
    restart)
 
        log_daemon_msg "Restarting daemon" "(>>>FILE<<<)"
 
        stop
 
        start && log_end_msg 0 || log_end_msg $?
 
        ;;
 
    force-reload)
 
        log_daemon_msg "Restarting daemon" "(>>>FILE<<<)"
 
        stop
 
        start && log_end_msg 0 || log_end_msg $?
 
        ;;
 
    status)
 
        status && exit 0 || exit $?
 
        ;;
 
    *)
 
        echo "(>>>FILE<<<) (start|stop|restart|force-reload|status|help)"
 
        ;;
 
esac
 

	
templates/init.d_gentoo.tpl
Show inline comments
 
new file 100644
 
#!/sbin/runscript
 

	
 

	
 
# Setup static variables
 
configFile='(>>>CONFIG_FILE<<<)'
 
daemonExec='(>>>DAEMON_EXEC<<<)'
 
daemonArgs='(>>>DAEMON_ARGS<<<)'
 
daemonName="$(basename "$daemonExec")"
 
pidFile='/var/run/(>>>FILE<<<).pid'
 

	
 

	
 
#
 
# Checks if the environment is capable of running the script (such as
 
# availability of programs etc).
 
#
 
# Return: 0 if the environmnt is properly setup for execution of init script, 1
 
#         if not all conditions have been met.
 
#
 
function checkEnvironment() {
 
    # Verify that the necessary binaries are available for execution.
 
    local binaries=((>>>REQ_BINARIES<<<))
 

	
 
    for bin in "${binaries[@]}"; do
 
        if ! which "$bin" > /dev/null; then
 
            eerror "Binary '$bin' is not available. Please install \
 
package containing it."
 
            exit 5
 
        fi
 
    done
 

	
 
    # Verify that the necessary environment variables have been set.
 
    local envVars=((>>>REQ_ENV_VARIABLES<<<))
 
    
 
    for var in "${envVars[@]}"; do
 
        if printenv "$var" > /dev/null; then
 
            eerror "Environment variable '$var' is not set. Please \
 
check your global environment settings."
 
            exit 6
 
        fi
 
    done
 
}
 

	
 

	
 
#
 
# Checks if the configuration files are available and properly setup.
 
#
 
# Return: 0 if irssid if properly configured, 1 otherwise.
 
#
 
function checkConfig() {
 
    # Make sure the configuration file has been created
 
    if ! [[ -f $configFile ]]; then
 
        eerror "Please populate the configuration file '$configFile' \
 
before running."
 
        exit 6
 
    fi
 

	
 
    # Make sure the required options have been set
 
    local reqOptions=((>>>REQ_OPTIONS<<<))
 
    for option in "${reqOptions[@]}"; do
 
        if ! grep -q -e "^[[:blank:]]*$option=" "$configFile"; then
 
            eerror "Mandatory option '$option' was not specified in \
 
'$configFile'"
 
            exit 6
 
        fi
 
    done
 
}
 

	
 

	
 
#
 
# Loads the configuration file and performs any additional configuration steps.
 
#
 
function configure() {
 
    . "$configFile"
 
    daemonCommand="$daemonExec $daemonArgs"
 
}
 

	
 

	
 
#############################
 
# Gentoo-specific functions #
 
#############################
 

	
 
opts="${opts} (>>>GENTOO_OPTS<<<)"
 

	
 
depend() {
 
    need (>>>GENTOO_NEED<<<)
 
    after (>>>GENTOO_AFTER<<<)
 
}
 

	
 
start () {
 
    ebegin "Starting irssid"
 
    start-stop-daemon --start --quiet --oknodo --pidfile "$pidFile" \
 
        --make-pidfile --exec "$daemonExec" -- $daemonArgs
 
    eend $?
 
}
 

	
 
stop () {
 
    ebegin "Stopping irssid"
 
    start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile "$pidFile" \
 
        --exec "$daemonExec" -- $daemonArgs
 
    eend $?
 
}
 

	
0 comments (0 inline, 0 general)