Skip to content

Make it possible to start the driver process in background #3

Description

@AlexandreBlais42

Klack has this snippet (https://codeberg.org/ForeverZer0/klack/src/branch/master/src/main.zig)

fn daemonize(io: Io) File {
    // Fork the process and exit the parent.
    if (std.os.linux.fork() != 0) {
        exit(0);
    }
    // Detach from terminal and start new session
    _ = std.os.linux.setsid();

    // Get handle to /dev/null to direct output to
    const dev_null = Dir.cwd().openFile(io, "/dev/null", .{
        .mode = .read_write,
    }) catch @panic("Unable to open /dev/null");

    // Redirect all input/output to /dev/null
    _ = std.os.linux.dup2(dev_null.handle, File.stdout().handle);
    _ = std.os.linux.dup2(dev_null.handle, File.stderr().handle);
    _ = std.os.linux.dup2(dev_null.handle, File.stdin().handle);

    return dev_null;
}

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions