Skip to content

Commit 23ded96

Browse files
committed
fix: duplicate webserver or incorrect kill
1 parent ef4400c commit 23ded96

1 file changed

Lines changed: 111 additions & 5 deletions

File tree

.ai-ready/skills/deploy-website/SKILL.md

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,36 @@ Automatically detect the project type and start the appropriate development serv
5454

5555
## Workflow
5656

57-
1. Detect the project type and start the appropriate server in the background:
57+
1. **Check for existing server processes** before starting a new one:
58+
59+
Before attempting to start a new server, check if a similar process is already running and listening on a port:
60+
61+
```bash
62+
# Check for existing Node.js dev servers
63+
ps aux | grep -E "(npm run dev|npm start|node.*server)" | grep -v grep
64+
65+
# Check for existing Python/PHP/Go servers
66+
ps aux | grep -E "(python.*manage.py runserver|python.*app.py|flask run|php -S|go run)" | grep -v grep
67+
68+
# Check listening ports with ss/netstat/lsof
69+
ss -tlnp 2>/dev/null || netstat -tlnp 2>/dev/null || lsof -iTCP -sTCP:LISTEN -P -n
70+
71+
# If a matching process is found listening on a port:
72+
# - DO NOT start a new server
73+
# - Use the existing port number
74+
# - Proceed directly to step 3 to call `request_preview` with that port
75+
```
76+
77+
If an existing server process is detected:
78+
- Extract the port number from the process output
79+
- Skip server startup and proceed to step 3 to request preview URL
80+
- Notify the user that an existing server was detected
81+
82+
2. **Detect the project type and start the appropriate server in the background** (only if no existing server found):
5883

5984
> **Note**: The example commands below run in the foreground. When executing, the model must run these commands in the background (e.g., using the `&` suffix) to avoid blocking the session.
85+
>
86+
> **IMPORTANT**: When starting a server in the background, **CAPTURE AND SAVE THE PID** immediately after launch. This PID must be stored for later process management.
6087
6188
```bash
6289
# Step 1: Node.js project
@@ -127,24 +154,103 @@ else
127154
fi
128155
```
129156

130-
2. Capture the port number from the server output (or use default 8000).
157+
3. **Capture the PID and port number** after starting the server:
158+
159+
When starting a server in the background, immediately capture its PID:
160+
161+
```bash
162+
# Example for Node.js
163+
npm run dev &
164+
SERVER_PID=$!
165+
echo "Server started with PID: $SERVER_PID"
166+
167+
# Example for Python
168+
python manage.py runserver 8000 &
169+
SERVER_PID=$!
170+
echo "Server started with PID: $SERVER_PID"
171+
172+
# Example for PHP
173+
php -S localhost:8000 &
174+
SERVER_PID=$!
175+
echo "Server started with PID: $SERVER_PID"
176+
```
177+
178+
**Store the PID** in a temporary file or environment variable for later use:
179+
```bash
180+
echo $SERVER_PID > /tmp/deploy_website_server.pid
181+
```
182+
183+
Also capture the port number from the server output (or use default 8000).
131184

132-
3. Call MCP Tool `request_preview` with the listening port number to get a preview URL.
185+
4. Call MCP Tool `request_preview` with the listening port number to get a preview URL.
133186

134-
4. Present the preview URL to the user:
187+
5. Present the preview URL to the user:
135188
- Output a clickable hyperlink pointing to the preview address returned by the tool
189+
- Inform the user of the server PID for reference
136190

137-
5. Handle IP whitelist issues:
191+
6. Handle IP whitelist issues:
138192
- If the user reports that access is denied due to IP not being whitelisted, ask the user for their IP address(es) which reported by the deny page
139193
- Call MCP Tool `request_preview` again with the `additional_ip_whitelist` field to add the user's IP(s) to the whitelist
140194
- The `additional_ip_whitelist` field accepts multiple IPv4 addresses separated by commas (e.g., `"192.168.1.100,10.0.0.50"`)
141195

196+
## Process Management
197+
198+
### Recording PIDs
199+
200+
When the model starts a server process, **ALWAYS** record the PID:
201+
- Store in a temporary file: `/tmp/deploy_website_server.pid`
202+
- Include PID in user notifications
203+
- Keep track of which workspace the PID belongs to
204+
205+
### Stopping Servers
206+
207+
When the user requests to stop a server, **ALWAYS** use PID-based termination:
208+
209+
```bash
210+
# Read the PID from storage
211+
if [ -f "/tmp/deploy_website_server.pid" ]; then
212+
SERVER_PID=$(cat /tmp/deploy_website_server.pid)
213+
214+
# Verify the process exists
215+
if ps -p $SERVER_PID > /dev/null 2>&1; then
216+
# Graceful termination first
217+
kill -TERM $SERVER_PID
218+
219+
# Wait briefly for graceful shutdown
220+
sleep 2
221+
222+
# Force kill if still running
223+
if ps -p $SERVER_PID > /dev/null 2>&1; then
224+
kill -9 $SERVER_PID
225+
fi
226+
227+
echo "Server (PID: $SERVER_PID) has been stopped"
228+
rm /tmp/deploy_website_server.pid
229+
else
230+
echo "Server process (PID: $SERVER_PID) is not running"
231+
rm /tmp/deploy_website_server.pid
232+
fi
233+
else
234+
echo "No server PID found. Server may have been started manually."
235+
fi
236+
```
237+
238+
**CRITICAL**: Never use `pkill` or `killall` with process names (e.g., `pkill node`, `killall python`) as this may terminate:
239+
- User's own manually-started processes
240+
- Other unrelated development servers
241+
- System processes with similar names
242+
243+
Always prefer PID-based termination for safety.
244+
142245
## Notes
143246

144247
- The server **MUST** run in the background or as a subagent task in order **NOT TO** block the session.
248+
- Always check for existing server processes before starting a new one to avoid port conflicts.
145249
- Default port for static server is 8000.
146250
- For Node.js projects, the port depends on the project configuration.
147251
- The port number can be dynamically changed when conflicted with another service.
252+
- Record and track PIDs for all model-started server processes.
253+
- Use PID-based termination to avoid accidentally killing user processes.
148254
- Make sure dependencies are installed before running:
149255
- Node.js: `npm install`
150256
- Python: `pip install -r requirements.txt` (if exists)

0 commit comments

Comments
 (0)