1

alias vov="scontrol -o show nodes | grep -e \"-lkeb\" -e \"-gpu\"| awk '{ printf(\"%-15s%-9s%-7s%-18s%-11s%-9s%s\n\", substr(\$1, 10), substr(\$4, 10), substr(\$5, 8), substr(\$9, 6), substr(substr(\$23, 12)/1024,0,5), substr(substr(\$24, 10)/1024,0,5), substr(\$38, 11))}' | awk '{printf(\"%-15s%-7s%-9s%-18s%-7s%-9s%50-s%2-s%9-s%9-s%9-s\n\", \$1, \$3, \$2, \$4, \$5, \$6, \"|\"\$7, \"|\", \$3-\$2, \$5-\$6, substr(\$4,length(\$4))-substr(\$7,length(\$7)))}' | awk '\$11 != 0' | awk 'BEGIN {print \"NodeName CPUTot CPUAlloc Gres MemTot MemAlloc AllocTres CPUAvail MemAvail GPUAvail CPU/GPU\"}{print \$0 FS \$9/\$11}'"

The above code works well when I write it to my .bashrc file.

watch "scontrol -o show nodes | grep -e \"-lkeb\" -e \"-gpu\"| awk '{ printf(\"%-15s%-9s%-7s%-18s%-11s%-9s%s\n\", substr(\$1, 10), substr(\$4, 10), substr(\$5, 8), substr(\$9, 6), substr(substr(\$23, 12)/1024,0,5), substr(substr(\$24, 10)/1024,0,5), substr(\$38, 11))}' | awk '{printf(\"%-15s%-7s%-9s%-18s%-7s%-9s%50-s%2-s%9-s%9-s%9-s\n\", \$1, \$3, \$2, \$4, \$5, \$6, \"|\"\$7, \"|\", \$3-\$2, \$5-\$6, substr(\$4,length(\$4))-substr(\$7,length(\$7)))}' | awk '\$11 != 0' | awk 'BEGIN {print \"NodeName CPUTot CPUAlloc Gres MemTot MemAlloc AllocTres CPUAvail MemAvail GPUAvail CPU/GPU\"}{print \$0 FS \$9/\$11}'"

The above code also works well when I paste it to my terminal (/bin/bash). Actually the code to be watched is just the vov.

But I met some issues:

  1. I can not use watch vov in my terminal (/bin/bash).
  2. I can not use alias to assign the second code (longer version) to another variable in my .bashrc.
  3. I can not use alias to assign watch vov (shorter version) to another variable in my .bashrc.

Can you help me to solve the 3 issues?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Jingnan Jia
  • 111
  • 3

1 Answers1

0

I've run into that same trouble with watch and shell aliases. I'm not sure why the aliases aren't available to watch, but you can solve (or work-around) this by putting your aliases into a shellscript, and calling watch on that.

Create a file called vov with the shebang #!/bin/bash on the first line and then the scontrol...etc on subsequent lines, save the file, chmod +x vov, mv vov ~/.local/bin/ (or some other place on your $PATH).

Then you can do watch -t -n 1 -c vov

I forget what flags watch needs so I have a shellscript called sho that looks like watch -t -n 1 -c "$1" so with that you could do sho vov.

alec
  • 1,708
  • Thanks for your answer. Do you have any solution without another seperate script? – Jingnan Jia Jun 09 '22 at 08:48
  • No. You should either: 1) just get used to using custom scripts because they're useful (I have ~/bin on my $PATH to make this more convenient), or 2) troubleshoot the scenario more to get a bare-minimum repeatable instance (no need for giant 1liners which detract from the question (unnecessary code make it harder for people to know what you're talking about)) and ask a more clearn+informed question. #1 achieves your objective. #2 is interesting and I'd like to know what's up with shell aliases + watch in this context too, but it might not be worth the time and effort. – alec Jun 09 '22 at 12:15