Skip to content

Commit

Permalink
Merge pull request #1 from josegonzalez/update-process-handling
Browse files Browse the repository at this point in the history
refactor: update process handling code to conform to new norm
  • Loading branch information
josegonzalez authored Jan 25, 2025
2 parents 63d5928 + 3e1a332 commit 6a2fe7f
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6a2fe7f

Please sign in to comment.