Created
August 14, 2015 14:00
-
-
Save adamotte/837df8b343924ea1ce32 to your computer and use it in GitHub Desktop.
[ARM] [RPI] InfluxDB init.d script
This file contains hidden or 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 | |
# chkconfig: 2345 80 05 | |
# description: Grafana web server & backend | |
# processname: grafana | |
# config: /etc/grafana/grafana.ini | |
# pidfile: /var/run/grafana.pid | |
### BEGIN INIT INFO | |
# Provides: grafana | |
# Required-Start: $all | |
# Required-Stop: $remote_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start grafana at boot time | |
### END INIT INFO | |
# tested on | |
# 1. New lsb that define start-stop-daemon | |
# 3. Centos with initscripts package installed | |
PATH=/bin:/usr/bin:/sbin:/usr/sbin | |
NAME=influxd | |
DESC="Influx DB" | |
DEFAULT=/opt/influxdb/$NAME | |
INFLUX_USER=influxdb | |
INFLUX_GROUP=influxdb | |
INFLUX_HOME=/opt/influxdb/ | |
CONF_DIR=/etc/opt/influxdb | |
WORK_DIR=$INFLUX_HOME | |
DATA_DIR=/var/lib/influxdb | |
LOG_DIR=/var/log/influxdb | |
CONF_FILE=$CONF_DIR/influxdb.conf | |
MAX_OPEN_FILES=10000 | |
PID_FILE=/var/run/$NAME.pid | |
DAEMON=$DEFAULT | |
if [ `id -u` -ne 0 ]; then | |
echo "You need root privileges to run this script" | |
exit 4 | |
fi | |
if [ ! -x $DAEMON ]; then | |
echo "Program not installed or not executable" | |
exit 5 | |
fi | |
. /lib/lsb/init-functions | |
if [ -r /etc/default/rcS ]; then | |
. /etc/default/rcS | |
fi | |
DAEMON_OPTS="--pidfile=${PID_FILE} --config=${CONF_FILE} cfg:default.paths.data=${DATA_DIR} cfg:default.paths.logs=${LOG_DIR}" | |
case "$1" in | |
start) | |
log_daemon_msg "Starting $DESC" | |
pid=`pidofproc -p $PID_FILE $NAME` | |
if [ -n "$pid" ] ; then | |
log_begin_msg "Already running." | |
log_end_msg 0 | |
exit 0 | |
fi | |
# Prepare environment | |
mkdir -p "$LOG_DIR" "$DATA_DIR" && chown "$INFLUX_USER":"$INFLUX_GROUP" "$LOG_DIR" "$DATA_DIR" | |
touch "$PID_FILE" && chown "$INFLUX_USER":"$INFLUX_GROUP" "$PID_FILE" | |
if [ -n "$MAX_OPEN_FILES" ]; then | |
ulimit -n $MAX_OPEN_FILES | |
fi | |
# Start Daemon | |
start-stop-daemon --start -b --chdir "$WORK_DIR" --user "$INFLUX_USER" -c "$INFLUX_USER" --pidfile "$PID_FILE" --exec $DAEMON -- $DAEMON_OPTS | |
return=$? | |
if [ $return -eq 0 ] | |
then | |
sleep 1 | |
# check if pid file has been written two | |
if ! [[ -s $PID_FILE ]]; then | |
log_end_msg 1 | |
exit 1 | |
fi | |
i=0 | |
timeout=10 | |
# Wait for the process to be properly started before exiting | |
until { cat "$PID_FILE" | xargs kill -0; } >/dev/null 2>&1 | |
do | |
sleep 1 | |
i=$(($i + 1)) | |
if [ $i -gt $timeout ]; then | |
log_end_msg 1 | |
exit 1 | |
fi | |
done | |
fi | |
log_end_msg $return | |
;; | |
stop) | |
log_daemon_msg "Stopping $DESC" | |
if [ -f "$PID_FILE" ]; then | |
start-stop-daemon --stop --pidfile "$PID_FILE" \ | |
--user "$INFLUX_USER" \ | |
--retry=TERM/20/KILL/5 >/dev/null | |
if [ $? -eq 1 ]; then | |
log_progress_msg "$DESC is not running but pid file exists, cleaning up" | |
elif [ $? -eq 3 ]; then | |
PID="`cat $PID_FILE`" | |
log_failure_msg "Failed to stop $DESC (pid $PID)" | |
exit 1 | |
fi | |
rm -f "$PID_FILE" | |
else | |
log_progress_msg "(not running)" | |
fi | |
log_end_msg 0 | |
;; | |
status) | |
status_of_proc -p $PID_FILE $NAME $NAME && exit 0 || exit $? | |
;; | |
restart|force-reload) | |
if [ -f "$PID_FILE" ]; then | |
$0 stop | |
sleep 1 | |
fi | |
$0 start | |
;; | |
*) | |
log_success_msg "Usage: $0 {start|stop|restart|force-reload|status}" | |
exit 3 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment