Skip to content

Instantly share code, notes, and snippets.

@underscorenygren
Forked from naholyr/_service.md
Last active August 29, 2015 14:18
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 underscorenygren/aa607d3dd3ebe4c57f1b to your computer and use it in GitHub Desktop.
Save underscorenygren/aa607d3dd3ebe4c57f1b to your computer and use it in GitHub Desktop.
Setting up an init.d service using chef.

Sample service script for debianoids

Look at LSB init scripts for more information.

Modified to work with Chef

Usage

template "/etc/init.d/[NAME]" do
  source "service.sh.erb"
  mode   777
  variables(name: NAME, user: USER, command: COMMAND, env: {})
end

directory "/var/log/[NAME]
  mode 777
  user USER
  action :create
end
  • <NAME> = $YOUR_SERVICE_NAME
  • <DESCRIPTION> = Describe your service here (be concise)
  • Feel free to modify the LSB header, I've made default choices you may not agree with
  • <COMMAND> = Command to start your server (for example /home/myuser/.dropbox-dist/dropboxd)
  • <USER> = Login of the system user the script should be run as (for example myuser)

Start and test your service:

service $YOUR_SERVICE_NAME start
service $YOUR_SERVICE_NAME stop

Uninstall

The service can uninstall itself with service $NAME uninstall. Yes, that's very easy, therefore a bit dangerous. But as it's an auto-generated script, you can bring it back very easily. I use it for tests and often install/uninstall, that's why I've put that here.

Don't want it? Remove lines 56-58 of the service's script.

Logs?

Your service will log its output to /var/log/$NAME/$NAME.log. Don't forget to setup a logrotate :)

#!/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: Created by chef from service.sh.erb
### END INIT INFO
SCRIPT='<%= @command %>'
RUNAS=<%= @user %>
PIDFILE=/var/run/<%= @name %>.pid
LOGFILE=/var/log/<%= @name %>/<%= @name %>.log
<% if @env
@env.each {|name,val| %>
export <%= "#{name}='#{val}'" %>
<%} end %>
start() {
if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service <%= @name %>!' >&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' >&1
return 0
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 is not be removed: '$LOGFILE'" >&2
update-rc.d -f <%= @name %> remove
rm -fv "$0"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|uninstall}"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment