celery-toggle.3s.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/bin/bash
  2. set -euo pipefail
  3. # --- CONFIG ---
  4. MATCH="manage.py runserver"
  5. PLIST="$HOME/Library/LaunchAgents/com.hungyroot.celery.plist"
  6. ICON="🥕"
  7. # --- Helpers ---
  8. is_running() {
  9. /bin/ps waux | /usr/bin/grep -F "$MATCH" | /usr/bin/grep -v grep >/dev/null 2>&1
  10. }
  11. # --- Menu bar title ---
  12. if is_running; then
  13. echo "$ICON 🟢"
  14. else
  15. echo "$ICON 🔴"
  16. fi
  17. echo "---"
  18. # --- Actions ---
  19. if is_running; then
  20. echo "Stop | bash='$0' param1=stop terminal=false refresh=true"
  21. echo "Restart | bash='$0' param1=restart terminal=false refresh=true"
  22. else
  23. echo "Start | bash='$0' param1=start terminal=false refresh=true"
  24. fi
  25. echo "---"
  26. echo "Show matching processes | bash='$0' param1=ps terminal=true"
  27. echo "Open plist | bash='/usr/bin/open' param1='$PLIST' terminal=false"
  28. # --- Handler ---
  29. ACTION="${1:-}"
  30. case "$ACTION" in
  31. start)
  32. /bin/launchctl bootstrap "gui/$(id -u)" "$PLIST" 2>/dev/null || true
  33. /bin/launchctl kickstart -k "gui/$(id -u)/$(/usr/bin/plutil -extract Label raw -o - "$PLIST")" 2>/dev/null || true
  34. ;;
  35. stop)
  36. # Kill only matching Celery runserver processes
  37. /bin/ps waux | /usr/bin/grep -F "$MATCH" | /usr/bin/grep -v grep | /usr/bin/awk '{print $2}' | /usr/bin/xargs -r kill
  38. ;;
  39. restart)
  40. "$0" stop
  41. sleep 0.5
  42. "$0" start
  43. ;;
  44. ps)
  45. /bin/ps waux | /usr/bin/grep -F "$MATCH" | /usr/bin/grep -v grep || echo "(no matches)"
  46. ;;
  47. esac