|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# wayang-service: Process Watchdog Script |
| 19 | +# |
| 20 | +# Usage: wayang-service {start|stop|restart|status} |
| 21 | +# |
| 22 | + |
| 23 | +# Get the absolute path of the script |
| 24 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 25 | +PID_DIR="$SCRIPT_DIR/pid" |
| 26 | +PID_FILE="$PID_DIR/wayang-service.pid" |
| 27 | +LOG_DIR="$SCRIPT_DIR/logs" |
| 28 | +EXECUTABLE="$SCRIPT_DIR/wayang-submit" |
| 29 | +ARGS="org.apache.wayang.api.json.Main" |
| 30 | +SLEEP_INTERVAL=5 |
| 31 | + |
| 32 | +# Ensure necessary directories exist |
| 33 | +mkdir -p "$PID_DIR" "$LOG_DIR" |
| 34 | + |
| 35 | +start_service() { |
| 36 | + if [ -f "$PID_FILE" ]; then |
| 37 | + PID=$(cat "$PID_FILE") |
| 38 | + if kill -0 "$PID" >/dev/null 2>&1; then |
| 39 | + echo "Wayang-service is already running with PID $PID." |
| 40 | + exit 1 |
| 41 | + else |
| 42 | + echo "Removing stale PID file." |
| 43 | + rm -f "$PID_FILE" |
| 44 | + fi |
| 45 | + fi |
| 46 | + |
| 47 | + echo "Starting Wayang-service..." |
| 48 | + nohup "$SCRIPT_DIR/$(basename "$0")" run >> "$LOG_DIR/wayang-$(date +'%Y-%m-%d_%H-%M-%S').log" 2>&1 & |
| 49 | + PID=$! |
| 50 | + echo $PID > "$PID_FILE" |
| 51 | + echo "Wayang-service started with PID $PID." |
| 52 | +} |
| 53 | + |
| 54 | +run_service() { |
| 55 | + while true; do |
| 56 | + # Define a new log file for each process restart |
| 57 | + SUB_LOG_FILE="$LOG_DIR/wayang-$(date +'%Y-%m-%d_%H-%M-%S').log" |
| 58 | + echo "Starting subprocess; logging to $SUB_LOG_FILE" |
| 59 | + "$EXECUTABLE" $ARGS >> "$SUB_LOG_FILE" 2>&1 & |
| 60 | + CHILD_PID=$! |
| 61 | + echo "Subprocess started with PID $CHILD_PID." |
| 62 | + |
| 63 | + # Wait for the process to exit |
| 64 | + wait $CHILD_PID |
| 65 | + EXIT_CODE=$? |
| 66 | + echo "Subprocess (PID $CHILD_PID) terminated with exit code $EXIT_CODE." |
| 67 | + echo "Restarting subprocess in $SLEEP_INTERVAL seconds..." |
| 68 | + sleep $SLEEP_INTERVAL |
| 69 | + done |
| 70 | +} |
| 71 | + |
| 72 | +stop_service() { |
| 73 | + if [ -f "$PID_FILE" ]; then |
| 74 | + PID=$(cat "$PID_FILE") |
| 75 | + if kill -0 "$PID" >/dev/null 2>&1; then |
| 76 | + echo "Stopping Wayang-service (PID $PID)..." |
| 77 | + kill "$PID" |
| 78 | + sleep 1 |
| 79 | + rm -f "$PID_FILE" |
| 80 | + echo "Wayang-service stopped." |
| 81 | + else |
| 82 | + echo "No process found with PID $PID. Removing stale PID file." |
| 83 | + rm -f "$PID_FILE" |
| 84 | + fi |
| 85 | + else |
| 86 | + echo "Wayang-service is not running." |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +status_service() { |
| 91 | + if [ -f "$PID_FILE" ]; then |
| 92 | + PID=$(cat "$PID_FILE") |
| 93 | + if kill -0 "$PID" >/dev/null 2>&1; then |
| 94 | + echo "Wayang-service is running with PID $PID." |
| 95 | + else |
| 96 | + echo "PID file exists, but no process with PID $PID is running." |
| 97 | + fi |
| 98 | + else |
| 99 | + echo "Wayang-service is not running." |
| 100 | + fi |
| 101 | +} |
| 102 | + |
| 103 | +case "$1" in |
| 104 | + start) |
| 105 | + start_service |
| 106 | + ;; |
| 107 | + run) |
| 108 | + run_service |
| 109 | + ;; |
| 110 | + stop) |
| 111 | + stop_service |
| 112 | + ;; |
| 113 | + restart) |
| 114 | + stop_service |
| 115 | + sleep 1 |
| 116 | + start_service |
| 117 | + ;; |
| 118 | + status) |
| 119 | + status_service |
| 120 | + ;; |
| 121 | + *) |
| 122 | + echo "Usage: $0 {start|stop|restart|status}" |
| 123 | + exit 1 |
| 124 | + ;; |
| 125 | +esac |
0 commit comments