Created
June 30, 2015 02:57
-
-
Save itxx00/1e192aa651a08fe09858 to your computer and use it in GitHub Desktop.
init scripts
This file contains 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
#!/bin/bash | |
# /etc/init.d/jenkins | |
# debian-compatible jenkins startup script. | |
# Amelia A Lewis <alewis@ibco.com> | |
# | |
### BEGIN INIT INFO | |
# Provides: jenkins | |
# Required-Start: $remote_fs $syslog $network | |
# Required-Stop: $remote_fs $syslog $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start jenkins at boot time | |
# Description: Controls the jenkins continuous integration engine. | |
### END INIT INFO | |
PATH=/bin:/usr/bin:/sbin:/usr/sbin | |
DESC="Jenkins Continuous Integration Server" | |
NAME=jenkins | |
SCRIPTNAME=/etc/init.d/$NAME | |
[ -r /etc/default/$NAME ] && . /etc/default/$NAME | |
#DAEMON=$JENKINS_SH | |
DAEMON=/usr/bin/daemon | |
DAEMON_ARGS="--name=$NAME --inherit --env=JENKINS_HOME=$JENKINS_HOME --output=$JENKINS_LOG --pidfile=$PIDFILE" | |
SU=/bin/su | |
# Exit if the package is not installed | |
[ -x "$DAEMON" ] || (echo "daemon package not installed" && exit 0) | |
# Exit if not supposed to run standalone | |
[ "$RUN_STANDALONE" = "false" ] && echo "Not configured to run standalone" && exit 0 | |
# load environments | |
if [ -r /etc/default/locale ]; then | |
. /etc/default/locale | |
export LANG LANGUAGE | |
elif [ -r /etc/environment ]; then | |
. /etc/environment | |
export LANG LANGUAGE | |
fi | |
# Load the VERBOSE setting and other rcS variables | |
. /lib/init/vars.sh | |
# Define LSB log_* functions. | |
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present. | |
. /lib/lsb/init-functions | |
# Make sure we run as root, since setting the max open files through | |
# ulimit requires root access | |
if [ `id -u` -ne 0 ]; then | |
echo "The $NAME init script can only be run as root" | |
exit 1 | |
fi | |
check_tcp_port() { | |
local service=$1 | |
local assigned=$2 | |
local default=$3 | |
if [ -n "$assigned" ]; then | |
port=$assigned | |
else | |
port=$default | |
fi | |
count=`netstat --listen --numeric-ports | grep \:$port[[:space:]] | grep -c . ` | |
if [ $count -ne 0 ]; then | |
echo "The selected $service port ($port) seems to be in use by another program " | |
echo "Please select another port to use for $NAME" | |
return 1 | |
fi | |
} | |
# | |
# Function that starts the daemon/service | |
# | |
do_start() | |
{ | |
# the default location is /var/run/jenkins/jenkins.pid but the parent directory needs to be created | |
mkdir `dirname $PIDFILE` > /dev/null 2>&1 || true | |
chown $JENKINS_USER `dirname $PIDFILE` | |
# Return | |
# 0 if daemon has been started | |
# 1 if daemon was already running | |
# 2 if daemon could not be started | |
$DAEMON $DAEMON_ARGS --running && return 1 | |
# Verify that the jenkins port is not already in use, winstone does not exit | |
# even for BindException | |
check_tcp_port "http" "$HTTP_PORT" "8080" || return 1 | |
# If the var MAXOPENFILES is enabled in /etc/default/jenkins then set the max open files to the | |
# proper value | |
if [ -n "$MAXOPENFILES" ]; then | |
[ "$VERBOSE" != no ] && echo Setting up max open files limit to $MAXOPENFILES | |
ulimit -n $MAXOPENFILES | |
fi | |
# --user in daemon doesn't prepare environment variables like HOME, USER, LOGNAME or USERNAME, | |
# so we let su do so for us now | |
$SU -l $JENKINS_USER --shell=/bin/bash -c "$DAEMON $DAEMON_ARGS -- $JAVA $JAVA_ARGS -jar $JENKINS_WAR $JENKINS_ARGS" || return 2 | |
} | |
# | |
# Verify that all jenkins processes have been shutdown | |
# and if not, then do killall for them | |
# | |
get_running() | |
{ | |
return `ps -U $JENKINS_USER --no-headers -f | egrep -e '(java|daemon)' | grep -c . ` | |
} | |
force_stop() | |
{ | |
get_running | |
if [ $? -ne 0 ]; then | |
killall -u $JENKINS_USER java daemon || return 3 | |
fi | |
} | |
# Get the status of the daemon process | |
get_daemon_status() | |
{ | |
$DAEMON $DAEMON_ARGS --running || return 1 | |
} | |
# | |
# Function that stops the daemon/service | |
# | |
do_stop() | |
{ | |
# Return | |
# 0 if daemon has been stopped | |
# 1 if daemon was already stopped | |
# 2 if daemon could not be stopped | |
# other if a failure occurred | |
get_daemon_status | |
case "$?" in | |
0) | |
$DAEMON $DAEMON_ARGS --stop || return 2 | |
# wait for the process to really terminate | |
for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do | |
sleep 1 | |
$DAEMON $DAEMON_ARGS --running || break | |
done | |
if get_daemon_status; then | |
force_stop || return 3 | |
fi | |
;; | |
*) | |
force_stop || return 3 | |
;; | |
esac | |
# Many daemons don't delete their pidfiles when they exit. | |
rm -f $PIDFILE | |
return 0 | |
} | |
case "$1" in | |
start) | |
log_daemon_msg "Starting $DESC" "$NAME" | |
do_start | |
case "$?" in | |
0|1) log_end_msg 0 ;; | |
2) log_end_msg 1 ;; | |
esac | |
;; | |
stop) | |
log_daemon_msg "Stopping $DESC" "$NAME" | |
do_stop | |
case "$?" in | |
0|1) log_end_msg 0 ;; | |
2) log_end_msg 1 ;; | |
esac | |
;; | |
restart|force-reload) | |
# | |
# If the "reload" option is implemented then remove the | |
# 'force-reload' alias | |
# | |
log_daemon_msg "Restarting $DESC" "$NAME" | |
do_stop | |
case "$?" in | |
0|1) | |
do_start | |
case "$?" in | |
0) log_end_msg 0 ;; | |
1) log_end_msg 1 ;; # Old process is still running | |
*) log_end_msg 1 ;; # Failed to start | |
esac | |
;; | |
*) | |
# Failed to stop | |
log_end_msg 1 | |
;; | |
esac | |
;; | |
status) | |
get_daemon_status | |
case "$?" in | |
0) | |
echo "$DESC is running with the pid `cat $PIDFILE`" | |
rc=0 | |
;; | |
*) | |
get_running | |
procs=$? | |
if [ $procs -eq 0 ]; then | |
echo -n "$DESC is not running" | |
if [ -f $PIDFILE ]; then | |
echo ", but the pidfile ($PIDFILE) still exists" | |
rc=1 | |
else | |
echo | |
rc=3 | |
fi | |
else | |
echo "$procs instances of jenkins are running at the moment" | |
echo "but the pidfile $PIDFILE is missing" | |
rc=0 | |
fi | |
exit $rc | |
;; | |
esac | |
;; | |
*) | |
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 | |
exit 3 | |
;; | |
esac | |
exit 0 |
This file contains 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
#! /bin/sh | |
# | |
# Copyright (c) 1999, 2006 Tanuki Software Inc. | |
# | |
# Java Service Wrapper sh script. Suitable for starting and stopping | |
# wrapped Java applications on UNIX platforms. | |
# | |
#----------------------------------------------------------------------------- | |
# These settings can be modified to fit the needs of your application | |
# If specified, the Wrapper will be run as the specified user. | |
# IMPORTANT - Make sure that the user has the required privileges to write into the Nexus installation directory. | |
# NOTE - This will set the user which is used to run the Wrapper as well as | |
# the JVM and is not useful in situations where a privileged resource or | |
# port needs to be allocated prior to the user being changed. | |
RUN_AS_USER=nexus | |
# Application | |
APP_NAME="nexus" | |
APP_LONG_NAME="Nexus OSS" | |
PLATFORM=linux-x86-64 | |
NEXUS_HOME=/home/nexus/nexus | |
# Wrapper | |
WRAPPER_CMD="$NEXUS_HOME/bin/jsw/$PLATFORM/wrapper" | |
WRAPPER_CONF="$NEXUS_HOME/bin/jsw/conf/wrapper.conf" | |
# Priority at which to run the wrapper. See "man nice" for valid priorities. | |
# nice is only used if a priority is specified. | |
PRIORITY= | |
# Location of the pid file. | |
PIDDIR="/home/nexus/sonatype-work/nexus" | |
# If uncommented, causes the Wrapper to be shutdown using an anchor file. | |
# When launched with the 'start' command, it will also ignore all INT and | |
# TERM signals. | |
#IGNORE_SIGNALS=true | |
# The following two lines are used by the chkconfig command. Change as is | |
# appropriate for your application. They should remain commented. | |
# chkconfig: 2345 20 80 | |
# description: Test Wrapper Sample Application | |
# Do not modify anything beyond this point | |
#----------------------------------------------------------------------------- | |
# Get the fully qualified path to the script | |
case $0 in | |
/*) | |
SCRIPT="$0" | |
;; | |
*) | |
PWD=`pwd` | |
SCRIPT="$PWD/$0" | |
;; | |
esac | |
# Resolve the true real path without any sym links. | |
CHANGED=true | |
while [ "X$CHANGED" != "X" ] | |
do | |
# Change spaces to ":" so the tokens can be parsed. | |
SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'` | |
# Get the real path to this script, resolving any symbolic links | |
TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'` | |
REALPATH= | |
for C in $TOKENS; do | |
# Change any ":" in the token back to a space. | |
C=`echo $C | sed -e 's;:; ;g'` | |
REALPATH="$REALPATH/$C" | |
# If REALPATH is a sym link, resolve it. Loop for nested links. | |
while [ -h "$REALPATH" ] ; do | |
LS="`ls -ld "$REALPATH"`" | |
LINK="`expr "$LS" : '.*-> \(.*\)$'`" | |
if expr "$LINK" : '/.*' > /dev/null; then | |
# LINK is absolute. | |
REALPATH="$LINK" | |
else | |
# LINK is relative. | |
REALPATH="`dirname "$REALPATH"`""/$LINK" | |
fi | |
done | |
done | |
if [ "$REALPATH" = "$SCRIPT" ] | |
then | |
CHANGED="" | |
else | |
SCRIPT="$REALPATH" | |
fi | |
done | |
# Change the current directory to the location of the script | |
cd "`dirname "$REALPATH"`" | |
REALDIR=`pwd` | |
# Resolve the location of the 'ps' command | |
PSEXE="/usr/bin/ps" | |
if [ ! -x "$PSEXE" ] | |
then | |
PSEXE="/bin/ps" | |
if [ ! -x "$PSEXE" ] | |
then | |
echo "Unable to locate 'ps'." | |
echo "Please report this message along with the location of the command on your system." | |
exit 1 | |
fi | |
fi | |
# Resolve the os | |
DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]` | |
case "$DIST_OS" in | |
'sunos') | |
DIST_OS="solaris" | |
;; | |
'hp-ux' | 'hp-ux64') | |
DIST_OS="hpux" | |
;; | |
'darwin') | |
DIST_OS="macosx" | |
;; | |
'unix_sv') | |
DIST_OS="unixware" | |
;; | |
esac | |
# Resolve the architecture | |
DIST_ARCH=`uname -p | tr [:upper:] [:lower:] | tr -d [:blank:]` | |
if [ "$DIST_ARCH" = "unknown" ] | |
then | |
DIST_ARCH=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]` | |
fi | |
case "$DIST_ARCH" in | |
'amd64' | 'athlon' | 'ia32' | 'ia64' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64') | |
DIST_ARCH="x86" | |
;; | |
'ip27') | |
DIST_ARCH="mips" | |
;; | |
'power' | 'powerpc' | 'power_pc' | 'ppc64') | |
DIST_ARCH="ppc" | |
;; | |
'pa_risc' | 'pa-risc') | |
DIST_ARCH="parisc" | |
;; | |
'sun4u' | 'sparcv9') | |
DIST_ARCH="sparc" | |
;; | |
'9000/800') | |
DIST_ARCH="parisc" | |
;; | |
esac | |
outputFile() { | |
if [ -f "$1" ] | |
then | |
echo " $1 (Found but not executable.)"; | |
else | |
echo " $1" | |
fi | |
} | |
# Default PIDDIR to current directory | |
if [ "X$PIDDIR" = "X" ] | |
then | |
PIDDIR="." | |
fi | |
# If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if | |
# the working directory is later changed. | |
FIRST_CHAR=`echo $PIDDIR | cut -c1,1` | |
if [ "$FIRST_CHAR" != "/" ] | |
then | |
PIDDIR=$REALDIR/$PIDDIR | |
fi | |
# Same test for WRAPPER_CMD | |
FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1` | |
if [ "$FIRST_CHAR" != "/" ] | |
then | |
WRAPPER_CMD=$REALDIR/$WRAPPER_CMD | |
fi | |
# Same test for WRAPPER_CONF | |
FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1` | |
if [ "$FIRST_CHAR" != "/" ] | |
then | |
WRAPPER_CONF=$REALDIR/$WRAPPER_CONF | |
fi | |
# Process ID | |
ANCHORFILE="$PIDDIR/$APP_NAME.anchor" | |
PIDFILE="$PIDDIR/$APP_NAME.pid" | |
LOCKDIR="/var/lock/subsys" | |
LOCKFILE="$LOCKDIR/$APP_NAME" | |
pid="" | |
if [ ! -x "$WRAPPER_CMD" ]; then | |
echo "Missing platform binary: $WRAPPER_CMD" | |
exit 1 | |
fi | |
# Build the nice clause | |
if [ "X$PRIORITY" = "X" ] | |
then | |
CMDNICE="" | |
else | |
CMDNICE="nice -$PRIORITY" | |
fi | |
# Build the anchor file clause. | |
if [ "X$IGNORE_SIGNALS" = "X" ] | |
then | |
ANCHORPROP= | |
IGNOREPROP= | |
else | |
ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\" | |
IGNOREPROP=wrapper.ignore_signals=TRUE | |
fi | |
# Build the lock file clause. Only create a lock file if the lock directory exists on this platform. | |
LOCKPROP= | |
if [ -d $LOCKDIR ] | |
then | |
if [ -w $LOCKDIR ] | |
then | |
LOCKPROP=wrapper.lockfile=\"$LOCKFILE\" | |
fi | |
fi | |
checkUser() { | |
# $1 touchLock flag | |
# $2 command | |
# Resolve the location of the 'id' command | |
IDEXE="/usr/xpg4/bin/id" | |
if [ ! -x "$IDEXE" ] | |
then | |
IDEXE="/usr/bin/id" | |
if [ ! -x "$IDEXE" ] | |
then | |
echo "Unable to locate 'id'." | |
echo "Please report this message along with the location of the command on your system." | |
exit 1 | |
fi | |
fi | |
# Check the configured user. If necessary rerun this script as the desired user. | |
if [ "X$RUN_AS_USER" != "X" ] | |
then | |
if [ "`$IDEXE -u -n`" != "$RUN_AS_USER" ] | |
then | |
# If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be | |
# able to create the lock file. The Wrapper will be able to update this file once it | |
# is created but will not be able to delete it on shutdown. If $2 is defined then | |
# the lock file should be created for the current command | |
if [ "X$LOCKPROP" != "X" ] | |
then | |
if [ "X$1" != "X" ] | |
then | |
# Resolve the primary group | |
RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1` | |
if [ "X$RUN_AS_GROUP" = "X" ] | |
then | |
RUN_AS_GROUP=$RUN_AS_USER | |
fi | |
touch $LOCKFILE | |
chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE | |
fi | |
fi | |
# Still want to change users, recurse. This means that the user will only be | |
# prompted for a password once. Variables shifted by 1 | |
su - $RUN_AS_USER -c "\"$REALPATH\" $2" | |
# Now that we are the original user again, we may need to clean up the lock file. | |
if [ "X$LOCKPROP" != "X" ] | |
then | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
# Wrapper is not running so make sure the lock file is deleted. | |
if [ -f "$LOCKFILE" ] | |
then | |
rm "$LOCKFILE" | |
fi | |
fi | |
fi | |
exit 0 | |
fi | |
fi | |
# Check that script is not run as root | |
LUID=`$IDEXE -u` | |
if [ $LUID -eq 0 ] | |
then | |
echo "****************************************" | |
echo "WARNING - NOT RECOMMENDED TO RUN AS ROOT" | |
echo "****************************************" | |
if [ ! "`$IDEXE -u -n`" = "$RUN_AS_USER" ] | |
then | |
echo "If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script." | |
exit 1 | |
fi | |
fi | |
} | |
getpid() { | |
if [ -f "$PIDFILE" ] | |
then | |
if [ -r "$PIDFILE" ] | |
then | |
pid=`cat "$PIDFILE"` | |
if [ "X$pid" != "X" ] | |
then | |
# It is possible that 'a' process with the pid exists but that it is not the | |
# correct process. This can happen in a number of cases, but the most | |
# common is during system startup after an unclean shutdown. | |
# The ps statement below looks for the specific wrapper command running as | |
# the pid. If it is not found then the pid file is considered to be stale. | |
pidtest=`$PSEXE -p $pid -o args | grep "wrapper.pidfile" | tail -1` | |
if [ "X$pidtest" = "X" ] | |
then | |
# This is a stale pid file. | |
rm -f "$PIDFILE" | |
echo "Removed stale pid file: $PIDFILE" | |
pid="" | |
fi | |
fi | |
else | |
echo "Cannot read $PIDFILE." | |
exit 1 | |
fi | |
fi | |
} | |
testpid() { | |
pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1` | |
if [ "X$pid" = "X" ] | |
then | |
# Process is gone so remove the pid file. | |
rm -f "$PIDFILE" | |
pid="" | |
fi | |
} | |
console() { | |
echo "Running $APP_LONG_NAME..." | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
# The string passed to eval must handles spaces in paths correctly. | |
COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=$APP_NAME wrapper.pidfile=\"$PIDFILE\" $ANCHORPROP $LOCKPROP" | |
eval $COMMAND_LINE | |
else | |
echo "$APP_LONG_NAME is already running." | |
exit 1 | |
fi | |
} | |
start() { | |
echo "Starting $APP_LONG_NAME..." | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
# The string passed to eval must handles spaces in paths correctly. | |
COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=$APP_NAME wrapper.pidfile=\"$PIDFILE\" wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $LOCKPROP" | |
eval $COMMAND_LINE | |
else | |
echo "$APP_LONG_NAME is already running." | |
exit 1 | |
fi | |
getpid | |
if [ "X$pid" != "X" ] | |
then | |
echo "Started $APP_LONG_NAME." | |
else | |
echo "Failed to start $APP_LONG_NAME." | |
fi | |
} | |
stopit() { | |
echo "Stopping $APP_LONG_NAME..." | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
echo "$APP_LONG_NAME was not running." | |
else | |
if [ "X$IGNORE_SIGNALS" = "X" ] | |
then | |
# Running so try to stop it. | |
kill $pid | |
if [ $? -ne 0 ] | |
then | |
# An explanation for the failure should have been given | |
echo "Unable to stop $APP_LONG_NAME." | |
exit 1 | |
fi | |
else | |
rm -f "$ANCHORFILE" | |
if [ -f "$ANCHORFILE" ] | |
then | |
# An explanation for the failure should have been given | |
echo "Unable to stop $APP_LONG_NAME." | |
exit 1 | |
fi | |
fi | |
# We can not predict how long it will take for the wrapper to | |
# actually stop as it depends on settings in wrapper.conf. | |
# Loop until it does. | |
savepid=$pid | |
CNT=0 | |
TOTCNT=0 | |
while [ "X$pid" != "X" ] | |
do | |
# Show a waiting message every 5 seconds. | |
if [ "$CNT" -lt "5" ] | |
then | |
CNT=`expr $CNT + 1` | |
else | |
echo "Waiting for $APP_LONG_NAME to exit..." | |
CNT=0 | |
fi | |
TOTCNT=`expr $TOTCNT + 1` | |
sleep 1 | |
testpid | |
done | |
pid=$savepid | |
testpid | |
if [ "X$pid" != "X" ] | |
then | |
echo "Failed to stop $APP_LONG_NAME." | |
exit 1 | |
else | |
echo "Stopped $APP_LONG_NAME." | |
fi | |
fi | |
} | |
status() { | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
echo "$APP_LONG_NAME is not running." | |
exit 1 | |
else | |
echo "$APP_LONG_NAME is running ($pid)." | |
exit 0 | |
fi | |
} | |
dump() { | |
echo "Dumping $APP_LONG_NAME..." | |
getpid | |
if [ "X$pid" = "X" ] | |
then | |
echo "$APP_LONG_NAME was not running." | |
else | |
kill -3 $pid | |
if [ $? -ne 0 ] | |
then | |
echo "Failed to dump $APP_LONG_NAME." | |
exit 1 | |
else | |
echo "Dumped $APP_LONG_NAME." | |
fi | |
fi | |
} | |
case "$1" in | |
'console') | |
checkUser touchlock $1 | |
console | |
;; | |
'start') | |
checkUser touchlock $1 | |
start | |
;; | |
'stop') | |
checkUser "" $1 | |
stopit | |
;; | |
'restart') | |
checkUser touchlock $1 | |
stopit | |
start | |
;; | |
'status') | |
checkUser "" $1 | |
status | |
;; | |
'dump') | |
checkUser "" $1 | |
dump | |
;; | |
*) | |
echo "Usage: $0 { console | start | stop | restart | status | dump }" | |
exit 1 | |
;; | |
esac | |
exit 0 |
This file contains 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
#! /bin/bash | |
# REDMINE | |
# Maintainer: @itxx00 | |
# Authors: itxx00@gmail.com | |
# App Version: 1.0 | |
### BEGIN INIT INFO | |
# Provides: redmine | |
# Required-Start: $local_fs $remote_fs $network $syslog | |
# Required-Stop: $local_fs $remote_fs $network $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Redmine project management | |
# Description: Redmine project management | |
### END INIT INFO | |
### Environment variables | |
RAILS_ENV="production" | |
app_root="/home/redmine/redmine" | |
app_user="redmine" | |
pid_path="$app_root/tmp/pids" | |
socket_path="$app_root/tmp/sockets" | |
web_server_pid_path="$pid_path/unicorn.pid" | |
# Switch to the app_user if it is not he/she who is running the script. | |
if [ "$USER" != "$app_user" ]; then | |
eval su - "$app_user" -c $(echo \")$0 "$@"$(echo \"); exit; | |
fi | |
# Switch to the gitlab path, if it fails exit with an error. | |
if ! cd "$app_root" ; then | |
echo "Failed to cd into $app_root, exiting!"; exit 1 | |
fi | |
### Init Script functions | |
check_pids(){ | |
if ! mkdir -p "$pid_path"; then | |
echo "Could not create the path $pid_path needed to store the pids." | |
exit 1 | |
fi | |
# If there exists a file which should hold the value of the Unicorn pid: read it. | |
if [ -f "$web_server_pid_path" ]; then | |
wpid=$(cat "$web_server_pid_path") | |
else | |
wpid=0 | |
fi | |
} | |
# We use the pids in so many parts of the script it makes sense to always check them. | |
# Only after start() is run should the pids change. Sidekiq sets it's own pid. | |
check_pids | |
# Checks whether the different parts of the service are already running or not. | |
check_status(){ | |
check_pids | |
# If the web server is running kill -0 $wpid returns true, or rather 0. | |
# Checks of *_status should only check for == 0 or != 0, never anything else. | |
if [ $wpid -ne 0 ]; then | |
kill -0 "$wpid" 2>/dev/null | |
web_status="$?" | |
else | |
web_status="-1" | |
fi | |
} | |
# Check for stale pids and remove them if necessary | |
check_stale_pids(){ | |
check_status | |
# If there is a pid it is something else than 0, the service is running if | |
# *_status is == 0. | |
if [ "$wpid" != "0" -a "$web_status" != "0" ]; then | |
echo "Removing stale Unicorn web server pid. This is most likely caused by the web server crashing the last time it ran." | |
if ! rm "$web_server_pid_path"; then | |
echo "Unable to remove stale pid, exiting" | |
exit 1 | |
fi | |
fi | |
} | |
# If no parts of the service is running, bail out. | |
exit_if_not_running(){ | |
check_stale_pids | |
if [ "$web_status" != "0" ]; then | |
echo "Redmine is not running." | |
exit | |
fi | |
} | |
# Starts Unicorn and Sidekiq. | |
start() { | |
check_stale_pids | |
# Then check if the service is running. If it is: don't start again. | |
if [ "$web_status" = "0" ]; then | |
echo "The Unicorn web server already running with pid $wpid, not restarting." | |
else | |
echo "Starting the Redmine Unicorn web server..." | |
# Remove old socket if it exists | |
rm -f "$socket_path"/gitlab.socket 2>/dev/null | |
# Start the webserver | |
RAILS_ENV=$RAILS_ENV script/web start | |
fi | |
sleep 2 | |
status | |
} | |
stop() { | |
exit_if_not_running | |
# If the Unicorn web server is running, tell it to stop; | |
if [ "$web_status" = "0" ]; then | |
RAILS_ENV=$RAILS_ENV script/web stop | |
echo "Stopping the Redmine Unicorn web server..." | |
stopping=true | |
else | |
echo "The Unicorn web was not running, doing nothing." | |
fi | |
# If something needs to be stopped, lets wait for it to stop. Never use SIGKILL in a script. | |
while [ "$stopping" = "true" ]; do | |
sleep 1 | |
check_status | |
if [ "$web_status" = "0" ]; then | |
printf "." | |
else | |
printf "\n" | |
break | |
fi | |
done | |
sleep 1 | |
# Cleaning up unused pids | |
rm "$web_server_pid_path" 2>/dev/null | |
status | |
} | |
# Returns the status of Redmine and it's components | |
status() { | |
check_status | |
if [ "$web_status" != "0" ]; then | |
echo "Redmine is not running." | |
return | |
fi | |
if [ "$web_status" = "0" ]; then | |
echo "The Redmine Unicorn webserver with pid $wpid is running." | |
printf "Redmine and all its components are \033[32mup and running\033[0m.\n" | |
else | |
printf "The Redmine Unicorn webserver is \033[31mnot running\033[0m.\n" | |
fi | |
} | |
reload(){ | |
exit_if_not_running | |
if [ "$wpid" = "0" ];then | |
echo "The Redmine Unicorn Web server is not running thus its configuration can't be reloaded." | |
exit 1 | |
fi | |
printf "Reloading Redmine Unicorn configuration... " | |
RAILS_ENV=$RAILS_ENV script/web reload | |
echo "Done." | |
sleep 2 | |
status | |
} | |
restart(){ | |
check_status | |
if [ "$web_status" = "0" ]; then | |
stop | |
fi | |
start | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
restart) | |
restart | |
;; | |
reload|force-reload) | |
reload | |
;; | |
status) | |
status | |
;; | |
*) | |
echo "Usage: service redmine {start|stop|restart|reload|status}" | |
exit 1 | |
;; | |
esac | |
exit |
This file contains 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
#!/bin/sh | |
# | |
# rc file for Sonar | |
# | |
# chkconfig: 345 96 10 | |
# description: Sonar system (www.sonarsource.org) | |
# | |
### BEGIN INIT INFO | |
# Provides: sonar | |
# Required-Start: $network | |
# Required-Stop: $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Sonar system (www.sonarsource.org) | |
# Description: Sonar system (www.sonarsource.org) | |
### END INIT INFO | |
sudo -u sonar -H /usr/bin/sonar $* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment