Skip to content

Instantly share code, notes, and snippets.

@sean-smith
Last active March 13, 2024 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sean-smith/d290af1da81cf53896f4304beab3eaea to your computer and use it in GitHub Desktop.
Save sean-smith/d290af1da81cf53896f4304beab3eaea to your computer and use it in GitHub Desktop.
Get instance ID to hostname mapping from a Slurm job.

Slurm Get Instance ID to Hostname

Update: you only need the following:

mpirun -N 1 -n 2 bash -c 'echo $(hostname): $(cat /sys/devices/virtual/dmi/id/board_asset_tag | tr -d " ")'
  1. Create a file get-instance-id.sh:
#!/bin/bash

hostname=$(hostname)
token=$(curl -sX PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
instance_id=$(curl -sH "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/instance-id)

echo "$hostname -> $instance_id"
  1. Add the following snippet to your Slurm submit script, the three lines at the top are optional.
#!/bin/bash
#SBATCH -N 2
#SBATCH --exclusive

# get all nodes allocated to the job.
NODES=$( scontrol show hostnames $SLURM_JOB_NODELIST )
for node in $NODES
do
        srun -w $node -N 1 bash get-instance-id.sh
done
  1. Then run and you'll get the following output:
ubuntu@p5-st-p5-1:~$ sbatch hostnames.sh
Submitted Job 20
ubuntu@p5-st-p5-1:~$ cat slurm-20.out
p5-st-p5-1 -> i-0f53b99e0960906ed
p5-st-p5-2 -> i-0221a76c26c555381
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment