{"record":{"id":"40652cbcfeee7ebd","repo":"hashicorp/nomad","slug":"exec-w","errorCode":null,"errorMessage":"Exec: %w","messagePattern":"Exec: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"drivers/shared/executor/executor.go","lineNumber":448,"sourceCode":"\t}\n\n\t// Run the runningFunc hook after the process starts\n\tif err := running(); err != nil {\n\t\treturn nil, err\n\t}\n\n\t// Wait on the task process\n\tgo e.wait()\n\treturn &ProcessState{Pid: e.childCmd.Process.Pid, ExitCode: -1, Time: time.Now()}, nil\n}\n\n// Exec a command inside a container for exec and java drivers.\nfunc (e *UniversalExecutor) Exec(deadline time.Time, name string, args []string) ([]byte, int, error) {\n\tctx, cancel := context.WithDeadline(context.Background(), deadline)\n\tdefer cancel()\n\n\tif cleanup, err := e.setSubCmdCgroup(&e.childCmd, e.command.StatsCgroup()); err != nil {\n\t\treturn nil, 0, fmt.Errorf(\"Exec: %w\", err)\n\t} else {\n\t\tdefer cleanup()\n\t}\n\n\treturn ExecScript(ctx, e.childCmd.Dir, e.command.Env, e.childCmd.SysProcAttr, e.command.NetworkIsolation, name, args)\n}\n\n// ExecScript executes cmd with args and returns the output, exit code, and\n// error. Output is truncated to drivers/shared/structs.CheckBufSize\nfunc ExecScript(ctx context.Context, dir string, env []string, attrs *syscall.SysProcAttr,\n\tnetSpec *drivers.NetworkIsolationSpec, name string, args []string) ([]byte, int, error) {\n\n\tcmd := exec.CommandContext(ctx, name, args...)\n\n\t// Copy runtime environment from the main command\n\tcmd.SysProcAttr = attrs\n\n\tcmd.Dir = dir","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/drivers/shared/executor/executor.go#L430-L466","documentation":"UniversalExecutor.Exec runs an additional command inside the existing task's containment. Before ExecScript it moves the sub-command into the task's stats cgroup via setSubCmdCgroup; any error there is wrapped as 'Exec: %w'. The actual in-container command execution happens afterwards and is not part of this error.","triggerScenarios":"Exec(deadline, name, args) is called (e.g. `nomad alloc exec`) and setSubCmdCgroup fails writing the child PID into the task's cgroup.procs — cgroup path gone (task exited), cgroup not writable, or StatsCgroup() empty/invalid.","commonSituations":"Running alloc exec after the task process already exited (its cgroup was cleaned up), exec into tasks on hosts with read-only cgroup mounts, cgroup v2 hosts with older Nomad builds, driver restarted between task start and exec.","solutions":["Confirm the task/alloc is still running — a dead task's cgroup is gone; re-run exec on a live alloc","Check the wrapped %w cause: ENOENT/EACCES on cgroup.procs indicates cleanup or permissions","Ensure /sys/fs/cgroup is writable and the client has privileges (root or delegated cgroups)","Upgrade Nomad if on cgroup v2 with an older release lacking v2 support"],"exampleFix":"// before\n// nomad alloc exec -job web date   # alloc already dead\n// after\n// nomad status web && nomad alloc exec -job web date  # target a running alloc","handlingStrategy":"validation","validationCode":"func execTargetAlive(allocStatus string, statsCgroup string) error {\n  if allocStatus != \"running\" { return fmt.Errorf(\"alloc not running: %s\", allocStatus) }\n  if statsCgroup == \"\" { return errors.New(\"no stats cgroup for task\") }\n  if _, err := os.Stat(filepath.Join(statsCgroup, \"cgroup.procs\")); err != nil {\n    return fmt.Errorf(\"task cgroup gone (task exited?): %w\", err)\n  }\n  return nil\n}","typeGuard":null,"tryCatchPattern":"out, code, err := executor.Exec(deadline, \"sh\", args)\nif err != nil {\n  if strings.Contains(err.Error(), \"Exec:\") && strings.Contains(err.Error(), \"no such file\") {\n    return fmt.Errorf(\"task cgroup disappeared; alloc likely dead: %w\", err)\n  }\n  return err\n}","preventionTips":["Check alloc status before running alloc exec","Avoid exec-ing into tasks right after stop/kill signals","Grant cgroup delegation if the client is non-root","Upgrade for cgroup v2 compatibility on modern hosts"],"tags":["cgroups","exec","subcommand","nomad-executor"],"backgroundTag":"cgroup-configuration-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}