Created
June 2, 2024 19:35
-
-
Save wojciak/0fd0c48fd4920331e0c582fae41de4ed to your computer and use it in GitHub Desktop.
Nomad restart allocs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if the nomad CLI is installed | |
if ! command -v nomad &> /dev/null; then | |
echo "Nomad CLI could not be found. Please install it first." | |
exit 1 | |
fi | |
# Get a list of all running jobs | |
running_jobs=$(nomad job status -short | grep running | awk '{print $1}') | |
# Check if there are any running jobs | |
if [ -z "$running_jobs" ]; then | |
echo "No running jobs found." | |
exit 0 | |
fi | |
# Loop through each running job | |
for job in $running_jobs; do | |
echo "Processing job: $job" | |
# Get all allocations for the job | |
allocs=$(nomad job status -all-allocs $job | awk '/Allocations/,0' | awk 'NR>2 {print $1}') | |
# Check if there are any allocations | |
if [ -z "$allocs" ]; then | |
echo "No allocations found for job: $job" | |
continue | |
fi | |
# Loop through each allocation and restart it | |
for alloc in $allocs; do | |
echo "Restarting allocation: $alloc for job: $job" | |
nomad alloc restart $alloc | |
done | |
done | |
echo "All allocations of all running jobs have been restarted." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment