forked from crowetic/QORTector-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer-count-auto-restart.sh
More file actions
executable file
·96 lines (82 loc) · 2.86 KB
/
peer-count-auto-restart.sh
File metadata and controls
executable file
·96 lines (82 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# Usage: ./peer-count-auto-restart.sh [acceptable_number_of_peers]
# Check if acceptable peer count is provided
if [ -z "$1" ]; then
ACCEPTABLE_PEERS=10
log "acceptable number of peers is set to: '$ACCEPTABLE_PEERS'"
echo "No acceptable peer count provided. Using default: $ACCEPTABLE_PEERS"
else
ACCEPTABLE_PEERS=$1
fi
QORTAL_DIR="$HOME/qortal" # Default path to Qortal directory
LOG_FILE="$QORTAL_DIR/peer_count_restart_log.txt"
# Check if screen exists
if command -v screen &> /dev/null; then
USE_SCREEN=true
else
USE_SCREEN=false
fi
# Log function
log() {
if [ -n "$LOG_FILE" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
else
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
fi
}
# Function to get number of connections
get_number_of_connections() {
local response=$(curl -s localhost:12391/admin/status)
if command -v jq &> /dev/null; then
echo "$response" | jq '.numberOfConnections'
else
echo "$response" | sed -n 's/.*"numberOfConnections":\([0-9]*\).*/\1/p'
fi
}
# Function to restart Qortal
restart_qortal() {
log "Restarting Qortal..."
cd "$QORTAL_DIR" || {
log "Failed to change directory to $QORTAL_DIR"
exit 1
}
./stop.sh
sleep 45
./start.sh
log "Qortal restarted."
}
# Main loop function
main_loop() {
log "Sleeping 125 seconds until first peer check..."
log "Number of acceptable peers set: $ACCEPTABLE_PEERS"
sleep 125 # Wait for 125 seconds before starting checks
while true; do
number_of_connections=$(get_number_of_connections)
if [ -z "$number_of_connections" ]; then
log "Failed to retrieve number of connections."
elif [[ "$number_of_connections" =~ ^[0-9]+$ ]] && [ "$number_of_connections" -lt "$ACCEPTABLE_PEERS" ]; then
log "Number of connections ($number_of_connections) is below acceptable threshold ($ACCEPTABLE_PEERS). Restarting Qortal..."
restart_qortal
else
log "Number of connections: ($number_of_connections) - is acceptable. No restarting needed..."
log "Set peer count: $ACCEPTABLE_PEERS"
fi
sleep 600 # Wait for 10 minutes before next check
done
}
# Run the script in a screen session if available
if [ "$USE_SCREEN" = true ]; then
if screen -list | grep -q "peer_count_monitor"; then
log "Screen session 'peer_count_monitor' already exists. Attaching to existing session."
log "Acceptable peer count set: $ACCEPTABLE_PEERS"
screen -x peer_count_monitor
else
screen -dmS peer_count_monitor bash -c "$(declare -f log get_number_of_connections restart_qortal main_loop); ACCEPTABLE_PEERS=$ACCEPTABLE_PEERS; QORTAL_DIR=$QORTAL_DIR; LOG_FILE=$LOG_FILE; main_loop"
log "Started in a new screen session."
log "Acceptable peer count set: $ACCEPTABLE_PEERS"
fi
else
log "Screen not found. Running in the current session."
log "Acceptable peer count set: $ACCEPTABLE_PEERS"
main_loop
fi