django-toggle.3s.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. set -euo pipefail
  3. PLIST="$HOME/Library/LaunchAgents/com.hungryroot.django.plist"
  4. ICON="🔌"
  5. MATCH="manage.py runserver"
  6. LABEL="$(/usr/bin/plutil -extract Label raw -o - "$PLIST" 2>/dev/null || true)"
  7. UID_NUM="$(/usr/bin/id -u)"
  8. DOMAIN="gui/$UID_NUM"
  9. TARGET="$DOMAIN/$LABEL"
  10. is_running() {
  11. /bin/ps waux | /usr/bin/grep -F "$MATCH" | /usr/bin/grep -v grep >/dev/null 2>&1
  12. }
  13. pids() {
  14. /bin/ps waux | /usr/bin/grep -F "$MATCH" | /usr/bin/grep -v grep | /usr/bin/awk '{print $2}'
  15. }
  16. # --- Title ---
  17. if is_running; then
  18. echo "$ICON 🟢"
  19. else
  20. echo "$ICON 🔴"
  21. fi
  22. echo "---"
  23. if is_running; then
  24. echo "Stop | bash='$0' param1=stop terminal=false refresh=true"
  25. echo "Restart | bash='$0' param1=restart terminal=false refresh=true"
  26. else
  27. echo "Start | bash='$0' param1=start terminal=false refresh=true"
  28. fi
  29. echo "---"
  30. echo "Open http://0.0.0.0:8000 | href=http://0.0.0.0:8000"
  31. echo "Debug | bash='$0' param1=debug terminal=true"
  32. echo "Open plist | bash='/usr/bin/open' param1='$PLIST' terminal=false"
  33. ACTION="${1:-}"
  34. case "$ACTION" in
  35. start)
  36. [[ -n "${LABEL:-}" ]] || {
  37. echo "ERROR: couldn't read Label from $PLIST" >&2
  38. exit 1
  39. }
  40. /bin/launchctl bootstrap "$DOMAIN" "$PLIST" 2>/dev/null || true
  41. /bin/launchctl kickstart -k "$TARGET" 2>/dev/null || true
  42. ;;
  43. stop)
  44. # THIS is the important part: unload the LaunchAgent so it won’t restart.
  45. [[ -n "${LABEL:-}" ]] || exit 0
  46. /bin/launchctl bootout "$TARGET" 2>/dev/null || true
  47. # Optional cleanup: kill any remaining process that matches
  48. pids | /usr/bin/xargs -r kill -TERM 2>/dev/null || true
  49. ;;
  50. restart)
  51. "$0" stop
  52. sleep 0.5
  53. "$0" start
  54. ;;
  55. debug)
  56. echo "PLIST: $PLIST"
  57. echo "LABEL: $LABEL"
  58. echo "TARGET: $TARGET"
  59. echo
  60. echo "=== launchctl print (may fail in SwiftBar, still useful) ==="
  61. /bin/launchctl print "$TARGET" 2>&1 || true
  62. echo
  63. echo "=== matching processes ==="
  64. /bin/ps waux | /usr/bin/grep -F "$MATCH" | /usr/bin/grep -v grep || echo "(no matches)"
  65. ;;
  66. esac