agentleFS
joserprietojoserprieto/ai-skills/skills/service-manager/SKILL.md

service-manager

Skill2 starsChanged 10 months agoMITView on GitHub

Use when adding background service management to a project that runs dev servers, preview servers, or any long-running processes. Also use when user says "service manager", "background server", "start/stop services", "PID management", "log rotation", or needs Makefile targets for start/stop/status/tail/logs.

  • Reads credentials
  • Installs packages

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 0 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Add background service management to any project with PID tracking, health checks, log rotation, and Makefile integration.

    line 17
  2. Very specific:

    Every managed service ships the full quartet (start/stop/restart/status, aggregate AND per-service) — see makefile-service-conventions for …

    line 65
  3. Very specific:

    Add tsx to devDependencies for running TypeScript service manager:

    line 74
  4. Very specific:

    Use the example file at examples/service-manager.ts as a starting point.

    line 86
  5. Very specific:

    Document all configurable variables with commented defaults.

    line 101

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Service Manager Pattern

Add background service management to any project with PID tracking, health checks, log rotation, and Makefile integration. Replaces foreground-only make dev patterns with a full start/stop/status/tail DX.

When to Use

  • Project has dev servers that run in foreground and block the terminal
  • Need to manage multiple services (dev, preview, etc.) independently
  • Want log persistence and rotation for background services
  • Need health checks to verify services are actually responding
  • Want consistent make start/stop/status/tail interface across projects

When NOT to Use

  • Single-command scripts that don't need background management
  • Docker/container-managed services (use docker-compose instead)
  • Projects already using PM2, systemd, or similar process managers

Architecture

project/
├── Makefile                   # Orchestrates via SM variable
├── .env.example               # Documented config options
├── .env.local                 # User overrides (git-ignored)
├── scripts/
│   └── service-manager.ts     # Core manager (TypeScript, runs via tsx)
├── .state/                    # PID files (git-ignored)
│   └── <service>.pid          # Format: pid:timestamp
└── .logs/                     # Log files with rotation (git-ignored)
    └── <service>.log

Quick Reference

CommandEffect
make startStart the default service set in background
make start/<svc>Start one service in background
make start/<svc>/fgStart in foreground (Ctrl+C to stop)
make stopStop all services (graceful SIGTERM → SIGKILL)
make stop/<svc>Stop one service
make restartStop + start (PRIMARY — always ship with start/stop)
make restart/<svc>Restart one service
make statusShow running/stopped with health check
make status/<svc>Status of one service
make status/detailsAdd PID, uptime, log size, paths

Every managed service ships the full quartet (start/stop/restart/status, aggregate AND per-service) — see makefile-service-conventions for the binding rule; this skill's manager is one way to implement it. | make tail | tail -f all running service logs | | make logs | List log files with sizes | | make logs/rotate | Rotate log files |

Implementation Steps

Step 1: Add Dependencies

Add tsx to devDependencies for running TypeScript service manager:

{
  "devDependencies": {
    "tsx": "^4.0.0"
  }
}

Step 2: Create scripts/service-manager.ts

Use the example file at examples/service-manager.ts as a starting point. Adapt:

  1. getServices() function — Define your services with:
    • name, displayName — identifier and human label
    • port, portEnvVar — port number and env var name for override
    • cwd — working directory for the command
    • command — array of command + args (e.g., ['pnpm', 'dev', '--port', '3000'])
    • healthUrl — HTTP URL for health check (use {port} placeholder)
  2. loadEnv() — reads .env.local (not .env to avoid framework conflicts)
  3. Environment variables — add project-specific config vars at the top

Step 3: Create .env.example

Document all configurable variables with commented defaults. See examples/.env.example.

Step 4: Create/Update Makefile

Key pattern — delegate to service manager via SM variable:

SM := npx tsx scripts/service-manager.ts

# Load optional .env.local
-include .env.local

# Export env vars to service manager
export VAR1 VAR2 PORT1 PORT2

# Defaults
VAR1 ?= default_value

start: _ensure
  @$(SM) start <service>

stop:
  @$(SM) stop --all

restart: stop start

status:
  @$(SM) status

_ensure: node_modules
node_modules: package.json
  @pnpm install
  @touch node_modules

Step 5: Update .gitignore

.state/
.logs/
.env.local

Core Service Manager Features

Kept from example (do not remove):

  • PID files — Format pid:timestamp in .state/<service>.pid
  • Process tree kill — pgrep -P recursive, SIGTERM then SIGKILL with timeout
  • Port detection — lsof -ti tcp:<port> fallback when PID file stale
  • Health checks — HTTP GET with 2s timeout via fetch()
  • Log rotation — Size-based rotation with configurable max files
  • Signal handlers — SIGINT/SIGTERM cleanup for foreground mode
  • Detached spawn — detached: true, stdio: ['pipe', logFd, logFd], unref()

Common Adaptations

ScenarioChange
Monorepo (multiple apps)Add APPS_DIR, use join(APPS_DIR, 'app-name') for cwd
Extra env per serviceAdd env?: Record<string, string> to ServiceConfig
Build-then-serveUse pnpm run preview script that chains build + serve
Conditional flagsBuild command array dynamically based on env vars
Custom health checkOverride healthUrl or extend checkHealth()

Common Mistakes

MistakeFix
Using .env instead of .env.localFrameworks read .env — use .env.local to avoid conflicts
Not exporting env vars in MakefileAdd export VAR1 VAR2 for service manager to see them
Missing _ensure prerequisiteAll service targets need _ensure (which depends on node_modules)
Forgetting .gitignore entries.state/, .logs/, .env.local must be git-ignored
Using && in spawn command arrayShell operators don't work in spawn — create a npm script instead

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.