Skip to content

Journal control

Source · io.systemd.Journal · systemd v257+ · Root

Drive systemd-journald the way journalctl --sync, --rotate, --flush and --relinquish-var do. Every method takes no parameters and returns no fields, so each generated method is just func(ctx) error.

Run it

sudo go run ./examples/journal-control sync
Output (real, systemd-journald v255)
2026/10/16 00:10:09 journald: sync done

The code

examples/journal-control/main.go
// Command journal-control asks systemd-journald to flush, sync or rotate
// its files, like `journalctl --sync` / `--rotate` / `--flush`. Requires
// root.
//
//  sudo go run ./examples/journal-control sync
package main

import (
    "context"
    "log"
    "os"
    "time"

    "github.com/rossedman/sdvarlink/systemd/v257/journal"
)

func main() {
    if len(os.Args) != 2 {
        log.Fatal("usage: journal-control sync|rotate|flush|relinquish")
    }
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    c, err := journal.Dial(ctx)
    if err != nil {
        log.Fatal(err)
    }
    defer c.Close()

    switch os.Args[1] {
    case "sync":
        err = c.Synchronize(ctx)
    case "rotate":
        err = c.Rotate(ctx)
    case "flush":
        err = c.FlushToVar(ctx)
    case "relinquish":
        err = c.RelinquishVar(ctx)
    default:
        log.Fatalf("unknown action %q", os.Args[1])
    }
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("journald: %s done", os.Args[1])
}

How it works

  • No input, no output. For method Rotate() -> () the generator emits Rotate(ctx context.Context) error.
  • Root only. journald's socket is mode 0600, so an unprivileged run fails at Dial with permission denied.
  • Version differences. This example pins v257. From v258, Synchronize takes a SynchronizeIn with an optional Offline flag; see Versioning.
  • Timeouts. Rotating and flushing touch disk; the 30-second context keeps a wedged journald from hanging the tool.

See also