This guide covers running Focus Bot as a persistent background service on Linux (systemd) and macOS (launchd).
- Bun installed and working (
bun --version) - Bot runs successfully in the foreground (
bun run start) .envfile configured in the project root
Find your Bun path (you'll need it below):
which bun
mkdir -p ~/.config/systemd/userCreate ~/.config/systemd/user/focus-bot.service with this content:
[Unit] Description=Focus Bot - Telegram to Obsidian note capture After=network.target [Service] Type=simple WorkingDirectory=/home/YOUR_USER/code/focus-bot ExecStart=/home/YOUR_USER/.bun/bin/bun run start Restart=always RestartSec=5 EnvironmentFile=/home/YOUR_USER/code/focus-bot/.env [Install] WantedBy=default.target
Replace /home/YOUR_USER with your actual home directory path throughout.
This keeps your user services running after you log out:
loginctl enable-linger $USERsystemctl --user daemon-reload
systemctl --user enable focus-bot.service
systemctl --user start focus-bot.servicesystemctl --user status focus-bot
# Start / stop / restart systemctl --user start focus-bot systemctl --user stop focus-bot systemctl --user restart focus-bot # View status systemctl --user status focus-bot # Follow logs live journalctl --user -u focus-bot -f # Recent logs journalctl --user -u focus-bot -n 50 # Enable / disable auto-start systemctl --user enable focus-bot systemctl --user disable focus-bot
systemctl --user restart focus-bot
If you edited the service file itself:
systemctl --user daemon-reload systemctl --user restart focus-bot
Create ~/Library/LaunchAgents/com.focus-bot.plist with this content:
<?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.focus-bot</string> <key>WorkingDirectory</key> <string>/Users/YOUR_USER/code/focus-bot</string> <key>ProgramArguments</key> <array> <string>/Users/YOUR_USER/.bun/bin/bun</string> <string>run</string> <string>start</string> </array> <key>EnvironmentVariables</key> <dict> <key>PATH</key> <string>/Users/YOUR_USER/.bun/bin:/usr/local/bin:/usr/bin:/bin</string> </dict> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> <key>StandardOutPath</key> <string>/Users/YOUR_USER/Library/Logs/focus-bot.log</string> <key>StandardErrorPath</key> <string>/Users/YOUR_USER/Library/Logs/focus-bot.error.log</string> </dict> </plist>
Replace /Users/YOUR_USER with your actual home directory path throughout.
Note: launchd doesn't support EnvironmentFile. You have two options for environment variables:
Option A — Add each variable to the plist's EnvironmentVariables dict:
<key>EnvironmentVariables</key> <dict> <key>PATH</key> <string>/Users/YOUR_USER/.bun/bin:/usr/local/bin:/usr/bin:/bin</string> <key>TELEGRAM_BOT_TOKEN</key> <string>your-token-here</string> <key>ALLOWED_USER_IDS</key> <string>123456</string> <!-- ... other vars ... --> </dict>
Option B (recommended) — Use a wrapper script that sources .env:
Create run.sh in the project root:
#!/bin/bash set -a source "$(dirname "$0")/.env" set +a exec bun run start
chmod +x run.sh
Then change the plist's ProgramArguments to:
<key>ProgramArguments</key> <array> <string>/Users/YOUR_USER/code/focus-bot/run.sh</string> </array>
launchctl load ~/Library/LaunchAgents/com.focus-bot.plistlaunchctl list | grep focus-botA 0 in the status column means it's running. A non-zero number means it exited with an error.
# Start / stop launchctl load ~/Library/LaunchAgents/com.focus-bot.plist launchctl unload ~/Library/LaunchAgents/com.focus-bot.plist # Check status launchctl list | grep focus-bot # View logs tail -f ~/Library/Logs/focus-bot.log tail -f ~/Library/Logs/focus-bot.error.log
launchctl unload ~/Library/LaunchAgents/com.focus-bot.plist launchctl load ~/Library/LaunchAgents/com.focus-bot.plist
Service won't start — Test the bot manually first:
bun run start
If that works but the service doesn't, check that all paths in the service/plist file are absolute and correct.
Linux: service stops after logout — Make sure lingering is enabled:
loginctl enable-linger $USERmacOS: "Operation not permitted" — You may need to grant Full Disk Access to the terminal or bun in System Settings > Privacy & Security.