From 2b46d9607598e24e792d04115151b906e4cf0f47 Mon Sep 17 00:00:00 2001 From: crowetic Date: Thu, 24 Oct 2024 14:48:12 -0700 Subject: [PATCH] added logging and additional checks for screen --- restart-qortal-every-2-hours.sh | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/restart-qortal-every-2-hours.sh b/restart-qortal-every-2-hours.sh index fa9695b..7876a46 100644 --- a/restart-qortal-every-2-hours.sh +++ b/restart-qortal-every-2-hours.sh @@ -2,23 +2,32 @@ # Path to the Qortal folder QORTAL_DIR=~/qortal +LOG_FILE="$QORTAL_DIR/restart_log.txt" + +# Logging function +log() { + echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" +} # Check if screen is installed if command -v screen &> /dev/null; then - echo "Screen is installed, running script in a screen session..." + log "Screen is installed, running script in a screen session..." SCRIPT_NAME="restart-qortal-every-2-hours.sh" - cp "$(dirname "$BASH_SOURCE")/$SCRIPT_NAME" "$QORTAL_DIR/$SCRIPT_NAME" - screen -S qortal_restart -dm bash "$QORTAL_DIR/$SCRIPT_NAME" + SCRIPT_PATH="$(realpath "$BASH_SOURCE")" + cp "$SCRIPT_PATH" "$QORTAL_DIR/$SCRIPT_NAME" + screen -S qortal_restart -dm bash -c "cd $QORTAL_DIR && bash $SCRIPT_NAME" exit 0 else - echo "Screen is not installed, running script normally..." + log "Screen is not installed, running script normally..." fi while true; do # Navigate to Qortal directory - cd "$QORTAL_DIR" || exit + log "Navigating to Qortal directory..." + cd "$QORTAL_DIR" || { log "Failed to navigate to Qortal directory."; exit 1; } # Stop Qortal core + log "Stopping Qortal core..." ./stop.sh &> stop_output.log & stop_pid=$! @@ -27,23 +36,26 @@ while true; do # Check if stop script succeeded if ! grep -q "Qortal ended gracefully" stop_output.log; then - # Stop script did not complete successfully, kill Java process - echo "Stop script did not complete successfully, force killing Java..." + log "Stop script did not complete successfully, force killing Java..." killall -9 java # Remove blockchain lock file + log "Removing blockchain lock file..." rm -rf "$QORTAL_DIR/db/blockchain.lck" else - echo "Qortal stopped gracefully." + log "Qortal stopped gracefully." fi # Ensure stop process completes + log "Waiting for stop process to complete..." wait $stop_pid # Start Qortal core + log "Starting Qortal core..." ./start.sh - # Wait for 2 hours while logging output --- CHANGE THE NUMBER OF HOURS HERE. + # Wait for 2 hours while logging output + log "Waiting for 2 hours before restarting..." sleep 2h & sleep_pid=$! tail -f "$QORTAL_DIR/qortal.log" & @@ -51,6 +63,7 @@ while true; do # Wait for the sleep to finish, then kill the tail process wait $sleep_pid + log "2-hour wait complete, killing tail process..." kill $tail_pid done