happy icon indicating copy to clipboard operation
happy copied to clipboard

Feature Request: launchd/systemd service for persistent daemon

Open BTForIT opened this issue 11 months ago • 2 comments

Summary

Would love to see official guidance or built-in support for running the Happy daemon as a system service (launchd on macOS, systemd on Linux) for auto-start on boot and crash recovery.

Current Approach

I set up a launchd service manually. Here's what worked for me on macOS:

LaunchAgent plist

~/Library/LaunchAgents/com.happy-coder.daemon.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.happy-coder.daemon</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/USERNAME/.npm-global/bin/happy</string>
        <string>daemon</string>
        <string>start-sync</string>
    </array>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/usr/local/bin:/usr/bin:/bin:/Users/USERNAME/.npm-global/bin</string>
    </dict>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/tmp/happy-coder.log</string>
    <key>StandardErrorPath</key>
    <string>/tmp/happy-coder.err</string>
</dict>
</plist>

Setup commands

# Load the service
launchctl load ~/Library/LaunchAgents/com.happy-coder.daemon.plist

# Check status
launchctl list | grep happy

Benefits

  • Auto-starts on login
  • Auto-restarts if daemon crashes
  • No need to remember happy daemon start after reboot

Potential Issues / Questions

  1. Conflict with happy daemon start: If someone runs happy daemon start manually while launchd is managing it, could get duplicate processes. Maybe needs mutex/lockfile?

  2. Path differences: npm global bin location varies (~/.npm-global/bin, /usr/local/bin, etc.) - would need templating or detection

  3. start-sync vs start: I used start-sync since launchd manages the lifecycle. Is this the right subcommand for service mode?

  4. Updates: After npm update -g happy-coder, does the daemon need restart? launchd's KeepAlive might auto-handle this

  5. Credentials/auth: Does the daemon need any env vars or auth tokens that might not be available at boot time?

Feature Request

Would be great to have:

  • happy daemon install - creates platform-appropriate service file
  • happy daemon uninstall - removes it
  • Docs on recommended service setup

Happy to help test or contribute if there's interest!


macOS 15.2, happy-coder 0.13.0

BTForIT avatar Dec 26 '25 23:12 BTForIT

Nice, we run a bunch of launchd-managed daemons for our social posting and agent orchestration systems. Some tips beyond what you've got:

KeepAlive with SuccessfulExit = false is the right call for crash recovery, but also add ThrottleInterval (default is 10s) to avoid restart loops if something is fundamentally broken. We use 30s.

For logging, StandardOutPath/StandardErrorPath work but they don't rotate. We pipe through a small wrapper script that uses logger to send output to the system log instead, then use log show --predicate to query it. Much cleaner than managing log files.

WatchPaths is useful if your daemon needs to restart when config changes. We use it to pick up new scheduling configs without manual restarts.

One thing that tripped us up: if your binary isn't signed or is in a path that Gatekeeper doesn't like, launchd will silently refuse to start it. Check launchctl print system/com.your.label for diagnostic info when things don't start.

m13v avatar Mar 13 '26 00:03 m13v

Our launchd plist configs for reference: https://github.com/m13v/social-autoposter/tree/main/launchd

We manage multiple daemons this way - social posting scheduler, agent orchestration, stats collection. All auto-start on boot with crash recovery.

m13v avatar Mar 13 '26 00:03 m13v