Skip to content

Instantly share code, notes, and snippets.

@jamchamb
Last active March 28, 2016 23:20
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 jamchamb/1e10913e33ad93547283 to your computer and use it in GitHub Desktop.
Save jamchamb/1e10913e33ad93547283 to your computer and use it in GitHub Desktop.
Centos init.d script for Phabricator PHD
#!/bin/sh
### BEGIN INIT INFO
# Provides: phd
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: phabricator
# Description: manages phd
### END INIT INFO
# Author: Joshua Hogendorn <jhogendorn@gmail.com>
# Original Author: Alexander Reitzel <funtimecoding@gmail.com>
# Do NOT "set -e"
# source function library
. /etc/rc.d/init.d/functions
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
DESC="Phabricator Daemons"
NAME="phd"
USER="phabricator"
DAEMON="/opt/phabricator/bin/${NAME}"
SCRIPTNAME="/etc/init.d/${NAME}"
LOCKFILE="/var/lock/subsys/${NAME}"
[ -x "${DAEMON}" ] || exit 4
run_as_user()
{
OUTPUT=$(sudo -u "${USER}" -s /bin/bash -c "${1} 2>&1")
return $?
}
is_running()
{
if run_as_user "${DAEMON} status"; then
return 0
else
return 1
fi
}
do_start()
{
if is_running; then
return 1
fi
if ! run_as_user "${DAEMON} start"; then
RETVAL=1
return 1
fi
RETVAL=0
touch "$LOCKFILE"
return 0
}
do_stop()
{
if ! is_running; then
return 1
fi
if ! run_as_user "${DAEMON} stop"; then
RETVAL=1
return 1
fi
RETVAL=0
rm -f "$LOCKFILE"
return 0
}
case "${1}" in
start)
echo -n $"Starting ${DESC}: "
do_start && success || failure
echo
;;
stop)
echo -n $"Stopping ${DESC}: "
do_stop && success || failure
echo
;;
status)
if is_running; then
echo "${DESC} are running."
exit 0
elif [ -e "$LOCKFILE" ]; then
echo "${DESC} are not running but lockfile still exists."
exit 2
else
echo "${DESC} are not running or may be in an unknown state."
echo "For more info, check the output of \"${DAEMON} status\""
exit 4
fi
;;
restart)
echo -n $"Restarting ${DESC}: "
do_stop && sleep 3 # The phd controller is a bit async so hack around that by waiting
do_start && success || failure
echo
;;
*)
echo "Usage: ${SCRIPTNAME} {start|stop|status|restart}" >&2
exit 3
;;
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment