Last active
November 6, 2015 17:15
-
-
Save kittolau/879f50aa14996a590ee2 to your computer and use it in GitHub Desktop.
System V init script template, the work from https://github.com/jasonblewis/sample-service-script
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/sh | |
SERVICE_RUNAS=selenium | |
SERVICE_NAME=selenium_server | |
#add system user to run selenium | |
sudo useradd -r $SERVICE_RUNAS | |
sudo mkdir /home/selenium | |
sudo chown selenium:selenium /home/selenium | |
#service params | |
NAME="selenium_server" | |
DESCRIPTION="a stand alone selenium server" | |
USERNAME="selenium" | |
COMMAND="DISPLAY=:99 java -jar /usr/local/share/java/selenium-server" | |
SERVICE_FILE=$(tempfile) | |
SERVICE_FILE_TEMPLATE_URL='https://gist.githubusercontent.com/kittolau/879f50aa14996a590ee2/raw/service_template' | |
wget -q -O "$SERVICE_FILE" $SERVICE_FILE_TEMPLATE_URL || { echo 'ERROR: Could not retreive SERVICE_TEMPLATE_URL from github';} | |
chmod +x "$SERVICE_FILE" | |
sed -i "s/<NAME>/$NAME/g" $SERVICE_FILE | |
sed -i "s/<DESCRIPTION>/$DESCRIPTION/g" $SERVICE_FILE | |
sed -i "s/<USERNAME>/$USERNAME/g" $SERVICE_FILE | |
sed -i "s/<SERVICE_NAME>/$NAME/g" $SERVICE_FILE | |
sed -i "s@<COMMAND>@$COMMAND@g" $SERVICE_FILE | |
sudo chown root:root "$SERVICE_FILE" | |
sudo mv -v "$SERVICE_FILE" "/etc/init.d/$NAME" | |
sudo touch "/var/log/$NAME.log" && sudo chown "$USERNAME" "/var/log/$NAME.log" | |
sudo update-rc.d "$NAME" defaults 98 | |
sudo service "$NAME" start |
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/sh | |
### BEGIN INIT INFO | |
# Provides: <NAME> | |
# Required-Start: $local_fs $network $named $time $syslog | |
# Required-Stop: $local_fs $network $named $time $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Description: <DESCRIPTION> | |
### END INIT INFO | |
SCRIPT="<COMMAND>" | |
RUNAS="<USERNAME>" | |
NAME="<SERVICE_NAME>" | |
PIDFILE=/var/run/$NAME.pid | |
LOGFILE=/var/log/$NAME.log | |
start() { | |
if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then | |
echo 'Service already running' >&2 | |
return 1 | |
fi | |
echo 'Starting service…' >&2 | |
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!" | |
su -c "$CMD" $RUNAS > "$PIDFILE" | |
echo 'Service started' >&2 | |
} | |
stop() { | |
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then | |
echo 'Service not running' >&2 | |
return 1 | |
fi | |
echo 'Stopping service…' >&2 | |
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE" | |
echo 'Service stopped' >&2 | |
} | |
uninstall() { | |
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " | |
local SURE | |
read SURE | |
if [ "$SURE" = "yes" ]; then | |
stop | |
rm -f "$PIDFILE" | |
echo "Notice: log file was not removed: '$LOGFILE'" >&2 | |
update-rc.d -f <NAME> remove | |
rm -fv "$0" | |
fi | |
} | |
status() { | |
printf "%-50s" "Checking $NAME..." | |
if [ -f $PIDFILE ]; then | |
PID=$(cat $PIDFILE) | |
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then | |
printf "%s\n" "The process appears to be dead but pidfile still exists" | |
else | |
echo "Running, the PID is $PID" | |
fi | |
else | |
printf "%s\n" "Service not running" | |
fi | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
status) | |
status | |
;; | |
uninstall) | |
uninstall | |
;; | |
restart) | |
stop | |
start | |
;; | |
*) | |
echo "Usage: $0 {start|stop|status|restart|uninstall}" | |
esac |
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/sh | |
SERVICE_RUNAS=tor | |
SERVICE_NAME=tor_proxy | |
#add system user to run tor | |
sudo useradd -r $SERVICE_RUNAS | |
sudo mkdir /home/tor | |
sudo chown tor:tor /home/tor | |
#service params | |
NAME="tor_proxy" | |
DESCRIPTION="a proxy service for tor" | |
USERNAME="tor" | |
COMMAND="tor" | |
SERVICE_FILE=$(tempfile) | |
SERVICE_FILE_TEMPLATE_URL='https://gist.githubusercontent.com/kittolau/879f50aa14996a590ee2/raw/service_template' | |
wget -q -O "$SERVICE_FILE" $SERVICE_FILE_TEMPLATE_URL || { echo 'ERROR: Could not retreive SERVICE_TEMPLATE_URL from github';} | |
chmod +x "$SERVICE_FILE" | |
sed -i "s/<NAME>/$NAME/g" $SERVICE_FILE | |
sed -i "s/<DESCRIPTION>/$DESCRIPTION/g" $SERVICE_FILE | |
sed -i "s/<USERNAME>/$USERNAME/g" $SERVICE_FILE | |
sed -i "s/<SERVICE_NAME>/$NAME/g" $SERVICE_FILE | |
sed -i "s@<COMMAND>@$COMMAND@g" $SERVICE_FILE | |
sudo chown root:root "$SERVICE_FILE" | |
sudo mv -v "$SERVICE_FILE" "/etc/init.d/$NAME" | |
sudo touch "/var/log/$NAME.log" && sudo chown "$USERNAME" "/var/log/$NAME.log" | |
sudo update-rc.d "$NAME" defaults | |
sudo service "$NAME" start |
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/sh | |
SERVICE_RUNAS=xvfb | |
SERVICE_NAME=xvfb | |
#add system user to run xvfb | |
sudo useradd -r $SERVICE_RUNAS | |
sudo mkdir /home/xvfb | |
sudo chown xvfb:xvfb /home/xvfb | |
#service params | |
NAME="xvfb" | |
DESCRIPTION="a virtual screen" | |
USERNAME="xvfb" | |
COMMAND="Xvfb ':99' -ac" | |
SERVICE_FILE=$(tempfile) | |
SERVICE_FILE_TEMPLATE_URL='https://gist.githubusercontent.com/kittolau/879f50aa14996a590ee2/raw/service_template' | |
wget -q -O "$SERVICE_FILE" $SERVICE_FILE_TEMPLATE_URL || { echo 'ERROR: Could not retreive SERVICE_TEMPLATE_URL from github';} | |
chmod +x "$SERVICE_FILE" | |
sed -i "s/<NAME>/$NAME/g" $SERVICE_FILE | |
sed -i "s/<DESCRIPTION>/$DESCRIPTION/g" $SERVICE_FILE | |
sed -i "s/<USERNAME>/$USERNAME/g" $SERVICE_FILE | |
sed -i "s/<SERVICE_NAME>/$NAME/g" $SERVICE_FILE | |
sed -i "s@<COMMAND>@$COMMAND@g" $SERVICE_FILE | |
sudo chown root:root "$SERVICE_FILE" | |
sudo mv -v "$SERVICE_FILE" "/etc/init.d/$NAME" | |
sudo touch "/var/log/$NAME.log" && sudo chown "$USERNAME" "/var/log/$NAME.log" | |
sudo update-rc.d "$NAME" defaults | |
sudo service "$NAME" start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment