0

I have this periodic task below in nomad. The purpose is to run a script on host nomad3 every 5 minutes. I've also included the contents of the script below.

job "periodic" {
  datacenters = ["dc1"]

  type = "batch"

  periodic {
    cron             = "*/5 * * * * *"
    prohibit_overlap = true
    time_zone        = "UTC"
  }

  group "periodic" {
    count = 1

    restart {
      interval = "5m"
      attempts = 10
      delay    = "25s"
      mode     = "delay"
    }

    task "cronjob-test" {
      driver = "exec"

      constraint {
        attribute = "${node.unique.name}"
        value = "nomad3"
      }

      config {
        command = "/bin/bash"
        args = ["/usr/local/bin/cronjob-test.sh"]
      }

      resources {
        cpu = 100 # Mhz
        memory = 10 # MB

        network {
          mbits = 10

          # Request for a dynamic port
          port "uptime" {
          }
        }
      }
    }
  }
}

with the cronjob-test.sh containing:

#!/usr/bin/env bash

DATE=$(date '+%F-%s')
touch /tmp/$DATE

The script is on nomad host nomad3. I'm supposed to see a file in /tmp with the format I indicated in the bash script but I'm not seeing this. Need help on what is going on.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
FrancisV
  • 1,619
  • 5
  • 21
  • 36

3 Answers3

1

The below script will create a 0 byte file under /tmp:-

DATE=$(date '+%F-%s')
touch /tmp/$DATE //file name would be 2018-04-04-%s

Check your DATE variable whether it is correctly set. Just add a echo command like $DATE and check whether that is accepted or not.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
0

It appears I was using the exec driver which is chrooted. I tried using raw_exec and it worked. I then re-tried using exec by adding the target directory in the chroot_env in the configuration and it worked as well.

FrancisV
  • 1,619
  • 5
  • 21
  • 36
-1

Use this code:

config {
    command = "/bin/bash"
    args = ["-c","/usr/local/bin/cronjob-test.sh"]
  }
jkdev
  • 11,360
  • 15
  • 54
  • 77