bambuddy.service 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # BamBuddy Systemd Service Template
  2. #
  3. # INSTALLATION:
  4. # 1. Copy this file to /etc/systemd/system/bambuddy.service
  5. # 2. Replace placeholders:
  6. # - INSTALL_PATH: Where BamBuddy is installed (e.g., /opt/bambuddy)
  7. # - SERVICE_USER: User to run as (e.g., bambuddy)
  8. # - DATA_DIR: Data directory (e.g., /opt/bambuddy/data)
  9. # - LOG_DIR: Log directory (e.g., /opt/bambuddy/logs)
  10. # 3. Run: sudo systemctl daemon-reload
  11. # 4. Run: sudo systemctl enable bambuddy
  12. # 5. Run: sudo systemctl start bambuddy
  13. #
  14. # Or use the install script: ./install/install.sh
  15. #
  16. [Unit]
  17. Description=BamBuddy - Bambu Lab Print Management
  18. Documentation=https://github.com/maziggy/bambuddy
  19. After=network.target
  20. [Service]
  21. Type=simple
  22. User=SERVICE_USER
  23. Group=SERVICE_USER
  24. WorkingDirectory=INSTALL_PATH
  25. # Environment file (optional - created by install script)
  26. EnvironmentFile=-INSTALL_PATH/.env
  27. # Use virtual environment
  28. Environment="PATH=INSTALL_PATH/venv/bin:/usr/local/bin:/usr/bin:/bin"
  29. # Server configuration
  30. # --loop asyncio is required: uvloop's SSL layer can silently truncate VP FTP
  31. # uploads on a ragged client close over slow storage (#1896). Do not remove.
  32. #
  33. # --timeout-graceful-shutdown is also required. Uvicorn's default is to wait
  34. # forever for in-flight requests, and an MJPEG camera stream is a response that
  35. # never completes — one open camera tile would hang the stop until systemd gave
  36. # up and SIGKILLed, skipping the WAL checkpoint, the MQTT disconnect and the
  37. # virtual-printer teardown entirely. On timeout uvicorn cancels the request
  38. # tasks; the camera generators unwind cleanly on CancelledError.
  39. ExecStart=INSTALL_PATH/venv/bin/uvicorn backend.app.main:app --host 0.0.0.0 --port ${PORT:-8000} --loop asyncio --timeout-graceful-shutdown 5
  40. # Restart policy
  41. Restart=on-failure
  42. RestartSec=5
  43. # Graceful shutdown. Uvicorn now bounds its own wait at 5s and the app's own
  44. # teardown takes ~1-2s, so this only has to be comfortably longer than that —
  45. # it is the backstop, not the mechanism. The old 10s could clip a slow teardown
  46. # on a Pi with several virtual printers.
  47. TimeoutStopSec=30
  48. # Kill zombie ffmpeg processes (timelapse processing)
  49. ExecStartPre=-/usr/bin/pkill -9 -f "ffmpeg.*bambuddy"
  50. ExecStopPost=-/usr/bin/pkill -9 -f "ffmpeg.*bambuddy"
  51. # Logging
  52. StandardOutput=journal
  53. StandardError=journal
  54. SyslogIdentifier=bambuddy
  55. # Allow binding to privileged ports (322 RTSP, 990 FTPS) for Virtual Printer
  56. # mode. Without this the VP's sockets never open and the slicer simply never
  57. # sees the printer — with no obvious error, since the bind failure is one line
  58. # in the journal (#2549). Works alongside NoNewPrivileges=true below: systemd
  59. # raises the ambient set at exec, which is not the privilege escalation that
  60. # setting forbids.
  61. AmbientCapabilities=CAP_NET_BIND_SERVICE
  62. # Security hardening
  63. NoNewPrivileges=true
  64. PrivateTmp=true
  65. ProtectSystem=strict
  66. # ProtectHome=true hides /home/* and breaks ExecStart when INSTALL_PATH is
  67. # under /home (issue #1685). Default is the safer read-only; flip to true if
  68. # your INSTALL_PATH is outside /home (e.g. /opt/bambuddy).
  69. ProtectHome=read-only
  70. #
  71. # ProtectSystem=strict mounts EVERYTHING outside these three paths read-only for
  72. # this service — including a NAS share you have mounted yourself and can write to
  73. # from your own shell. Writes there fail with EROFS ("Read-only file system"),
  74. # which looks like a permission problem but is not one (issue #2544).
  75. #
  76. # So if you point Scheduled Backups at a directory outside the install, data and
  77. # log dirs, add it here — or better, in a drop-in that survives a reinstall:
  78. #
  79. # sudo systemctl edit bambuddy
  80. # [Service]
  81. # ReadWritePaths=/mnt/your-nas-share
  82. #
  83. ReadWritePaths=DATA_DIR LOG_DIR INSTALL_PATH
  84. [Install]
  85. WantedBy=multi-user.target