Skip to content

Instantly share code, notes, and snippets.

@BetsuNo
Last active May 10, 2022 15:29
Show Gist options
  • Save BetsuNo/331f6ecc3b0bc5b3997bf2098235ae2e to your computer and use it in GitHub Desktop.
Save BetsuNo/331f6ecc3b0bc5b3997bf2098235ae2e to your computer and use it in GitHub Desktop.
nginx, php-fpm, memcached and centrifugo rc.d scripts for CentOS 7
#!/bin/bash
#
# /etc/init.d/centrifugo
#
# Startup script for centrifugo
#
# chkconfig: 2345 20 80
# description: Starts and stops centrifugo
# Source function library.
if [ -f /etc/init.d/functions ] ; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 1
fi
# Daemon options
CENTRIFUGO_OPTS=
# Process name
NAME=centrifugo
USER=centrifugo
GROUP=centrifugo
# Daemon name, where is the actual executable
# If the daemon is not there, then exit.
DAEMON=/usr/bin/centrifugo
# Configuration file
CONFIG=/etc/centrifugo/config.json
# Max open files
OPEN_FILE_LIMIT=65536
# Logging
if [ -z "$STDOUT" ]; then
STDOUT=/var/log/centrifugo/centrifugo.log
fi
if [ ! -f "$STDOUT" ]; then
mkdir -p $(dirname $STDOUT)
fi
if [ -z "$STDERR" ]; then
STDERR=/var/log/centrifugo/centrifugo.log
fi
if [ ! -f "$STDERR" ]; then
mkdir -p $(dirname $STDERR)
fi
lockfile="/var/lock/centrifugo"
pidfile="/var/run/centrifugo.pid"
[ -e /etc/sysconfig/centrifugo ] && . /etc/sysconfig/centrifugo
if ! [ -x $DAEMON ]; then
echo "$DAEMON binary not found."
exit 5
fi
start() {
if ! [ -f $CONFIG ]; then
echo "configuration file not found $CONFIG"
exit 6
fi
# Bump the file limits, before launching the daemon.
ulimit -n $OPEN_FILE_LIMIT
if [ $? -ne 0 ]; then
exit 1
fi
configtest || return $?
echo -n $"Starting $NAME: "
daemon --pidfile=$pidfile --user centrifugo --check=$DAEMON "nohup $DAEMON -c $CONFIG $CENTRIFUGO_OPTS >>$STDOUT 2>&1 &"
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
pgrep -f "$DAEMON -c $CONFIG" > $pidfile
return $retval
}
stop() {
echo -n $"Stopping $NAME: "
killproc -p $pidfile $NAME -TERM
retval=$?
if [ $retval -eq 0 ]; then
if [ "$CONSOLETYPE" != "serial" ]; then
echo -en "\\033[16G"
fi
while rh_status_q
do
sleep 1
echo -n $"."
done
rm -f $lockfile
fi
echo
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $NAME: "
killproc $DAEMON -HUP
sleep 1
RETVAL=$?
echo
}
configtest() {
$DAEMON checkconfig -c $CONFIG
}
rh_status() {
status -p $pidfile $NAME
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
status|status_q)
rh_$1
;;
condrestart)
rh_status_q && restart || exit 0
;;
*)
echo $"Usage: $0 {start|stop|reload|status|restart|configtest|condrestart}"
exit 2
esac
#! /bin/sh
#
# chkconfig: - 55 45
# description: The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached
# Source function library.
. /etc/rc.d/init.d/functions
PORT=11211
USER=memcached
MAXCONN=1024
CACHESIZE=64
OPTIONS=""
if [ -f /etc/sysconfig/memcached ];then
. /etc/sysconfig/memcached
fi
# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
exit 0
fi
RETVAL=0
memcached=/usr/bin/memcached
prog=$(basename $memcached)
PID_FILE="/var/run/memcached/memcached.pid"
LOCK_FILE="/var/lock/memcached"
start () {
echo -n $"Starting $prog: "
daemon $memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P $PID_FILE $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
}
stop () {
echo -n $"Stopping $prog: "
killproc $memcached
RETVAL=$?
echo
if [ $RETVAL -eq 0 ] ; then
rm -f $LOCK_FILE
rm -f $PID_FILE
fi
}
restart () {
stop
start
}
# See how we were called.
case "$1" in
start)
$1
;;
stop)
$1
;;
status)
$1 $memcached
;;
restart|reload)
restart
;;
condrestart)
[ -f $LOCK_FILE ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
exit 1
esac
exit $?
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
#. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile=/var/lock/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE >/dev/null 2>&1
}
rh_status() {
status -p $PID_FILE -l $LOCK_FILE $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
#!/bin/sh
#
# php-fpm - this script starts and stops the php-fpm daemin
#
# chkconfig: - 85 15
# description: PHP-FPM (FastCGI Process Manager) is an alternative
# PHP FastCGI implementation
# processname: php-fpm
# config: /etc/php-fpm.conf
# pidfile: /var/run/php-fpm.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
phpfpm="/usr/sbin/php-fpm"
prog=$(basename $phpfpm)
CONF_FILE="/etc/php-fpm.conf"
PID_FILE="/var/run/php-fpm.pid"
LOCK_FILE="/var/lock/nginx"
start() {
[ -x $phpfpm ] || exit 5
[ -f $CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $phpfpm -y $CONF_FILE -g $PID_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $LOCK_FILE
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $LOCK_FILE
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $phpfpm -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$phpfpm -t --fpm-config $CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
@cautionbug
Copy link

cautionbug commented Oct 30, 2019

These scripts are lifesavers. Trying to cobble together a CentOS 7 system running through WSL, and since D-Bus isn't supported (yet?) i needed your workaround mentioned over here.

Suggested change:
In the nginx script, the rh_status function should be the same as the one in php-fpm. The init.d/functions status function already provides the desired output, and the script doesn't specify a PID file anyway. i tried adding one and discovered it's redundant.

Many thanks!!! 👍

@BetsuNo
Copy link
Author

BetsuNo commented Oct 30, 2019

Suggested change:
In the nginx script, the rh_status function should be the same as the one in php-fpm. The init.d/functions status function already provides the desired output, and the script doesn't specify a PID file anyway. i tried adding one and discovered it's redundant.

Sorry, at the moment I have not an environment to test it, but thank you for your reply and suggestion.

@cautionbug
Copy link

No worries. i did make the change in my environment and it works correctly.
In any case, thanks very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment