From 4dbef0734e7b02d3fc7e6433cf7dccdb9f694fdc Mon Sep 17 00:00:00 2001 From: Fierelier Date: Tue, 23 Apr 2024 05:54:34 +0200 Subject: [PATCH] Add the ability for services to require others --- app/bin/screen_exists | 3 --- app/bin/service_running | 9 +++++++++ app/bin/start | 17 +++++++++++++---- app/bin/stop | 9 ++++++--- services/hello_world/settings | 2 ++ 5 files changed, 30 insertions(+), 10 deletions(-) delete mode 100644 app/bin/screen_exists create mode 100644 app/bin/service_running diff --git a/app/bin/screen_exists b/app/bin/screen_exists deleted file mode 100644 index 90de810..0000000 --- a/app/bin/screen_exists +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash -screen -ls "$1" >/dev/null 2>&1 -echo $? diff --git a/app/bin/service_running b/app/bin/service_running new file mode 100644 index 0000000..f593cd2 --- /dev/null +++ b/app/bin/service_running @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +"$APP" onearg "$@" +"$APP" exist "$1" +source "$APP" service_env "$1" +ps -u "$SERVICE_USER" +if [ "$?" = "0" ]; then + exit 0 +fi +exit 1 diff --git a/app/bin/start b/app/bin/start index 2810ff2..3485991 100644 --- a/app/bin/start +++ b/app/bin/start @@ -3,18 +3,24 @@ "$APP" exist "$1" source "$APP" service_env "$1" +for SERVICE in "${SERVICE_REQUIRE:@}"; do + "$APP" start "$SERVICE" +done + echo "[$APP_NAME] Starting '$1' ..." if [ "$SERVICE_RUN" = "" ]; then exit 0 fi -set +e -ps -u "$SERVICE_USER" -if [ "$?" = "0" ]; then +if "$APP" service_running "$1"; then echo "Service is already running." exit 0 fi -set -e + +if [ -e "$APP_SERVICES/$1/start" ]; then + source "$APP_SERVICES/$1/start" +fi + SERVICE_RUN_RAW="cd \"$SERVICE_CD\"; exec ${SERVICE_RUN//"/\\"}" if [ "$SERVICE_SCREEN" = "1" ]; then screen -dmS "$SERVICE_SCREEN_NAME" gosu "$SERVICE_USER" bash -c "$SERVICE_RUN_RAW" @@ -22,3 +28,6 @@ else gosu "$SERVICE_USER" nohup bash -c "$SERVICE_RUN_RAW" > /dev/null 2>&1 & fi +if [ -e "$APP_SERVICES/$1/await" ]; then + source "$APP_SERVICES/$1/await" +fi diff --git a/app/bin/stop b/app/bin/stop index fcee1c2..1a61698 100644 --- a/app/bin/stop +++ b/app/bin/stop @@ -7,13 +7,16 @@ source "$APP" service_env "$1" if [ "$SERVICE_STOP" = "0" ]; then exit 0 fi -set +e -ps -u "$SERVICE_USER" -if ! [ "$?" = "0" ]; then + +if ! "$APP" service_running "$1"; then echo "Service already stopped." exit 0 fi +if [ -e "$APP_SERVICES/$1/stop" ]; then + source "$APP_SERVICES/$1/stop" +fi + if [ "$SERVICE_SCREEN" = "1" ] && ! [ "$SERVICE_SCREEN_EXIT_COMMAND" = "" ]; then echo "[$APP_NAME] * Sending backspaces to screen ..." if [ "$SERVICE_SCREEN_EXIT_BACKSPACE" = "1" ]; then diff --git a/services/hello_world/settings b/services/hello_world/settings index 924f1fb..5a7e0ee 100644 --- a/services/hello_world/settings +++ b/services/hello_world/settings @@ -1,4 +1,6 @@ #!/bin/sh +# If defined, the service will wait for other services to be started +SERVICE_REQUIRE=() # If command is defined, it will create and run a service unit SERVICE_RUN='./hello' # Working directory