Skip to content

Instantly share code, notes, and snippets.

@CyprienRicque
Last active August 20, 2024 07:23
Show Gist options
  • Save CyprienRicque/42601996149c479f8c64a38bd2d73eed to your computer and use it in GitHub Desktop.
Save CyprienRicque/42601996149c479f8c64a38bd2d73eed to your computer and use it in GitHub Desktop.
Enhanced nvidia-smi with username and command line of the processes
#!/bin/bash
nvidia-smi > /tmp/c-nvidia-smi.out
header_end=$(nvidia-smi | grep -n "Processes:" | cut -d: -f1)
cat /tmp/c-nvidia-smi.out | head -n $((header_end))
echo "| GPU PID Command GPU Memory User |"
echo "| Usage |"
echo "|=============================================================================|"
cat /tmp/c-nvidia-smi.out | tail -n +$((header_end)) | head -n -1 | while read -r line; do
pid=$(echo $line | awk '{print $5}')
user=$(ps -o user= -p $pid 2>/dev/null)
if [ ! -z "$user" ]; then
cmd=$(ps -p $pid -o cmd=)
gpu=$(echo $line | awk '{print $2}')
usage=$(echo $line | awk '{print $8}')
if [ ${#cmd} -gt 33 ]; then
cmd="...$(echo "$cmd" | awk '{print substr($0,length($0)-29,30)}')"
fi
printf "%s |%4s %8s %-37s %8s %-12s|\n" "$usage" "$gpu" "$pid" "$cmd" "$usage" "$user"
fi
done | sort -rbh | cut -d " " -f 2-
echo "+-----------------------------------------------------------------------------+"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment