-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-server.sh
More file actions
executable file
·148 lines (123 loc) · 4.33 KB
/
start-server.sh
File metadata and controls
executable file
·148 lines (123 loc) · 4.33 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
#!/bin/bash
# Soulfield OS Server Startup Script
# Handles port conflicts and process cleanup automatically
set -e # Exit on error
# Configuration
DEFAULT_PORT=8790
LOG_FILE="/tmp/soulfield-server.log"
PID_FILE="/tmp/soulfield-server.pid"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}=====================================${NC}"
echo -e "${BLUE} Soulfield OS Server Startup${NC}"
echo -e "${BLUE}=====================================${NC}"
echo ""
# Function to kill processes on port
kill_port() {
local port=$1
echo -e "${YELLOW}Checking for processes on port ${port}...${NC}"
# Find PIDs using the port
local pids=$(lsof -ti :${port} 2>/dev/null || true)
if [ -n "$pids" ]; then
echo -e "${YELLOW}Found processes: ${pids}${NC}"
echo -e "${YELLOW}Killing processes...${NC}"
echo "$pids" | xargs -r kill -9 2>/dev/null || true
sleep 1
echo -e "${GREEN}✓ Port ${port} cleared${NC}"
else
echo -e "${GREEN}✓ Port ${port} is free${NC}"
fi
}
# Function to kill old Soulfield processes
kill_old_servers() {
echo -e "${YELLOW}Checking for old Soulfield processes...${NC}"
# Find all node processes running backend/index.cjs
local old_pids=$(pgrep -f "node backend/index.cjs" 2>/dev/null || true)
if [ -n "$old_pids" ]; then
echo -e "${YELLOW}Found old processes: ${old_pids}${NC}"
echo -e "${YELLOW}Killing old servers...${NC}"
echo "$old_pids" | xargs -r kill -9 2>/dev/null || true
sleep 1
echo -e "${GREEN}✓ Old processes cleared${NC}"
else
echo -e "${GREEN}✓ No old processes found${NC}"
fi
}
# Function to check if port is available
check_port() {
local port=$1
if lsof -i :${port} >/dev/null 2>&1; then
return 1 # Port in use
else
return 0 # Port available
fi
}
# Step 1: Kill old Soulfield servers
kill_old_servers
# Step 2: Clear the default port
kill_port $DEFAULT_PORT
# Step 3: Verify port is free
if ! check_port $DEFAULT_PORT; then
echo -e "${RED}✗ Port ${DEFAULT_PORT} still in use after cleanup!${NC}"
echo -e "${YELLOW}Attempting force cleanup...${NC}"
kill_port $DEFAULT_PORT
sleep 2
if ! check_port $DEFAULT_PORT; then
echo -e "${RED}✗ Cannot free port ${DEFAULT_PORT}${NC}"
echo -e "${YELLOW}Try manually: sudo lsof -ti :${DEFAULT_PORT} | xargs kill -9${NC}"
exit 1
fi
fi
# Step 4: Remove old PID file
if [ -f "$PID_FILE" ]; then
echo -e "${YELLOW}Removing old PID file...${NC}"
rm -f "$PID_FILE"
fi
# Step 5: Start the server
echo ""
echo -e "${BLUE}Starting Soulfield OS server...${NC}"
echo -e "${BLUE}Port: ${DEFAULT_PORT}${NC}"
echo -e "${BLUE}Logs: ${LOG_FILE}${NC}"
echo ""
export PORT=$DEFAULT_PORT
# Start server in background
nohup node backend/index.cjs > "$LOG_FILE" 2>&1 &
SERVER_PID=$!
# Save PID
echo $SERVER_PID > "$PID_FILE"
# Wait a moment for server to start
sleep 3
# Step 6: Verify server started
if ps -p $SERVER_PID > /dev/null 2>&1; then
echo -e "${GREEN}✓ Server started successfully!${NC}"
echo -e "${GREEN} PID: ${SERVER_PID}${NC}"
echo -e "${GREEN} Port: ${DEFAULT_PORT}${NC}"
echo ""
# Test health endpoint
echo -e "${YELLOW}Testing health endpoint...${NC}"
if curl -s -f http://localhost:${DEFAULT_PORT}/health > /dev/null 2>&1; then
echo -e "${GREEN}✓ Server responding to requests${NC}"
else
echo -e "${YELLOW}⚠ Server started but not responding yet (may still be initializing)${NC}"
echo -e "${YELLOW} Check logs: tail -f ${LOG_FILE}${NC}"
fi
echo ""
echo -e "${BLUE}=====================================${NC}"
echo -e "${GREEN}Server is running!${NC}"
echo -e "${BLUE}=====================================${NC}"
echo ""
echo -e "Commands:"
echo -e " ${YELLOW}Test:${NC} curl -X POST http://localhost:${DEFAULT_PORT}/chat -H 'Content-Type: application/json' -d '{\"text\":\"@marketing test\"}'"
echo -e " ${YELLOW}Logs:${NC} tail -f ${LOG_FILE}"
echo -e " ${YELLOW}Stop:${NC} ./stop-server.sh"
echo -e " ${YELLOW}Restart:${NC} ./restart-server.sh"
echo ""
else
echo -e "${RED}✗ Server failed to start!${NC}"
echo -e "${YELLOW}Check logs: tail -100 ${LOG_FILE}${NC}"
exit 1
fi