Skip to content

Commit

Permalink
Merge pull request #14 from companieshouse/feature/daemon-management-…
Browse files Browse the repository at this point in the history
…scripts

Add daemon management scripts
  • Loading branch information
marcransome authored Jun 8, 2022
2 parents 9adadca + 01b77dd commit c969809
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
45 changes: 45 additions & 0 deletions scripts/mncq_start.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# Start mncq daemon.

# -- Internal variables -------------------------------------------------------

script_name=$(basename "$0")

default_interval="60"
default_log_file="{{ tuxedo_logs_path }}/${USER}/cabsQTX.log"
default_log_level="3"

# -- General ------------------------------------------------------------------

check_user () {
if [[ "${USER}" == "root" ]]; then
echo "${script_name}: This script should be executed as a Tuxedo service user not root"
exit 1
fi
}

check_daemon () {
if [[ -n "$(pgrep -u ${USER} mncq)" ]]; then
echo "${script_name}: mncq daemon is already running" >&2
exit 1
fi
}

start_daemon () {
local interval="${1:-${default_interval}}"
local log_level="${2:-${default_log_level}}"
local log_file="${3:-${default_log_file}}"

mncq "${interval}" "${log_level}" "${log_file}"
}

# -- Entrypoint ---------------------------------------------------------------

main () {
check_user
check_daemon
start_daemon "${@}"
}

main "${@}"
56 changes: 56 additions & 0 deletions scripts/mncq_stop.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

# Stop mncq daemon.

# -- Internal variables -------------------------------------------------------

script_name=$(basename "$0")

max_pid_wait_seconds="10"

# -- General ------------------------------------------------------------------

check_user () {
if [[ "${USER}" == "root" ]]; then
echo "${script_name}: This script should be executed as a Tuxedo service user not root"
exit 1
fi
}

check_daemon () {
if [[ -z "$(pgrep -u ${USER} mncq)" ]]; then
echo "${script_name}: mncq daemon is not running" >&2
exit 1
fi
}

stop_daemon () {
local daemon_pid="$(pgrep -u ${USER} mncq)"

echo "${script_name}: stopping mncq daemon"
kill -s SIGTERM "${daemon_pid}"

local pid_wait_attempts=0

while $(kill -0 "${daemon_pid}" 2>/dev/null); do
if [[ "${pid_wait_attempts}" -gt "${max_pid_wait_seconds}" ]]; then
echo "${script_name}: could not stop mncq daemon"
exit 1
fi

((pid_wait_attempts++))
sleep 1
done

echo "${script_name}: mncq daemon stopped"
}

# -- Entrypoint ---------------------------------------------------------------

main () {
check_user
check_daemon
stop_daemon
}

main "${@}"
37 changes: 37 additions & 0 deletions scripts/packq_start.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# Start packq daemon.

# -- Internal variables -------------------------------------------------------

script_name=$(basename "$0")

default_interval="60"
default_log_file="{{ tuxedo_logs_path }}/${USER}/packQTX.log"
default_log_level="3"

# -- General ------------------------------------------------------------------

check_daemon () {
if [[ -n "$(pgrep -u ${USER} packq)" ]]; then
echo "${script_name}: packq daemon is already running" >&2
exit 1
fi
}

start_daemon () {
local interval="${1:-${default_interval}}"
local log_level="${2:-${default_log_level}}"
local log_file="${3:-${default_log_file}}"

packq "${interval}" "${log_level}" "${log_file}"
}

# -- Entrypoint ---------------------------------------------------------------

main () {
check_daemon
start_daemon "${@}"
}

main "${@}"
48 changes: 48 additions & 0 deletions scripts/packq_stop.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# Stop packq daemon.

# -- Internal variables -------------------------------------------------------

script_name=$(basename "$0")

max_pid_wait_seconds="10"

# -- General ------------------------------------------------------------------

check_daemon () {
if [[ -z "$(pgrep -u ${USER} packq)" ]]; then
echo "${script_name}: packq daemon is not running" >&2
exit 1
fi
}

stop_daemon () {
local daemon_pid="$(pgrep -u ${USER} packq)"

echo "${script_name}: stopping packq daemon"
kill -s SIGTERM "${daemon_pid}"

local pid_wait_attempts=0

while $(kill -0 "${daemon_pid}" 2>/dev/null); do
if [[ "${pid_wait_attempts}" -gt "${max_pid_wait_seconds}" ]]; then
echo "${script_name}: could not stop packq daemon"
exit 1
fi

((pid_wait_attempts++))
sleep 1
done

echo "${script_name}: packq daemon stopped"
}

# -- Entrypoint ---------------------------------------------------------------

main () {
check_daemon
stop_daemon
}

main "${@}"

0 comments on commit c969809

Please sign in to comment.