Bỏ qua để đến nội dung

Chạy Bestie ở nền

Dùng background runtime khi bạn muốn Telegram, Zalo hoặc cron schedules tiếp tục chạy sau khi terminal hiện tại thoát. Bestie hỗ trợ hai cách chạy local:

  • bestie daemon ... khởi động và theo dõi các process tách rời từ CLI.
  • bestie service ... cài user-level startup integration cho platform hiện tại.

Dùng bestie daemon ... khi cần điều khiển thủ công, debug local hoặc start/stop từng runtime target. Dùng bestie service ... khi bạn muốn Bestie tự chạy lúc đăng nhập hoặc được hệ điều hành quản lý.

Manual daemon

Phù hợp để debug, chạy trên hệ không phải Linux và start/stop từng runtime target.

User service

Phù hợp cho local install chạy lâu dài, cần tự chạy lại sau login hoặc restart.

Runtime targets

Telegram, Zalo, cron và local UI có thể chạy qua platform startup integration.

Safety check

Chỉ chạy một background mode cho cùng một target để tránh polling hoặc cron bị chạy trùng.

Bestie quản lý ba runtime target:

  • telegram chạy bestie channels telegram.
  • zalo chạy bestie channels zalo.
  • cron chạy bestie cron run.

Lệnh daemon dùng --channel telegram|zalo|cron|all. Nếu không truyền --channel, daemon command mặc định nhắm tới Telegram.

Hoàn tất setup cho từng kênh bạn muốn chạy:

  • Telegram cần channels.telegram.enabled, owner id hoặc username, và giá trị env chứa Telegram bot token.
  • Zalo cần channels.zalo.enabled, owner id, và giá trị env chứa Zalo bot token.
  • Cron cần ít nhất một schedule nếu bạn mong có job được chạy.

Chạy Doctor trước khi bật process lâu dài:

Terminal window
bestie doctor
bestie channels doctor

Nếu kênh chưa cấu hình đúng, daemon vẫn có thể start, nhưng process của kênh sẽ ghi lỗi vào daemon log.

Chọn một background mode cho vận hành bình thường. Chạy đồng thời manual daemon và user service cho cùng một target có thể tạo polling hoặc cron trùng lặp.

Start một kênh:

Terminal window
bestie daemon start --channel telegram
bestie daemon start --channel zalo
bestie daemon start --channel cron

Start toàn bộ daemon channel được hỗ trợ:

Terminal window
bestie daemon start --channel all

Khi start, Bestie ghi daemon state trong runtime app directory và in process id cùng log path. Mỗi target có state file và log file riêng, ví dụ daemon-telegram.jsondaemon-telegram.log, daemon-zalo.jsondaemon-zalo.log, hoặc daemon-cron.jsondaemon-cron.log.

Start một daemon đang chạy sẽ không tạo bản thứ hai. Bestie sẽ báo process id hiện có.

Kiểm tra daemon Telegram mặc định:

Terminal window
bestie daemon status

Kiểm tra tất cả daemon channel:

Terminal window
bestie daemon status --channel all

Status có thể báo:

  • running khi process id được ghi lại vẫn đang chạy.
  • stale khi Bestie có daemon state file nhưng process không còn chạy.
  • stopped khi kênh đó chưa có daemon state.

Nếu status là stale, restart daemon để xóa state cũ và tạo process nền mới.

Với state daemon Telegram kiểu cũ, Bestie vẫn nhận file legacy daemon.json để status, stoprestart có thể dọn qua command hiện tại.

Restart kênh sau khi đổi config, token, voice setting hoặc cron schedule:

Terminal window
bestie daemon restart --channel telegram
bestie daemon restart --channel zalo
bestie daemon restart --channel cron

Restart mọi daemon channel:

Terminal window
bestie daemon restart --channel all

Restart gửi SIGTERM tới process được ghi lại, đợi process thoát, xóa state file cũ rồi start process mới. Nếu process id đã bị tái sử dụng bởi command khác, Bestie xóa state stale thay vì kill process không liên quan.

Stop một kênh:

Terminal window
bestie daemon stop --channel telegram

Stop mọi daemon channel:

Terminal window
bestie daemon stop --channel all

