From 3e1a332c8344356453d284bda1e63af93034411c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 25 Jan 2025 00:06:45 -0500 Subject: [PATCH] refactor: update process handling code to conform to new norm - respect SDCARD_PATH - rename SUPPORTS_DAEMON_MODE to ONLY_LAUNCH_THEN_EXIT - add internal support for launching scripts via LAUNCHES_SCRIPT --- launch.sh | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/launch.sh b/launch.sh index 05c9e20..610e5d0 100644 --- a/launch.sh +++ b/launch.sh @@ -9,9 +9,10 @@ BUTTON_LOG="$progdir/log/buttons.log" SERVICE_NAME="sftpgo" HUMAN_READABLE_NAME="SFTPGo Server" -SUPPORTS_DAEMON_MODE=1 +ONLY_LAUNCH_THEN_EXIT=0 +LAUNCHES_SCRIPT="false" service_on() { - cd /mnt/SDCARD/ || exit + cd "$SDCARD_PATH" || exit 1 if [ -f "$progdir/log/service.log" ]; then mv "$progdir/log/service.log" "$progdir/log/service.log.old" fi @@ -148,11 +149,25 @@ wait_for_button() { done } +is_service_running() { + if pgrep "$SERVICE_NAME" >/dev/null 2>&1; then + return 0 + fi + + if [ "$LAUNCHES_SCRIPT" = "true" ]; then + if pgrep -fn "$SERVICE_NAME" >/dev/null 2>&1; then + return 0 + fi + fi + + return 1 +} + wait_for_service() { max_counter="$1" counter=0 - while ! pgrep "$SERVICE_NAME" >/dev/null 2>&1; do + while ! is_service_running; do counter=$((counter + 1)) if [ "$counter" -gt "$max_counter" ]; then return 1 @@ -163,7 +178,7 @@ wait_for_service() { main_daemonize() { echo "Toggling $SERVICE_NAME..." - if pgrep "$SERVICE_NAME"; then + if is_service_running; then show_message "Disabling the $HUMAN_READABLE_NAME" 2 service_off else @@ -180,7 +195,7 @@ main_daemonize() { } main_process() { - if pgrep "$SERVICE_NAME"; then + if is_service_running; then show_message "Disabling the $HUMAN_READABLE_NAME" 2 service_off fi @@ -208,7 +223,7 @@ main_process() { } main() { - if [ "$SUPPORTS_DAEMON_MODE" -eq 0 ]; then + if [ "$ONLY_LAUNCH_THEN_EXIT" -eq 1 ]; then service_on return $? fi