'Scheduled termination of nomad job

I'm looking for analogue of nomad stop <job> command which I can schedule from the job itself. Is there such a thing in Nomad? I failed to find anything which can do it. It looks like Nomad fundamentally only starting jobs, but stopping them is not something which can be specified in the definition.

The only idea I have is to have two jobs, one to start and another is to stop. The 'stop' one will use command line to stop the other one. Is that the right way? Wondering how everyone else is doing this.



Solution 1:[1]

You can always just have your job exit with code 0, jobs don't need to run forever. Nomad will then mark the job as "complete" and you're done.

If the job itself can't control its own exit then yes, you might need to try and set up some external job that can make api calls to stop the task, but I'd recommend trying to just have the job exit when it has completed its task.

Solution 2:[2]

Nomad have different schedulers for different type of jobs.

For you requirement, you can use Nomad Batch job.

The default job type is service so if you don't mention any job type it will run as a service job which is expected to not exit unless stopped explicitly. You can mention the job type in the Nomad job file as follows:

job "sample" {
  type = "batch"
  # ...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 maxm
Solution 2 Sanket Dalvi