2026-04-29 · Markus Chen (Lead Video Engineer)
Robust Dev Environments: Deep Cleaning Stray Background Processes
An administrative engineering guide to running cleanup scripts and using pkill safely in multi-process development environments.
The Danger of Stray Background Processes
When developing and scaling multi-process applications (such as platforms running background workers, API servers, and frontend compilation daemons), process lifecycle management is crucial. If a process crashes unexpectedly or is terminated incorrectly, it can leave "stray" or orphaned processes running in the background.
These stray processes can lock database tables, consume system RAM, and keep TCP ports occupied (e.g., keeping port 8000 locked), causing subsequent startup attempts to fail.
TryScript’s Safe Deep Cleanup Strategy
To ensure a highly stable and reliable development environment, TryScript implements a robust deep cleanup routine right inside its dev.sh startup script. Before starting any platform services, the script executes a comprehensive cleanup sweep:
echo "Stopping existing processes..."
pkill -9 -f "uvicorn" || true
pkill -9 -f "next-server" || true
pkill -9 -f "python.*worker" || true
pkill -9 -f "supervisor.py" || true
pkill -9 -f "node.*gemini" || true
sleep 1.5
Why the Cleanup Loop is Crucial for Platform Health
- Port Liberation: Killing existing uvicorn and Next.js processes guarantees that backend ports (8000 and 8081) and the frontend port (3000) are fully liberated.
- Worker Reset: Terminating stale standalone worker scripts prevents duplicate workers from polling the database, avoiding race conditions and double credit deductions.
- Graceful Reboot: A deliberate 1.5-second sleep pause allows the OS kernel to clean up the process sockets safely before starting the fresh services.
By automating this cleanup routine, TryScript ensures that the entire environment reboots into a clean, healthy state every single time, preventing unexpected operational conflicts.