You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nomad周期性任务无法执行Bash脚本问题排查求助

Nomad Periodic Batch Job Configuration for 5-Minute Script Execution on nomad3 Host

Let's refine your Nomad periodic job to reliably run a script every 5 minutes on the nomad3 node. I'll share the complete working configuration, break down key settings, and include a sample script to test with.

Complete Job Configuration

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

  periodic {
    # Corrected cron for 5-minute intervals (5-field syntax for minutes/hours/etc.)
    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 to run only on the nomad3 node
      constraint {
        attribute = "${node.unique.name}"
        value = "nomad3"
      }

      # Exec driver config: point to your script
      config {
        # If your script has execute permissions, use it directly as the command
        command = "/path/to/your/script.sh"
        # Or use bash to run it if needed
        # command = "bash"
        # args = ["/path/to/your/script.sh"]
      }

      # Optional: Specify resources if your script needs them
      resources {
        cpu = 100
        memory = 64
      }
    }
  }
}

Key Configuration Explanations

  • Cron Expression Fix: Your original 6-field cron (*/5 * * * * *) would run the job every 5 seconds. The corrected 5-field syntax (*/5 * * * *) triggers it every 5 minutes, aligned with your goal. If you need precise second-level timing, use 0 */5 * * * * (6-field: 0 seconds, every 5 minutes).
  • Prohibit Overlap: prohibit_overlap = true ensures a new instance won't start if the previous run is still ongoing—critical for avoiding resource conflicts.
  • Node Constraint: The node.unique.name constraint guarantees the job only runs on the nomad3 host, exactly as you requested.
  • Restart Policy: The restart block defines how Nomad handles failures: it will retry up to 10 times over a 5-minute window, with a 25-second delay between attempts.

Sample Script (/path/to/your/script.sh)

Make sure to set execute permissions with chmod +x /path/to/your/script.sh:

#!/bin/bash

# Example: Log a timestamp to a file
LOG_FILE="/var/log/nomad_periodic_job.log"
echo "Job ran at: $(date -u)" >> "$LOG_FILE"

# Add your custom logic here (e.g., cleanup, data sync, health checks)

内容的提问来源于stack exchange,提问作者FrancisV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:38:19