SIGTERM signal to your agent’s process. By listening for this signal, your agent can finish in-progress work, close external connections, flush pending data, and log a clean final status before the container exits. Without a handler, the process may be terminated mid-operation, leaving connections open or data in an inconsistent state.
The grace period
After sendingSIGTERM, the platform waits 10 seconds for your process to exit on its own. If your agent is still running after 10 seconds, the container is forcibly killed. Use this window to perform any cleanup that matters — flushing writes, closing database connections, sending a final log line — and then call process.exit(0).
Basic SIGTERM handler
Register the handler once at the top level of your entry file, outside any function:process.exit(0) explicitly once cleanup is complete so the container shuts down immediately without waiting out the full grace period.
Recommendations for long-running agents
Agents that run continuous loops or maintain persistent external connections need a shutdown strategy:Set a shutdown flag
Declare a boolean variable (e.g.
let isShuttingDown = false) and set it to true inside the SIGTERM handler. Check this flag in your loop condition so the loop exits cleanly on the next iteration.Close external connections
If your agent holds open database connections, HTTP keep-alive sockets, or WebSocket connections, close them inside the handler before calling
process.exit(0).