-
-
Save k3an3/59cbdfb2d2026b43be6b70355bc90048 to your computer and use it in GitHub Desktop.
Wrapper for sv command in runit. Adds additional features that makes using runit easier. Not that I'm actually using runit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Wrapper for sv to add additional functionality | |
# Copyright Keane O'Kelley 2017 | |
# | |
# Place in /usr/local/bin | |
SV="/usr/bin/sv" | |
if [[ $# -lt 2 ]]; then | |
cat <<EOF | |
usage: sv-wrapper (add|remove|enable|disable|chown) | |
add <service_name> [user:group] | |
remove <service_name> | |
enable <service_name> | |
disable <service_name> | |
chown <user:group> | |
EOF | |
$SV | |
exit | |
fi | |
if [[ $(id -u) -eq 0 ]]; then | |
SERVICEDIR="/etc/service" | |
SVDIR="/etc/sv" | |
else | |
SERVICEDIR="~/.service" | |
SVDIR="~/.sv" | |
fi | |
DIR="$SVDIR/$2" | |
if [[ "$1" == "add" ]]; then | |
if [[ -d "$DIR" ]]; then | |
echo "Service '$2' already exists. Exiting..." | |
exit | |
fi | |
mkdir -p "$DIR" | |
touch "$DIR/run" | |
chmod 755 "$DIR/run" | |
if [[ "$#" -eq 3 ]]; then | |
mkdir "$DIR/supervise" | |
path=`echo $DIR/supervise/{ok,control,status}` | |
touch $path | |
chown "$3" $path | |
fi | |
exit | |
elif [[ "$1" == "remove" ]]; then | |
echo "Are you sure you want to permanently delete '$2'? (y/n)" | |
read confirm | |
if [[ "$confirm" == "y" ]]; then | |
rm -rf "$DIR" | |
else | |
echo "Exiting without any actions taken..." | |
exit | |
fi | |
elif [[ "$1" == "enable" ]]; then | |
if [[ ! -d "$DIR" ]]; then | |
echo "Service '$2' doesn't exist." | |
exit | |
fi | |
echo "Symlinking $DIR to $SERVICEDIR/$2..." | |
ln -s "$DIR" "$SERVICEDIR" | |
exit | |
elif [[ "$1" == "disable" ]]; then | |
if [[ ! -d "$SERVICEDIR/$2" ]]; then | |
echo "Service '$2' not enabled or doesn't exist." | |
exit | |
fi | |
echo "Removing symlink from $DIR to $SERVICEDIR/$2..." | |
unlink "$SERVICEDIR/$2" | |
exit | |
elif [[ "$1" == "chown" ]]; then | |
if [[ ! -d "$SERVICEDIR/$2" ]]; then | |
echo "Service '$2' doesn't exist." | |
exit | |
fi | |
if [[ "$#" -lt 3 ]]; then | |
echo "usage: sv-wrapper chown <service> <user:group>" | |
exit | |
fi | |
chown "$3" "$DIR/supervise/{ok,control,status}" | |
exit | |
fi | |
$SV "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment