DAEMON PROCESS
daemon-process.ts
PID management, Unix socket API, job scheduler, signal handling, per-project configuration.
StarkWHAT THIS PATTERN TEACHES
How to build a long-running daemon with PID file management, Unix socket control API, cron-like job scheduling, graceful signal handling, and per-project path configuration with dual-daemon guard (v22.0).
WHEN TO USE THIS
Background services: heartbeat monitors, spend watchers, portfolio pollers, token refreshers.
AT A GLANCE
class Daemon {
async start() {
this.writePid();
this.startSocket();
this.scheduleJobs();
process.on("SIGTERM", () => this.shutdown());
}
}FRAMEWORK IMPLEMENTATIONS
TypeScript
import fs from "fs";
import net from "net";
class Daemon {
private pidFile: string;
private socketPath: string;
async start() {
if (this.isRunning()) throw new Error("Already running");
this.writePid();
this.startSocket();
process.on("SIGTERM", () => this.shutdown());
process.on("SIGINT", () => this.shutdown());
}
private writePid() {
fs.writeFileSync(this.pidFile, String(process.pid));
}
private startSocket() {