Skip to content

Commit a77b388

Browse files
committed
mac-os-launch-agent-update
1 parent 7a4e006 commit a77b388

File tree

4 files changed

+69
-40
lines changed

4 files changed

+69
-40
lines changed

docs/MACOS_LAUNCHAGENT_EXAMPLE.md

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,32 @@ Place at: `~/Library/LaunchAgents/com.embed-rerank.server.plist`
99

1010
```xml
1111
<?xml version="1.0" encoding="UTF-8"?>
12-
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
12+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
1313
<plist version="1.0">
1414
<dict>
15-
<key>Label</key>
16-
<string>com.embed-rerank.server</string>
17-
18-
<!-- server-run.sh activates .venv and loads .env -->
19-
<key>ProgramArguments</key>
20-
<array>
21-
<string>/bin/bash</string>
22-
<string>-lc</string>
23-
<string>/Users/USERNAME/embed-rerank/tools/server-run.sh</string>
24-
</array>
25-
26-
<key>WorkingDirectory</key>
27-
<string>/Users/USERNAME/embed-rerank</string>
28-
29-
<!-- Auto-restart if the process exits -->
30-
<key>KeepAlive</key>
31-
<true/>
32-
33-
<key>RunAtLoad</key>
34-
<true/>
35-
36-
<!-- Default values (can be overridden via .env) -->
37-
<key>EnvironmentVariables</key>
38-
<dict>
39-
<key>HOST</key><string>0.0.0.0</string>
40-
<key>PORT</key><string>11436</string>
41-
<!-- Add more variables if needed -->
42-
</dict>
43-
44-
<key>StandardOutPath</key>
45-
<string>/tmp/embed-rerank.log</string>
46-
<key>StandardErrorPath</key>
47-
<string>/tmp/embed-rerank.err</string>
15+
<key>Label</key>
16+
<string>com.embed-rerank.server</string>
17+
<key>ProgramArguments</key>
18+
<array>
19+
<string>/bin/sh</string>
20+
<string>/Users/username/embed-rerank/tools/server-launchd.sh</string>
21+
</array>
22+
<key>WorkingDirectory</key>
23+
<string>/Users/username/embed-rerank</string>
24+
<key>EnvironmentVariables</key>
25+
<dict>
26+
<key>HOST</key><string>0.0.0.0</string>
27+
<key>PORT</key><string>11436</string>
28+
<key>MODEL_NAME</key><string>mlx-community/Qwen3-Embedding-4B-4bit-DWQ</string>
29+
</dict>
30+
<key>KeepAlive</key>
31+
<true/>
32+
<key>RunAtLoad</key>
33+
<true/>
34+
<key>StandardOutPath</key>
35+
<string>/tmp/embed-rerank.log</string>
36+
<key>StandardErrorPath</key>
37+
<string>/tmp/embed-rerank.err</string>
4838
</dict>
4939
</plist>
5040
```
@@ -81,7 +71,4 @@ launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.embed-rerank.server.
8171
## Notes
8272
- Ensure the path contains no typos (spaces are fine because we invoke via bash -lc).
8373
- `.venv` and `.env` must exist in the repo root.
84-
- For system-wide usage, create a LaunchDaemon under `/Library/LaunchDaemons/` (then adjust ownership/root requirements).
85-
```
86-
87-
Similar code found with 1 license type
74+
- For system-wide usage, create a LaunchDaemon under `/Library/LaunchDaemons/` (then adjust ownership/root requirements).

tools/server-launchd.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
# LaunchAgent-only startup script
3+
# - launchd manages process lifecycle (restart / stdout & stderr redirection)
4+
# - No backgrounding, nohup, PID files, or watchdog required here
5+
# - Clear separation: server-run.sh (manual/dev, background + watchdog) vs server-launchd.sh (foreground for launchd)
6+
set -euo pipefail
7+
8+
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
9+
cd "$REPO_ROOT"
10+
11+
# Provide sane locale defaults (launchd often supplies a minimal environment)
12+
export LANG="${LANG:-en_US.UTF-8}"
13+
export LC_ALL="${LC_ALL:-en_US.UTF-8}"
14+
15+
# Prepend project virtualenv to PATH to ensure correct python & scripts are found first
16+
export PATH="$REPO_ROOT/.venv/bin:/usr/local/bin:/usr/bin:/bin:$PATH"
17+
18+
VENV_PY="./.venv/bin/python"
19+
if [[ ! -x "$VENV_PY" ]]; then
20+
echo "[launchd] .venv python not found. Create & install deps:\n python -m venv .venv\n . .venv/bin/activate\n pip install -r requirements.txt" >&2
21+
exit 2
22+
fi
23+
24+
# Load .env (if present) to populate environment variables
25+
if [[ -f .env ]]; then
26+
set -a
27+
# shellcheck disable=SC1091
28+
. .env
29+
set +a
30+
fi
31+
32+
HOST="${HOST:-0.0.0.0}"
33+
PORT="${PORT:-9000}"
34+
35+
echo "[launchd] Starting embed-rerank (HOST=${HOST} PORT=${PORT}) using $($VENV_PY -V 2>/dev/null || echo python)"
36+
37+
# Foreground exec so launchd can supervise the process directly
38+
exec "$VENV_PY" -m uvicorn app.main:app --host "$HOST" --port "$PORT"

tools/server-run-watchdog.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ restart_server() {
5555
fi
5656
# Restart via server-run.sh but prevent spawning a second watchdog
5757
echo "[watchdog] Starting fresh server instance" | tee -a "$LOGFILE"
58-
WATCHDOG_SKIP=1 bash "$REPO_ROOT/tools/server-run.sh" >> "$LOGFILE" 2>&1 || {
58+
# Use bash -lc with a single argument so paths with spaces are handled
59+
# correctly by the shell. This avoids splitting $REPO_ROOT on spaces.
60+
WATCHDOG_SKIP=1 bash -lc "exec '$REPO_ROOT/tools/server-run.sh'" >> "$LOGFILE" 2>&1 || {
5961
echo "[watchdog] Restart attempt failed" | tee -a "$LOGFILE"
6062
}
6163
}

tools/server-run.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ if [[ "${WATCHDOG_SKIP:-0}" != "1" && "$WATCHDOG" == "1" ]]; then
9898
echo "Watchdog already running (PID $(cat "$WATCHDOG_PIDFILE"))"
9999
else
100100
echo "Starting watchdog..." | tee -a "$LOGFILE"
101-
nohup bash "$REPO_ROOT/tools/server-run-watchdog.sh" >> "$LOGFILE" 2>&1 &
101+
# Execute watchdog script directly (ensure script is executable). Quoting the
102+
# full path prevents word-splitting when REPO_ROOT contains spaces.
103+
nohup "$REPO_ROOT/tools/server-run-watchdog.sh" >> "$LOGFILE" 2>&1 &
102104
if command -v disown >/dev/null 2>&1; then disown $! || true; fi
103105
fi
104106
fi

0 commit comments

Comments
 (0)