Why does this task keep on restarting - What does NOMAD consider a successful task?

I am curious as to what Nomad thinks is a successful task ?

I am doing something like this

task "mkdir" {
      driver = "raw_exec"
      config { 
        command = "bash" 
        args = ["-c", "rm -rf /var/log/service; mkdir -p /var/log/service/foo"]
      }
    }

The folder does get created but Nomad keeps on attempting to do this task.
Any idea why that happens ? Why does Nomad keep repeating this task even though the folder got created ? I am trying to understand what Nomad considers a successful task ?

Hi @rajeshkhan808;

It depends what scheduler type you are using to run the job. The default scheduler type is service. A task of this type is expected to run until it is explicitly told to stop by an action such as a new deployment, or an operator initiated stop call. If this task exits, it will restart it as this is seen as a failure.

To run one-off commands like this, you should set the job specification type to batch. Batch jobs are not expected to run indefinitely and when the tasks exits with a zero exit code, the task will be considered complete.

Your full job spec would look something similar to the following given your example:

job "mkdir" {

  datacenters =  ["dc1"]
  type        = "batch"

  group "mkdir" {
    task "mkdir" {
      driver = "raw_exec"
      config { 
        command = "bash" 
        args = ["-c", "rm -rf /var/log/service; mkdir -p /var/log/service/foo"]
      }
    }
  }
}

Thanks,
jrasell and the Nomad team

Thank you for clearing that up