CoolFace
Apppublic

llzai/axonhub

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
stop.sh227 linesDownload Raw Back to deploy
1#!/bin/bash
2
3# AxonHub Stop Script
4# This script stops AxonHub directly (no systemd), with proper error handling
5
6set -e
7
8# Colors for output
9RED='\033[0;31m'
10GREEN='\033[0;32m'
11YELLOW='\033[1;33m'
12BLUE='\033[0;34m'
13NC='\033[0m' # No Color
14
15# Configuration
16SERVICE_NAME="axonhub"
17# Resolve non-root user's HOME when running via sudo
18if [[ -n "$SUDO_USER" && "$SUDO_USER" != "root" ]]; then
19    USER_HOME="$(eval echo ~${SUDO_USER})"
20else
21    USER_HOME="$HOME"
22fi
23BASE_DIR="${USER_HOME}/.config/axonhub"
24PID_FILE="${BASE_DIR}/axonhub.pid"
25PROCESS_NAME="axonhub"
26
27print_info() {
28    echo -e "${BLUE}[INFO]${NC} $1"
29}
30
31print_success() {
32    echo -e "${GREEN}[SUCCESS]${NC} $1"
33}
34
35print_warning() {
36    echo -e "${YELLOW}[WARNING]${NC} $1"
37}
38
39print_error() {
40    echo -e "${RED}[ERROR]${NC} $1"
41}
42
43# Note: systemd-related logic removed for simplicity; this script always stops directly
44
45stop_by_pid() {
46    print_info "Stopping AxonHub using PID file..."
47    
48    if [[ ! -f "$PID_FILE" ]]; then
49        print_warning "PID file not found at $PID_FILE"
50        return 1
51    fi
52    
53    local pid=$(cat "$PID_FILE")
54    
55    if [[ ! "$pid" =~ ^[0-9]+$ ]]; then
56        print_error "Invalid PID in file: $pid"
57        rm -f "$PID_FILE"
58        return 1
59    fi
60    
61    if ! kill -0 "$pid" 2>/dev/null; then
62        print_warning "Process with PID $pid is not running"
63        rm -f "$PID_FILE"
64        return 1
65    fi
66    
67    print_info "Sending SIGTERM to process $pid..."
68    if kill -TERM "$pid" 2>/dev/null; then
69        # Wait for graceful shutdown
70        local timeout=10
71        local count=0
72        
73        while kill -0 "$pid" 2>/dev/null && [[ $count -lt $timeout ]]; do
74            sleep 1
75            ((count++))
76        done
77        
78        if kill -0 "$pid" 2>/dev/null; then
79            print_warning "Process did not stop gracefully, sending SIGKILL..."
80            kill -KILL "$pid" 2>/dev/null || true
81            sleep 2
82        fi
83        
84        if ! kill -0 "$pid" 2>/dev/null; then
85            print_success "AxonHub stopped successfully (PID: $pid)"
86            rm -f "$PID_FILE"
87        else
88            print_error "Failed to stop AxonHub process"
89            return 1
90        fi
91    else
92        print_error "Failed to send signal to process $pid"
93        return 1
94    fi
95}
96
97stop_by_process_name() {
98    print_info "Stopping AxonHub by process name..."
99    
100    local pids
101    pids=$(pgrep -f "$PROCESS_NAME" 2>/dev/null || true)
102    
103    if [[ -z "$pids" ]]; then
104        print_warning "No AxonHub processes found"
105        return 1
106    fi
107    
108    print_info "Found AxonHub processes: $pids"
109    
110    for pid in $pids; do
111        print_info "Stopping process $pid..."
112        
113        if kill -TERM "$pid" 2>/dev/null; then
114            # Wait for graceful shutdown
115            local timeout=10
116            local count=0
117            
118            while kill -0 "$pid" 2>/dev/null && [[ $count -lt $timeout ]]; do
119                sleep 1
120                ((count++))
121            done
122            
123            if kill -0 "$pid" 2>/dev/null; then
124                print_warning "Process $pid did not stop gracefully, sending SIGKILL..."
125                kill -KILL "$pid" 2>/dev/null || true
126            fi
127        fi
128    done
129    
130    sleep 2
131    
132    # Check if any processes are still running
133    local remaining_pids
134    remaining_pids=$(pgrep -f "$PROCESS_NAME" 2>/dev/null || true)
135    
136    if [[ -z "$remaining_pids" ]]; then
137        print_success "All AxonHub processes stopped successfully"
138        rm -f "$PID_FILE"
139    else
140        print_error "Some AxonHub processes are still running: $remaining_pids"
141        return 1
142    fi
143}
144
145check_running_processes() {
146    local pids
147    pids=$(pgrep -f "$PROCESS_NAME" 2>/dev/null || true)
148    
149    if [[ -n "$pids" ]]; then
150        print_info "Running AxonHub processes:"
151        ps -p $pids -o pid,ppid,cmd --no-headers 2>/dev/null || true
152        return 0
153    else
154        return 1
155    fi
156}
157
158main() {
159    print_info "Stopping AxonHub..."
160    
161    local stopped=false
162    
163    # Try PID file first
164    if stop_by_pid; then
165        stopped=true
166    fi
167    
168    # If PID file method didn't work, try by process name
169    if [[ "$stopped" != true ]]; then
170        if stop_by_process_name; then
171            stopped=true
172        fi
173    fi
174    
175    # Final check
176    if [[ "$stopped" != true ]]; then
177        if check_running_processes; then
178            print_error "Failed to stop all AxonHub processes"
179            return 1
180        else
181            print_info "No AxonHub processes were running"
182        fi
183    fi
184    
185    # Clean up PID file
186    rm -f "$PID_FILE"
187    
188    print_success "AxonHub has been stopped"
189}
190
191# Handle script arguments
192case "${1:-}" in
193    --force)
194        print_info "Force stopping all AxonHub processes..."
195        if check_running_processes; then
196            pkill -KILL -f "$PROCESS_NAME" 2>/dev/null || true
197            sleep 2
198            if ! check_running_processes; then
199                print_success "All AxonHub processes force-stopped"
200                rm -f "$PID_FILE"
201            else
202                print_error "Failed to force-stop some processes"
203                exit 1
204            fi
205        else
206            print_info "No AxonHub processes found"
207        fi
208        ;;
209    --help|-h)
210        echo "Usage: $0 [--force]"
211        echo
212        echo "This script stops AxonHub directly (no systemd)."
213        echo "Options:"
214        echo "  --force     Force kill all AxonHub processes"
215        echo "  --help, -h  Show this help message"
216        exit 0
217        ;;
218    "")
219        main
220        ;;
221    *)
222        print_error "Unknown option: $1"
223        print_info "Use --help for usage information"
224        exit 1
225        ;;
226esac
227