Skip to content

Instantly share code, notes, and snippets.

@k3an3
Last active February 26, 2017 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k3an3/59cbdfb2d2026b43be6b70355bc90048 to your computer and use it in GitHub Desktop.
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.
#!/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