Skip to content

Instantly share code, notes, and snippets.

@brecke
Last active December 11, 2015 19:08
Show Gist options
  • Save brecke/4646034 to your computer and use it in GitHub Desktop.
Save brecke/4646034 to your computer and use it in GitHub Desktop.
my own version for the gitlab init script on a debian squeeze machine. adapted from the original script on https://raw.github.com/gitlabhq/gitlab-recipes/4-1-stable/init.d/gitlab
#! /bin/bash
# GITLAB
# Maintainer: @brecke, based on what @randx did on https://raw.github.com/gitlabhq/gitlab-recipes/4-1-stable/init.d/gitlab
# App Version: 4.1
# Differences to the original version:
# - The gitlab user is actually gitlabhq (so is the app root dir)
# - Using nginx instead of unicorn
# - Assumes nginx creates a pid file in /home/gitlabhq/gitlabhq/tmp/pids/
### BEGIN INIT INFO
# Provides: gitlab
# Required-Start: $local_fs $remote_fs $network $syslog redis-server
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: GitLab git repository management
# Description: GitLab git repository management
### END INIT INFO
APP_ROOT="/home/gitlabhq/gitlabhq"
# DAEMON_OPTS="-c $APP_ROOT/config/unicorn.rb -E production"
PID_PATH="$APP_ROOT/tmp/pids"
NGINX_PID="$PID_PATH/nginx.pid"
SIDEKIQ_PID="$PID_PATH/sidekiq.pid"
STOP_SIDEKIQ="RAILS_ENV=production bundle exec rake sidekiq:stop"
START_SIDEKIQ="RAILS_ENV=production bundle exec rake sidekiq:start"
NAME="nginx"
DESC="Gitlab service"
check_pid(){
if [ -f $NGINX_PID ]; then
PID=`cat $NGINX_PID`
SPID=`cat $SIDEKIQ_PID`
STATUS=`ps aux | grep $PID | grep -v grep | wc -l`
else
STATUS=0
PID=0
fi
}
start() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
# Program is running, exit with error code 1.
echo "Error! $DESC $NAME is currently running!"
exit 1
else
if [ `whoami` = root ]; then
# sudo -u gitlab -H bash -l -c "nohup bundle exec unicorn_rails $DAEMON_OPTS > /dev/null 2>&1 &"
bash -l -c "/etc/init.d/nginx start > /dev/null 2>&1 &"
sudo -u gitlabhq -H bash -l -c "mkdir -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &"
echo "$DESC started"
fi
fi
}
stop() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
## Program is running, stop it.
# kill -QUIT `cat $NGINX_PID`
bash -l -c "killall $NAME"
sudo -u gitlabhq -H bash -l -c "mkdir -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1 &"
# rm "$NGINX_PID" >> /dev/null
echo "$DESC stopped"
else
## Program is not running, exit with error.
echo "Error! $DESC not started!"
exit 1
fi
}
restart() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
echo "Restarting $DESC..."
# kill -USR2 `cat $NGINX_PID`
bash -l -c "killall $NAME"
sudo -u gitlabhq -H bash -l -c "mkdir -p $PID_PATH && $STOP_SIDEKIQ > /dev/null 2>&1 &"
if [ `whoami` = root ]; then
bash -l -c "/etc/init.d/nginx start > /dev/null 2>&1 &"
sudo -u gitlabhq -H bash -l -c "mkdir -p $PID_PATH && $START_SIDEKIQ > /dev/null 2>&1 &"
fi
echo "$DESC restarted."
else
echo "Error, $NAME not running!"
exit 1
fi
}
status() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
echo "$DESC / Nginx with PID $PID is running."
echo "$DESC / Sidekiq with PID $SPID is running."
else
echo "$DESC is not running."
exit 1
fi
}
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root"
exit 1
fi
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload|force-reload)
echo -n "Reloading $NAME configuration: "
kill -HUP `cat $PID`
echo "done."
;;
status)
status
;;
*)
echo "Usage: sudo service gitlab {start|stop|restart|reload}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment