svc

Simple service scripts and examples, to be used everywhere.
git clone git://r-36.net/svc
Log | Files | Refs | LICENSE

commit 7161edc222ae1b1a0a6a36881a7a94c5cb6de149
Author: Christoph Lohmann <20h@r-36.net>
Date:   Wed, 10 Apr 2013 21:24:08 +0200

Initial commit of svc.

Diffstat:
LICENSE | 21+++++++++++++++++++++
README.md | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
bin/svc | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
svc/avail/acpid | 0
svc/avail/alsa | 33+++++++++++++++++++++++++++++++++
svc/avail/atd | 0
svc/avail/barebone | 22++++++++++++++++++++++
svc/avail/crond | 0
svc/avail/dovecot | 0
svc/avail/fancontrol | 32++++++++++++++++++++++++++++++++
svc/avail/gpsd | 0
svc/avail/hostapd | 0
svc/avail/hwclock | 15+++++++++++++++
svc/avail/input-event-daemon | 0
svc/avail/laptop-mode | 24++++++++++++++++++++++++
svc/avail/nscd | 31+++++++++++++++++++++++++++++++
svc/avail/oscam | 0
svc/avail/rfkill | 23+++++++++++++++++++++++
svc/avail/rfkilld | 0
svc/avail/sensors | 18++++++++++++++++++
svc/avail/sshd | 33+++++++++++++++++++++++++++++++++
svc/avail/syslog-ng | 0
svc/avail/wireless-regdom | 15+++++++++++++++
svc/bare.sh | 37+++++++++++++++++++++++++++++++++++++
svc/default/alsa | 6++++++
svc/default/crond | 2++
svc/default/dovecot | 2++
svc/default/fancontrol | 2++
svc/default/gpsd | 2++
svc/default/hostapd | 2++
svc/default/hwclock | 3+++
svc/default/oscam | 2++
svc/default/rfkill | 3+++
svc/default/sensors | 5+++++
svc/default/wireless-regdom | 2++
35 files changed, 499 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT/X Consortium License + +© 2012-13 Christoph Lohmann <20h@r-36.net> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md @@ -0,0 +1,52 @@ +# SVC – Service Handling + +This framework is a simple tool to handle service starting and restarting. + +The documentation is sparse due to it being in alpha stage. + +## Installation + + % chmod 755 /bin/svc + % cp -rf svc / + +## Services +### Simplest case + + % touch /svc/avail/somebin + % svc -s somebin + +This will run »somebin« in the $PATH using the script in /svc/bare.sh. + +### Some params + + % touch /svc/avail/somebin + % echo 'PARAMS="-a"' > /svc/default/somebin + % svc -s somebin + +This will run »somebin -a« in the $PATH using the script /svc/bare.sh. + +### Own script + + % cp /svc/bare.sh /svc/default/somebin + % $EDITOR /svc/default/somebin + % echo 'SOME_VAR="no"' > /svc/default/somebin + % svc -s somebin + +This will run »somebin« with the script at /svc/default/somebin using the +special variable from /svc/default/somebin. + +## svc(1) + + % svc -a # list all links in /svc/run (all activated services) + % svc -a ser # activate service »ser« to be run on startup + % svc -c # create the basic directories of svc (configure) + % svc -d ser # deactivate service »ser« + % svc -k # kill all services linked to /svc/run + % svc -k ser # kill the service »ser« + % svc -s # run all services linked to /svc/run + % svc -s ser # run service »ser« + % svc -l # list all services in /svc/avail + % svc -r ser # restart service »ser« + +Have fun! + diff --git a/bin/svc b/bin/svc @@ -0,0 +1,112 @@ +#!/bin/sh +# +# See LICENSE file for license details. +# + +BASEDIR="/svc" +BASENAME="$(basename $0)" + +usage() { + printf "usage: %s [-acdklrs] [service]\n" "$BASENAME" >&2 + exit 1 +} + +if [ $# -lt 1 ]; +then + usage +fi + +runservice() { + service="$1" + dir="$2" + cmd="$3" + + if [ $(stat -L -t "$BASEDIR/$dir/$service" | cut -d' ' -f 2) -eq 0 ]; + then + "$BASEDIR/bare.sh" "$service" "$cmd" 2>&1 >/dev/null & + else + "$BASEDIR/$dir/$service" "$cmd" 2>&1 >/dev/null & + fi +} + +doservice() { + cmd="$1" + service="$2" + + if [ -z "${service}" ]; + then + cd $BASEDIR/run + services=$(ls -1 .) + for s in ${services}; + do + runservice "$s" run "$cmd" + printf "%s: '%s' %s.\n" \ + "$BASENAME" "$s" "$cmd" + done + else + if [ -x "$BASEDIR/avail/$service" ]; + then + runservice "$service" avail "$cmd" + printf "%s: '%s' %s.\n" \ + "$BASENAME" "$service" "$cmd" + else + printf "%s: '%s' is not available or executable.\n" \ + "$BASENAME" "$service" >&2 + exit 1 + fi + fi +} + +case "$1" in + -a) + if [ $# -gt 1 ] && [ -e "$BASEDIR/avail/$2" ]; + then + ln -sf "$BASEDIR/avail/$2" "$BASEDIR/run/$2" + printf "%s: '%s' activated.\n" \ + "$BASENAME" "$2" + else + cd $BASEDIR/run + ls . + fi + ;; + -c) + [ -d "$BASEDIR" ] || mkdir -p "$BASEDIR" + [ -d "$BASEDIR/avail" ] || mkdir -p "$BASEDIR/avail" + [ -d "$BASEDIR/default" ] || mkdir -p "$BASEDIR/default" + [ -d "$BASEDIR/run" ] || mkdir -p "$BASEDIR/run" + ;; + -d) + if [ $# -gt 1 ] && [ -e "$BASEDIR/run/$2" ]; + then + rm -f "$BASEDIR/run/$2" + printf "%s: '%s' deactivated.\n" \ + "$BASENAME" "$2" + else + printf "%s: No service given or service is not marked as to run.\n" \ + "$BASENAME" >&2 + exit 1 + fi + ;; + -k|-s) + doservice "$1" "$2" + ;; + -l) + cd $BASEDIR/avail + ls . + ;; + -r) + if [ $# -gt 1 ]; + then + svc -k "$2" + svc -s "$2" + else + printf "%s: please give a service name.\n" \ + "$BASENAME" >&2 + exit 1 + fi + ;; + *) + usage + ;; +esac + diff --git a/svc/avail/acpid b/svc/avail/acpid diff --git a/svc/avail/alsa b/svc/avail/alsa @@ -0,0 +1,33 @@ +#!/bin/sh + +SERVICE="alsa" + +[ -e "/svc/default/$SERVICE" ] && . "/svc/default/$SERVICE" + +PID=$(pidof -o %PPID $BIN) + +case $1 in + -s) + [ -e "$STATEFILE" ] || alsactl $PARAMS store + alsactl $PARAMS restore + + dfiles="/sys/modules/snd_ac97_codec/parameters/power_save + /sys/modules/snd_hda_intel/parameters/power_save" + for f in dfiles; + do + if [ -e $f ]; + then + echo $POWERSAVE > $f + [ -c /dev/dsp ] && echo 1 > /dev/dsp + fi + done + ;; + -k) + [ "$SAVEVOLUME" = "yes" ] && alsactl $PARAMS store + [ "$MUTEVOLUME" = "yes" ] && amixer -q set Master 0 mute + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/avail/atd b/svc/avail/atd diff --git a/svc/avail/barebone b/svc/avail/barebone @@ -0,0 +1,22 @@ +#!/bin/sh + +SERVICE="barebone" + +[ -e "/svc/default/$SERVICE" ] && . "/svc/default/$SERVICE" + +BIN="/usr/bin/$SERVICE" + +PID=$(pidof -o %PPID $BIN) + +case $1 in + -s) + [ -z "$PID" ] && $BIN $PARAMS; + ;; + -k) + [ -n "$PID" ] && kill -9 $PID &> /dev/null; + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/avail/crond b/svc/avail/crond diff --git a/svc/avail/dovecot b/svc/avail/dovecot diff --git a/svc/avail/fancontrol b/svc/avail/fancontrol @@ -0,0 +1,32 @@ +#!/bin/sh + +set -x + +SERVICE="fancontrol" + +[ -e "/svc/default/$SERVICE" ] && . "/svc/default/$SERVICE" + +BIN="/usr/sbin/$SERVICE" + +PID=$(pidof -o %PPID -x $BIN) + +case $1 in + -s) + [ -z "$PID" ] && { + rm /run/fancontrol.pid + $BIN $PARAMS + } + ;; + -k) + [ -n "$PID" ] && { + for p in $PID; + do + kill -9 $p &> /dev/null; + done + } + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/avail/gpsd b/svc/avail/gpsd diff --git a/svc/avail/hostapd b/svc/avail/hostapd diff --git a/svc/avail/hwclock b/svc/avail/hwclock @@ -0,0 +1,15 @@ +#!/bin/sh + +. /svc/default/hwclock + +case $1 in + -s) + ;; + -k) + hwclock --adjust $PARAMS + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/avail/input-event-daemon b/svc/avail/input-event-daemon diff --git a/svc/avail/laptop-mode b/svc/avail/laptop-mode @@ -0,0 +1,24 @@ +#!/bin/sh + +SERVICE="laptop-mode" + +[ -e "/svc/default/$SERVICE" ] && . "/svc/default/$SERVICE" + +BIN="/usr/sbin/$SERVICE" + +case $1 in + -s) + [ ! -d /run/laptop-mode-tools ] && install -d /run/laptop-mode-tools + rm -f /run/laptop-mode-tools/* + touch /run/laptop-mode-tools/enabled + $BIN auto 2>&1 >/dev/null + ;; + -k) + rm -f /run/laptop-mode-tools/enabled + $BIN stop 2>&1 >/dev/null + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/avail/nscd b/svc/avail/nscd @@ -0,0 +1,31 @@ +#!/bin/sh + +SERVICE="nscd" + +BIN="/usr/sbin/$SERVICE" + +PID=$(pidof -o %PPID $BIN) + +case $1 in + -s) + if [ -z $PID ]; + then + rm -f "/run/$SERVICE.pid" 2>&1 >/dev/null + mkdir -p /run/nscd /var/db/nscd + rm -f /run/nscd/* /var/db/nscd/* 2>&1 >/dev/null + + $BIN $PARAMS; + fi + ;; + -k) + if [ -n $PID ]; + then + nscd --shutdown 2>&1 >/dev/null + rm -f "/run/$SERVICE.pid" 2>&1 >/dev/null + fi + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/avail/oscam b/svc/avail/oscam diff --git a/svc/avail/rfkill b/svc/avail/rfkill @@ -0,0 +1,23 @@ +#!/bin/sh + +. /svc/default/rfkill + +case $1 in + -s) + for s in $BLOCK; + do + rfkill block $s + done + + for s in $UNBLOCK + do + rfkill unblock $s + done + ;; + -k) + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/avail/rfkilld b/svc/avail/rfkilld diff --git a/svc/avail/sensors b/svc/avail/sensors @@ -0,0 +1,18 @@ +#!/bin/sh + +. /svc/default/sensors + +case $1 in + -s) + for m in $HWMON_MODULES; + do + modprobe $m + done + ;; + -k) + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/avail/sshd b/svc/avail/sshd @@ -0,0 +1,33 @@ +#!/bin/sh + +SERVICE="sshd" + +[ -e "/svc/default/$SERVICE" ] && . "/svc/default/$SERVICE" + +BIN="" +for p in /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin; +do + if [ -x "$p/$SERVICE" ]; + then + BIN="$p/$SERVICE" + break + fi +done +[ -z "$BIN" ] && exit 1 + +PID=$(pidof -o %PPID $BIN) + +case $1 in + -s) + ssh-keygen -A + [ -d /var/empty ] || mkdir -p /var/empty + [ -z "$PID" ] && $BIN $PARAMS; + ;; + -k) + [ -n "$PID" ] && kill -9 $PID &> /dev/null; + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/avail/syslog-ng b/svc/avail/syslog-ng diff --git a/svc/avail/wireless-regdom b/svc/avail/wireless-regdom @@ -0,0 +1,15 @@ +#!/bin/sh + +. /svc/default/wireless-regdom + +case $1 in + -s) + ;; + -k) + iw reg set $REGDOM + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/bare.sh b/svc/bare.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +[ $# -eq 0 ] && SERVICE="$0" + +if [ $# -gt 0 ]; +then + SERVICE="$1" + shift 1 +fi + +[ -e "/svc/default/$SERVICE" ] && . "/svc/default/$SERVICE" + +BIN="" +for p in /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin; +do + if [ -x "$p/$SERVICE" ]; + then + BIN="$p/$SERVICE" + break + fi +done +[ -z "$BIN" ] && exit 1 + +PID=$(pidof -o %PPID $BIN) + +case $1 in + -s) + [ -z "$PID" ] && $BIN $PARAMS; + ;; + -k) + [ -n "$PID" ] && kill -9 $PID &> /dev/null; + ;; + *) + echo "usage: $0 -s|-k" + exit 1 +esac + diff --git a/svc/default/alsa b/svc/default/alsa @@ -0,0 +1,6 @@ +STATEFILE="/var/lib/alsa/asound.state" +PARAMS="" +SAVEVOLUME="yes" +MUTEVOLUME="no" +POWERSAVE=0 + diff --git a/svc/default/crond b/svc/default/crond @@ -0,0 +1,2 @@ +PARAMS="-s /cron/cron.d -c /cron/crontabs -t /cron/cronstamps -S -l info" + diff --git a/svc/default/dovecot b/svc/default/dovecot @@ -0,0 +1,2 @@ +PARAMS="-c /cfg/dovecot/dovecot.conf" + diff --git a/svc/default/fancontrol b/svc/default/fancontrol @@ -0,0 +1,2 @@ +PARAMS="-D" + diff --git a/svc/default/gpsd b/svc/default/gpsd @@ -0,0 +1,2 @@ +PARAMS="-F /run/gpsd.sock /dev/ttyUSB0" + diff --git a/svc/default/hostapd b/svc/default/hostapd @@ -0,0 +1,2 @@ +PARAMS="-B /cfg/hostapd/hostapd.conf" + diff --git a/svc/default/hwclock b/svc/default/hwclock @@ -0,0 +1,3 @@ +# --utc or --localtime +PARAMS="--utc" + diff --git a/svc/default/oscam b/svc/default/oscam @@ -0,0 +1,2 @@ +PARAMS="-b -c /cfg/oscam" + diff --git a/svc/default/rfkill b/svc/default/rfkill @@ -0,0 +1,3 @@ +BLOCK="wwan bluetooth" +UNBLOCK="wifi" + diff --git a/svc/default/sensors b/svc/default/sensors @@ -0,0 +1,5 @@ +# Get here the HWMON_MODULES from the generated lm_sensors file, which +# sensors-detect(8) generates. + +HWMON_MODULES="coretemp" + diff --git a/svc/default/wireless-regdom b/svc/default/wireless-regdom @@ -0,0 +1,2 @@ +REGDOM="de" +