hashicorp/nomad · error

both -n and -c set

Error message

both -n and -c set

What it means

`nomad alloc logs` accepts either -n (tail N lines) or -c (tail N bytes), but not both, because the offset into the log file cannot be computed two ways at once. handleSingleFile returns this error when both flags were explicitly set on the command line.

Source

Thrown at command/alloc_logs.go:287

	return 0
}

func (l *AllocLogsCommand) handleSingleFile(client *api.Client, alloc *api.Allocation, logType string) error {
	// We have a file, output it.
	var r io.ReadCloser
	var readErr error
	if !l.tail {
		r, readErr = l.followFile(client, alloc, logType, api.OriginStart, 0)
		if readErr != nil {
			return fmt.Errorf("error reading file: %v", readErr)
		}
	} else {
		// Parse the offset
		var offset = defaultTailLines * bytesToLines

		if nLines, nBytes := l.numLines != -1, l.numBytes != -1; nLines && nBytes {
			return errors.New("both -n and -c set")
		} else if nLines {
			offset = l.numLines * bytesToLines
		} else if nBytes {
			offset = l.numBytes
		} else {
			l.numLines = defaultTailLines
		}

		r, readErr = l.followFile(client, alloc, logType, api.OriginEnd, offset)

		// If numLines is set, wrap the reader
		if l.numLines != -1 {
			r = NewLineLimitReader(r, int(l.numLines), int(l.numLines*bytesToLines), 1*time.Second)
		}

		if readErr != nil {
			return fmt.Errorf("error tailing file: %v", readErr)
		}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Remove either -n or -c from the command, keeping only one.
  2. Default behavior (no flags) tails the default number of lines; use that if you don't need a specific size.
  3. If scripting, set only one flag conditionally based on the unit the user chose.

Example fix

// before
$ nomad alloc logs -n 100 -c 5000 1f2b3c4d
// after
$ nomad alloc logs -n 100 1f2b3c4d   # or: -c 5000, but not both
Defensive patterns

Strategy: validation

Validate before calling

if n != nil && c != nil {
    return errors.New("pass only one of -n (lines) or -c (bytes)")
}

Try / catch

out, err := runAllocLogs(); if err != nil && strings.Contains(err.Error(), "both -n and -c") {
    fmt.Fprintln(os.Stderr, "use -n or -c, not both")
    os.Exit(1)
}

Prevention

When it happens

Trigger: Running `nomad alloc logs -n 100 -c 5000 <alloc>` — both line-count and byte-count tails requested simultaneously.

Common situations: Copy-pasting example commands that include both flags, or scripting where a default -n is combined with a user-supplied -c.

Understand the failure class

Background: "mutually exclusive" flag errors: what "can't supply both nx and xx", "--raw is not compatible with -i" and "cannot be used with" mean, and how to fix them — this error's family across 29 libraries.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/77548bfac8095ca6. Report an issue: GitHub.