This is the easiest way to query nvidia-smi for the gpu id nvidia-smi --query-gpu=gpu_uuid --format=csv | tail -n +2