Bestie đợi tối đa 30 giây để process dừng. Trước khi stop process được ghi lại, Bestie kiểm tra command line của process khi nền tảng hỗ trợ. Nếu pid hiện thuộc về thứ khác, Bestie xóa daemon state stale thay vì kill process đó. Nếu process Bestie không thoát kịp, command báo timeout và giữ state để bạn tiếp tục troubleshoot.

Bestie có thể tạo user-level startup integration. Tính năng này thuộc lệnh bestie service, không phải installer và không phải bestie daemon:

Terminal window
bestie service install

Trên Linux, lệnh này ghi bestie.service vào user systemd config directory, xóa các per-channel service file cũ nếu có, rồi chạy:

Terminal window
systemctl --user daemon-reload
systemctl --user enable --now bestie.service

Linux service unit chạy bestie service run ở foreground với Restart=on-failure, RestartSec=5TimeoutStopSec=45. Khi start, Bestie chọn installable targets từ local config:

  • cron luôn được include mặc định.
  • telegram chỉ được include khi Telegram channel bật và giá trị env của bot token đã có.
  • zalo chỉ được include khi Zalo channel bật và giá trị env của bot token đã có.

Kiểm tra status hint của service command:

Terminal window
bestie service status

Trên macOS, bestie service install ghi launchd LaunchAgents cho service runtime và local UI. Trên Windows, lệnh này ghi Bestie.lnkBestie.ico vào Startup folder của user; shortcut start bestie daemon start --channel all, đồng thời start Web UI companion daemon.

Restart user service sau khi đổi channel, token, voice, cron hoặc UI config:

Terminal window
bestie service restart

Gỡ service:

Terminal window
bestie service uninstall

Trên Linux, uninstall disable và stop bestie.service, xóa service file rồi reload user systemd daemon. Nó cũng xóa các per-channel service file cũ tên bestie-telegram.service, bestie-zalo.servicebestie-cron.service nếu tồn tại. Service unit bị thiếu sẽ được bỏ qua khi uninstall. Trên macOS, uninstall unload và xóa Bestie LaunchAgents. Trên Windows, lệnh này xóa Startup shortcut và icon.

Không tự chạy bestie service run cho nhu cầu thường ngày. Đây là hidden foreground command được platform service integration dùng.

Daemon command in log path khi process start hoặc khi status tìm thấy running/stale state. Dùng path đó để debug lỗi khởi động kênh.

Với user service, dùng status hint Bestie in ra. Trên Linux, xem systemd journal của bestie.service; trên macOS, xem launchd logs; trên Windows, kiểm tra Startup shortcut và Bestie daemon logs.

Bạn cũng có thể xem log Bestie đã redacted gần đây bằng tools command:

Terminal window
bestie tools logs --lines 20

Không paste bot token, provider key, .bestie/.env hoặc raw daemon log vào public issue tracker. Logs có thể chứa ngữ cảnh vận hành dù secret được thiết kế để nằm trong env files.

Chạy kênh trực tiếp một lần để thấy lỗi ngay trong terminal:

Terminal window
bestie channels telegram --once
bestie channels zalo --once

Sau đó kiểm tra channel diagnostics:

Terminal window
bestie channels doctor --channel telegram --connect
bestie channels doctor --channel zalo --connect

Restart kênh bị ảnh hưởng:

Terminal window
bestie daemon restart --channel telegram

Bestie xóa stale state trong lúc restart và ghi state file mới cho process thay thế.

Startup integration đã chuyển sang service command. Dùng:

Terminal window
bestie service install
bestie service status

Manual daemon lifecycle command chỉ gồm:

Terminal window
bestie daemon start --channel all
bestie daemon stop --channel all
bestie daemon restart --channel all
bestie daemon status --channel all

Bestie luôn include cron service target trong service runtime. Telegram và Zalo service target bị skip nếu kênh chưa bật hoặc giá trị env của bot token chưa có. Trên macOS và Windows, local UI cũng có thể được platform integration start.

Kiểm tra channel setup và env values, sau đó restart service:

Terminal window
bestie channels doctor --channel telegram --connect
bestie channels doctor --channel zalo --connect
bestie service restart

Nếu stop báo daemon pid không dừng trong timeout, xem log path từ status, rồi chỉ stop process thủ công sau khi xác nhận pid vẫn thuộc về Bestie.

Terminal window
bestie daemon status --channel telegram