dracoox commited on
Commit
74b9dbd
·
verified ·
1 Parent(s): a4a67ac

Update run_tmate.sh

Browse files
Files changed (1) hide show
  1. run_tmate.sh +24 -59
run_tmate.sh CHANGED
@@ -1,64 +1,29 @@
1
  #!/bin/bash
2
 
3
- # Path to log file
4
- LOGFILE="/home/draco/tmate.txt"
5
-
6
- # Start tmate session function
7
- start_tmate() {
8
- echo "[tmate-log] Launching new tmate session..." | tee -a "$LOGFILE"
9
- tmate -S /tmp/tmate.sock new-session -d
10
- tmate -S /tmp/tmate.sock wait tmate-ready
11
-
12
- echo "[tmate-log] SSH: $(tmate -S /tmp/tmate.sock display -p "#{tmate_ssh}")" | tee -a "$LOGFILE"
13
- echo "[tmate-log] Web: $(tmate -S /tmp/tmate.sock display -p "#{tmate_web}")" | tee -a "$LOGFILE"
14
- echo "[tmate-log] Monitoring connection (will restart in 1.5 hours)..." | tee -a "$LOGFILE"
15
- }
16
-
17
- # Kill tmate session function
18
- kill_tmate() {
19
- echo "[tmate-log] Killing tmate session..." | tee -a "$LOGFILE"
20
- pkill -f "tmate -S /tmp/tmate.sock"
21
- }
22
-
23
- # Start a background netcat HTTP server to listen for reset POSTs
24
- start_http_server() {
25
- while true; do
26
- # Listen for a single connection, timeout 10 sec to avoid hanging
27
- REQUEST=$(timeout 10 nc -l -p 7860)
28
-
29
- # Check if POST /reset-terminal is requested
30
- if echo "$REQUEST" | grep -q "^POST /reset-terminal "; then
31
- echo -e "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"message\":\"Terminal reset done\"}"
32
- kill_tmate
33
- else
34
- # Return 404 for anything else
35
- echo -e "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\nNot Found"
36
- fi
37
- done
38
- }
39
-
40
- # Start the HTTP server in background
41
- start_http_server &
42
-
43
- # Store HTTP server PID to optionally kill later
44
- HTTP_PID=$!
45
-
46
- # Main tmate loop with 1.5h restart
47
  while true; do
48
- start_tmate
49
-
50
- start_time=$(date +%s)
51
- while pgrep -f "tmate -S /tmp/tmate.sock" > /dev/null; do
52
- current_time=$(date +%s)
53
- elapsed=$((current_time - start_time))
54
- if [ $elapsed -ge 5400 ]; then
55
- echo "[tmate-log] 1.5 hours passed. Restarting session..." | tee -a "$LOGFILE"
56
- kill_tmate
57
- break
58
- fi
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  sleep 5
60
- done
61
-
62
- echo "[tmate-log] ⚠️ Connection lost or timeout. Reconnecting in 5 seconds..." | tee -a "$LOGFILE"
63
- sleep 5
64
  done
 
1
  #!/bin/bash
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  while true; do
4
+ echo "[tmate-log] Launching new tmate session..." | tee /home/draco/tmate.txt
5
+
6
+ # Start tmate session
7
+ tmate -S /tmp/tmate.sock new-session -d
8
+ tmate -S /tmp/tmate.sock wait tmate-ready
9
+
10
+ echo "[tmate-log] SSH: $(tmate -S /tmp/tmate.sock display -p "#{tmate_ssh}")" | tee -a /home/draco/tmate.txt
11
+ echo "[tmate-log] Web: $(tmate -S /tmp/tmate.sock display -p "#{tmate_web}")" | tee -a /home/draco/tmate.txt
12
+ echo "[tmate-log] Monitoring connection (will restart in 3 hours)..." | tee -a /home/draco/tmate.txt
13
+
14
+ # Monitor for 3 hours or until tmate process exits
15
+ start_time=$(date +%s)
16
+ while pgrep -f "tmate -S /tmp/tmate.sock" > /dev/null; do
17
+ current_time=$(date +%s)
18
+ elapsed=$((current_time - start_time))
19
+ if [ $elapsed -ge 10800 ]; then
20
+ echo "[tmate-log] ⏰ 3 hours passed. Restarting session..." | tee -a /home/draco/tmate.txt
21
+ pkill -f "tmate -S /tmp/tmate.sock"
22
+ break
23
+ fi
24
+ sleep 5
25
+ done
26
+
27
+ echo "[tmate-log] ⚠️ Connection lost or timeout. Reconnecting in 5 seconds..." | tee -a /home/draco/tmate.txt
28
  sleep 5
 
 
 
 
29
  done