Created
November 11, 2008 20:58
-
-
Save kelp/23968 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
# | |
# Setup a proceess to run from runit in the ilike users home dir | |
# | |
# For more information about runit see: http://smarden.org/runit/ | |
# | |
export SVDIR=~/service/ | |
progname=$(basename $0) | |
# Commence ~40 lines just to parse 4 arguments | |
usage(){ | |
cat << EOF | |
Usage: $0 -s|-r service -c "command line" [ options ] | |
Options: | |
--command, -c Command line to run, must be in quotes | |
--service, -s Service name, no spaces please | |
--remove, -r Shut down and remove an existing service | |
--verbose, -v Verbose mode | |
--help, -h Print this help | |
EOF | |
exit 1 | |
} | |
if [ $# -lt 1 ]; then | |
usage | |
fi | |
function rmservice { | |
if [ ! -d ~/sv/$service ]; then | |
echo "Error: the $service service doesn't seem to exist, please investigate" | |
exit 1 | |
fi | |
echo "Stopping $service" | |
sv stop $service | |
rm ~/service/${service} | |
rm -r ~/sv/${service} | |
rm ~/bin/${service} | |
echo "$service shut down and removed" | |
exit 0 | |
} | |
SHORTOPTS="c:s:r:vh" | |
LONGOPTS="command:,service:,remove:,verbose,help" | |
OPTS=$(getopt -o $SHORTOPTS --long $LONGOPTS -n "$progname" -- "$@") | |
if [ $? -ne 0 ]; then | |
usage | |
fi | |
eval set -- "$OPTS" | |
while [ $# -gt 0 ]; do | |
case $1 in | |
-c|--command) | |
command="$2" | |
shift 2;; | |
-s|--service) | |
service="$2" | |
shift 2;; | |
-r|--remove) | |
service="$2" | |
rmservice | |
shift;; | |
-v|--verbose) | |
set -x | |
shift;; | |
-h|--help) | |
usage;; | |
--) | |
shift | |
break;; | |
*) | |
usage;; | |
esac | |
done | |
if [[ -z $command && -z $service ]]; then | |
echo "Error: service and command arguments required" | |
usage | |
fi | |
# Done with argument parsing | |
# Create the service and log dirs in one go | |
mkdir -p ~/sv/${service}/log/main | |
cat > ~/sv/${service}/run <<-EOF | |
#!/bin/sh | |
exec 2>&1 | |
exec $command | |
EOF | |
cat > ~/sv/${service}/log/run <<-EOF | |
#!/bin/sh | |
exec svlogd -tt ./main | |
EOF | |
chmod +x ~/sv/${service}/run ~/sv/${service}/log/run | |
# Activate! | |
ln -fs ~/sv/${service} ~/service/ | |
# Create an LSB style init script, mostly for users | |
mkdir -p ~/bin/ | |
ln -fs /sbin/sv ~/bin/${service} | |
echo "Starting $service" | |
# Give the service a chance to start | |
sleep 5 | |
status=`sv status $service` | |
RETVAL=$? | |
if [ $RETVAL != 0 ]; then | |
echo $status | |
echo "Something went terribly wrong while starting your service! Please investigate." | |
exit 1 | |
fi | |
cat << EOF | |
$status | |
$service is now running! | |
You can control it with these handy commands: | |
~/bin/$service start | |
~/bin/$service stop | |
~/bin/$service restart | |
~/bin/$service status | |
Please check for application errors by running: | |
tail ~/sv/${service}/log/main/current | |
For more information on this system see sv(8) or http://smarden.org/runit/ | |
EOF | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment