Skip to content

Commit b949c73

Browse files
authored
Update macos-launchd-setup.sh
1 parent b8d2305 commit b949c73

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed
Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,67 @@
11
#!/bin/bash
22
set -e
33

4-
PORT="$1"
5-
SERVE_PATH="$2"
4+
# Parse command-line arguments
5+
while [[ $# -gt 0 ]]; do
6+
case "$1" in
7+
-path)
8+
SERVE_PATH="$2"
9+
shift 2
10+
;;
11+
-port)
12+
PORT="$2"
13+
shift 2
14+
;;
15+
*)
16+
echo "❌ Unknown option: $1"
17+
exit 1
18+
;;
19+
esac
20+
done
621

7-
if [[ -z "$PORT" || -z "$SERVE_PATH" ]]; then
8-
echo "❌ Port and path are required."
9-
exit 1
10-
fi
22+
# Define paths and interpreter
23+
# Destination for the launchd plist
24+
PLIST_PATH="$HOME/Library/LaunchAgents/com.custom_http_server.plist"
25+
SCRIPT_PATH="/usr/local/custom_http_server/custom_http_server.py"
26+
PYTHON_PATH="$(which python3)"
1127

12-
PLIST_PATH="$HOME/Library/LaunchAgents/com.custom.httpserver.plist"
13-
TARGET_DIR="/usr/local/custom_http_server"
28+
echo "Python Path: $PYTHON_PATH"
29+
echo "Script Path: $SCRIPT_PATH"
1430

1531
mkdir -p "$(dirname "$PLIST_PATH")"
1632

33+
# Create the launchd plist XML content
1734
cat > "$PLIST_PATH" <<EOF
1835
<?xml version="1.0" encoding="UTF-8"?>
1936
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
2037
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
2138
<plist version="1.0">
2239
<dict>
23-
<key>Label</key>
24-
<string>com.custom.httpserver</string>
25-
<key>ProgramArguments</key>
26-
<array>
27-
<string>/usr/local/bin/python3</string>
28-
<string>$TARGET_DIR/custom_http_server.py</string>
29-
<string>--path</string>
30-
<string>$SERVE_PATH</string>
31-
<string>--port</string>
32-
<string>$PORT</string>
33-
</array>
34-
<key>RunAtLoad</key>
35-
<true/>
36-
<key>WorkingDirectory</key>
37-
<string>$TARGET_DIR</string>
38-
<key>StandardOutPath</key>
39-
<string>/tmp/custom_http_server.log</string>
40-
<key>StandardErrorPath</key>
41-
<string>/tmp/custom_http_server.err</string>
40+
<key>Label</key>
41+
<string>com.custom_httpserver.python</string>
42+
<key>ProgramArguments</key>
43+
<array>
44+
<string>$PYTHON_PATH</string>
45+
<string>$SCRIPT_PATH</string>
46+
<string>--path</string>
47+
<string>$SERVE_PATH</string>
48+
<string>--port</string>
49+
<string>$PORT</string>
50+
</array>
51+
<key>RunAtLoad</key>
52+
<true/>
53+
<key>StandardOutPath</key>
54+
<string>/tmp/custom_httpserver.log</string>
55+
<key>StandardErrorPath</key>
56+
<string>/tmp/custom_httpserver.err</string>
4257
</dict>
4358
</plist>
4459
EOF
4560

61+
# Unload the plist if already loaded (ignore errors), then load the new version
4662
echo "✅ launchd plist created at $PLIST_PATH"
4763
launchctl unload "$PLIST_PATH" 2>/dev/null || true
4864
launchctl load "$PLIST_PATH"
49-
echo "✅ Loaded into launchctl. Use 'launchctl list | grep custom.httpserver' to check."
65+
66+
# Notify user
67+
echo "✅ Loaded com.custom_httpserver.python service. To verify: launchctl list | grep custom_httpserver"

0 commit comments

Comments
 (0)