go-guerrilla icon indicating copy to clipboard operation
go-guerrilla copied to clipboard

[Ubuntu 16.04] systemd service unit + monit task

Open lord-alfred opened this issue 6 years ago • 2 comments

I made a small script/service to start and stop guerrillad after system startup (and a little more). This is my first experience, so I will be glad to hear comments. :)

#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides:          guerrillad
# Required-Start:    $remote_fs $syslog $mysqld
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: mini smtp server
# Description:       mini smtp server guerrillad
### END INIT INFO

DESC="mini smtp server"
NAME="guerillad"

# some configuration variables (change to your!)
USER="guser"
CONFIG="/etc/guerrilla.conf.json"
DAEMON_FILE="/usr/local/bin/guerrillad"
DAEMON_LOG="/var/log/guerrilla/daemon.log"
PID_FILE="/var/run/guerrillad/guerrillad.pid"

PID_DIR=$(dirname $PID_FILE)

start_service() {
    # create directory (/var/run - is tmpfs, after reboot directory is removed)
    mkdir -p $PID_DIR
    chown -R $USER:$USER $PID_DIR

    # wait mysql start (fix "dial tcp 127.0.0.1:3306: connect: connection refused" in logs)
    sleep_iter=0
    sleep_max=30
    # mysql host and port (change if you use another!)
    until nc -z -v -w30 127.0.0.1 3306
    do
        if [ $sleep_iter -ge $sleep_max ]; then
            echo "sleep iter greater than max: $sleep_max"
            break
        fi

        echo "Waiting for database connection... iter: $sleep_iter"
        # wait for 1 seconds before check again
        sleep 1

        sleep_iter=$(expr $sleep_iter + 1)
    done

    # run guerrillad daemon
    sudo -i -u $USER $DAEMON_FILE -c $CONFIG serve >> $DAEMON_LOG 2>&1 &
}

stop_service() {
    PID=$(cat $PID_FILE)
    /bin/kill -TERM $PID
    RETVAL="$?"
    return "$RETVAL"
}

reload_config() {
    PID=$(cat $PID_FILE)
    /bin/kill -HUP $PID
    return 0
}

rotate_logs() {
    PID=$(cat $PID_FILE)
    /bin/kill -USR1 $PID
    return 0
}

case "$1" in
        start)
                echo "Starting $DESC"
                start_service
                echo "start exit code: $?"
                ;;
        stop)
                echo "Stopping $DESC"
                stop_service
                echo "stop exit code: $?"
                ;;
        restart)
                echo "Restarting $DESC"
                stop_service
                sleep 1
                start_service
                echo "restart exit code: $?"
                ;;
        reload)
                echo "Reloading $DESC configuration"
                reload_config
                echo "reloading configuration exit code: $?"
                ;;
        rotate)
                echo "Re-opening $DESC log files"
                rotate_logs
                echo "re-opening log files exit code: $?"
                ;;
        *)
                echo "Usage: $NAME {start|stop|restart|reload|rotate}" >&2
                exit 3
                ;;
esac

How to use it:

  1. create file and put this script: sudo vim /etc/init.d/guerrillad
  2. update services: update-rc.d guerrillad defaults
  3. run service: systemctl start guerrillad
  4. If you change /etc/init.d/guerrillad - reload service: systemctl daemon-reload

Use:

  • start: /bin/systemctl start guerrillad
  • stop: /bin/systemctl stop guerrillad
  • restart: /bin/systemctl restart guerrillad
  • reload (config): /bin/systemctl reload guerrillad
  • status (with sudo for to view logs): sudo /bin/systemctl status guerrillad
  • rotate (reopen log files): service guerrillad rotate (not systemctl rotate(!))

Bonus: monit task:

check process guerrillad with pidfile /var/run/guerrillad/guerrillad.pid
    start program = "/bin/systemctl start guerrillad"
    stop program = "/bin/systemctl stop guerrillad"
    if failed host 127.0.0.1 port 25 protocol smtp then restart
    if cpu > 60% for 2 cycles then alert
    if cpu > 80% for 5 cycles then restart
    if 5 restarts within 5 cycles then timeout
    group service

lord-alfred avatar Aug 06 '19 17:08 lord-alfred

Awesome! Do you allow me to add this to the wiki?

Have you tested the rotate and reload options? Sorry, out of time to try myself.

flashmob avatar Aug 15 '19 09:08 flashmob

Yes of course you can add this to the wiki! :)

I tested this options and they work (but I tested this only on Ubuntu 16.04)

lord-alfred avatar Aug 15 '19 10:08 lord-alfred