|
1 | 1 | #!/bin/bash |
2 | 2 | set -e |
3 | 3 |
|
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 |
6 | 21 |
|
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)" |
11 | 27 |
|
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" |
14 | 30 |
|
15 | 31 | mkdir -p "$(dirname "$PLIST_PATH")" |
16 | 32 |
|
| 33 | +# Create the launchd plist XML content |
17 | 34 | cat > "$PLIST_PATH" <<EOF |
18 | 35 | <?xml version="1.0" encoding="UTF-8"?> |
19 | 36 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" |
20 | 37 | "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
21 | 38 | <plist version="1.0"> |
22 | 39 | <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> |
42 | 57 | </dict> |
43 | 58 | </plist> |
44 | 59 | EOF |
45 | 60 |
|
| 61 | +# Unload the plist if already loaded (ignore errors), then load the new version |
46 | 62 | echo "✅ launchd plist created at $PLIST_PATH" |
47 | 63 | launchctl unload "$PLIST_PATH" 2>/dev/null || true |
48 | 64 | 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