Skip to content

Conversation

@4iKZ
Copy link

@4iKZ 4iKZ commented Dec 2, 2025

What this PR does

This PR adds daemon mode support for the GreptimeDB standalone command, allowing users to run the database in the background using the --daemon or -d flag. This enhancement improves the usability of GreptimeDB on ARM64 boards or edge devices, enabling it to run independently of shell sessions, enhancing its resilience and availability.

Implementation details

  1. Modified files :

    • src/cmd/src/standalone.rs : Added daemon field to StartCommand struct, made SubCommand enum and subcmd field public
    • src/cmd/src/bin/greptime.rs : Added daemon mode check logic and daemonize function
  2. Key changes :

    • Added --daemon / -d command line flag to standalone start command
    • Implemented daemonize() function with platform-specific handling:
      • Unix-like systems : Uses nix library to implement complete daemonization:
        • Double fork to detach from parent process
        • Create new session to become session leader
        • Change working directory to root
        • Close and redirect standard file descriptors to /dev/null
      • Windows systems : Returns Ok for compatibility, no daemonization logic
    • Made necessary fields and enums public to allow access from the bin crate
  3. Usage example :

    greptime standalone start --daemon
    # or
    greptime standalone start -d
    

Problem solved

When running GreptimeDB in standalone mode on environments like ARM64 boards or edge devices, the process currently depends on the shell session that started it. If the user disconnects (e.g., network cable removed or SSH session closed), the GreptimeDB process exits.

This PR addresses this issue by adding a built-in daemon mode, allowing GreptimeDB to continue running in the background independently of the user session, making it more robust and user-friendly for embedded or self-hosted setups.

Benefits

  • Improved resilience : Database continues running even if shell session is closed
  • Better user experience : No need to use external tools like nohup or create custom systemd service files
  • Consistent behavior : Works the same way across different Unix-like systems
  • Cross-platform compatibility : Gracefully handles Windows systems
  • Easy to use : Simple command line flag for quick setups

Testing

The implementation has been successfully compiled with cargo build --bin greptime and follows the project's code style and error handling patterns. It maintains compatibility with existing functionality while adding the new daemon mode feature.

Future enhancements

Potential future improvements could include:

  • Adding support for writing PID files
  • Enhancing Windows support with proper service integration
  • Adding log file rotation for daemon mode
    This change aligns with GreptimeDB's goal of being a robust and user-friendly database for edge and cloud environments, making it more suitable for deployment in resource-constrained devices and embedded systems.

@4iKZ 4iKZ requested a review from a team as a code owner December 2, 2025 10:35
@github-actions github-actions bot added size/XS docs-not-required This change does not impact docs. labels Dec 2, 2025
@github-actions github-actions bot added size/XXL and removed size/XS labels Dec 2, 2025
@sunng87
Copy link
Member

sunng87 commented Dec 3, 2025

hey @4iKZ , thank you for this. We already have contributor working on this #7317 and we prefer to use daemonize library for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-not-required This change does not impact docs. size/XXL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants