-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteslaontarget.sh
More file actions
executable file
·196 lines (174 loc) · 4.7 KB
/
Copy pathteslaontarget.sh
File metadata and controls
executable file
·196 lines (174 loc) · 4.7 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
# TeslaOnTarget Control Script - Start, Stop, and Status in one script
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_DIR="$SCRIPT_DIR"
LOG_FILE="$PROJECT_DIR/teslaontarget.log"
PID_FILE="$PROJECT_DIR/teslaontarget.pid"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to check if running
is_running() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p $PID > /dev/null 2>&1; then
return 0 # Running
else
# Stale PID file
rm -f "$PID_FILE"
return 1 # Not running
fi
else
return 1 # Not running
fi
}
# Function to start TeslaOnTarget
start_tesla() {
if is_running; then
PID=$(cat "$PID_FILE")
echo -e "${YELLOW}TeslaOnTarget is already running with PID $PID${NC}"
exit 1
fi
echo -e "${BLUE}Starting TeslaOnTarget...${NC}"
echo "Log file: $LOG_FILE"
# Change to project directory
cd "$PROJECT_DIR"
# Start in background with logging
nohup python3 -m teslaontarget >> "$LOG_FILE" 2>&1 &
PID=$!
# Save PID
echo $PID > "$PID_FILE"
# Wait a moment to check if it started successfully
sleep 2
if is_running; then
echo -e "${GREEN}✓ TeslaOnTarget started successfully with PID $PID${NC}"
echo -e "To view logs: tail -f $LOG_FILE"
else
echo -e "${RED}✗ Failed to start TeslaOnTarget${NC}"
echo "Check the log file for errors: tail -20 $LOG_FILE"
exit 1
fi
}
# Function to stop TeslaOnTarget
stop_tesla() {
if ! is_running; then
echo -e "${YELLOW}TeslaOnTarget is not running${NC}"
exit 1
fi
PID=$(cat "$PID_FILE")
echo -e "${BLUE}Stopping TeslaOnTarget (PID $PID)...${NC}"
# Try graceful shutdown first
kill $PID 2>/dev/null
# Wait up to 5 seconds for graceful shutdown
for i in {1..5}; do
if ! ps -p $PID > /dev/null 2>&1; then
break
fi
sleep 1
done
# Force kill if still running
if ps -p $PID > /dev/null 2>&1; then
echo -e "${YELLOW}Force killing...${NC}"
kill -9 $PID 2>/dev/null
fi
rm -f "$PID_FILE"
echo -e "${GREEN}✓ TeslaOnTarget stopped${NC}"
}
# Function to show status
show_status() {
echo -e "${BLUE}TeslaOnTarget Status${NC}"
echo "===================="
if is_running; then
PID=$(cat "$PID_FILE")
echo -e "Status: ${GREEN}RUNNING${NC}"
echo "PID: $PID"
echo ""
echo "Process info:"
ps -fp $PID
echo ""
# Show connection status from recent logs
if [ -f "$LOG_FILE" ]; then
echo "Recent activity:"
# Get last few relevant log lines
tail -20 "$LOG_FILE" | grep -E "(Connected to TAK|Got vehicle data|Dead reckoning|ERROR|WARNING)" | tail -5
fi
else
echo -e "Status: ${RED}NOT RUNNING${NC}"
fi
}
# Function to restart TeslaOnTarget
restart_tesla() {
echo -e "${BLUE}Restarting TeslaOnTarget...${NC}"
if is_running; then
stop_tesla
sleep 2
fi
start_tesla
}
# Function to show logs
show_logs() {
if [ -f "$LOG_FILE" ]; then
tail -f "$LOG_FILE"
else
echo -e "${RED}Log file not found: $LOG_FILE${NC}"
exit 1
fi
}
# Function to show help
show_help() {
echo "TeslaOnTarget Control Script"
echo ""
echo "Usage: $0 {start|stop|restart|status|logs|help}"
echo ""
echo "Commands:"
echo " start - Start TeslaOnTarget in the background"
echo " stop - Stop TeslaOnTarget"
echo " restart - Restart TeslaOnTarget"
echo " status - Show current status"
echo " logs - Follow the log file (tail -f)"
echo " help - Show this help message"
echo ""
echo "Configuration:"
echo " Edit config.py to set your Tesla account and TAK server details"
echo ""
echo "Examples:"
echo " $0 start # Start tracking"
echo " $0 status # Check if running"
echo " $0 logs # View live logs"
echo " $0 stop # Stop tracking"
}
# Main script logic
case "$1" in
start)
start_tesla
;;
stop)
stop_tesla
;;
restart)
restart_tesla
;;
status)
show_status
;;
logs)
show_logs
;;
help|--help|-h)
show_help
;;
*)
if [ -z "$1" ]; then
# No arguments - show status
show_status
else
echo -e "${RED}Unknown command: $1${NC}"
echo ""
show_help
exit 1
fi
;;
esac