Skip to content

Instantly share code, notes, and snippets.

@sebboh
Created April 7, 2015 19: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 sebboh/2796cbc79c695caa5d82 to your computer and use it in GitHub Desktop.
Save sebboh/2796cbc79c695caa5d82 to your computer and use it in GitHub Desktop.
daemonize service
#!/bin/sh
# template inputs:
# @service_name: name of the service
# @service_cwd: working directory for running the service
# @service_cmd: command to run the service
start () {
echo -n "Starting <%= @service_name %>..."
# Start daemon
daemon --chdir='<%= @service_cwd %>' --command '<%= @service_cmd %>' --respawn --output=/var/log/<%= @service_name %>/output.log --name=<%= @service_name %> --user=<%= @service_user %> --verbose
RETVAL=$?
if [ $RETVAL = 0 ]
then
echo "done."
else
echo "failed. See error code for more information."
fi
return $RETVAL
}
stop () {
# Stop daemon
echo -n "Stopping <%= @service_name %>..."
daemon --stop --name=<%= @service_name %> --user=<%= @service_user %> --verbose
RETVAL=$?
if [ $RETVAL = 0 ]
then
echo "Done."
else
echo "Failed. See error code for more information."
fi
return $RETVAL
}
restart () {
status
RETVAL=$?
if [ $RETVAL = 0 ]
then
stop
sleep 2
fi
start
}
status () {
# Report on the status of the daemon
daemon --running --verbose --user=<%= @service_user %> --name=<%= @service_name %>
return $?
}
case "$1" in
start)
start
;;
status)
status
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: <%= @service_name %> {start|status|stop|restart}"
exit 3
;;
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment