0

I use squeue to display jobs like below

jobs

and I would like to count the number of running jobs (according to the fifth) column. The command

squeue -h -u $USER | awk '{print $5}' | grep R | wc -l

does the job nicely so I put it into alias in ~/.cshrc file as

alias rj "squeue -h -u $USER | awk '{print $5}' | grep R | wc -l"

However, the alias never shows me the correct number. For example

alias

could you please help me on this? Thanks in advance!

0

2 Answers 2

1

Because your whole command is inside double quotes, the inner ' ' don't act as quotes yet but only as regular text, and the $5 is getting expanded as a shell variable right there during alias definition. It's probably an empty string, so your awk command becomes awk '{print }' which no longer cuts off unwanted columns, and as a result your grep command matches too much.

Backslash escape the $:

alias fj "... | awk '{print \$5}' | ..."
2
  • Thanks, I tried to escape $ but it is giving me backslash not last character on line error when I tried to run it. I then tried to escape ' and {}, neither worked for me.
    – zyy
    Aug 14, 2020 at 23:10
  • tcsh handling of quotes is different; need "...{print"\$"5} ..." (yuck) Aug 17, 2020 at 5:12
0

I ended up having to write a script for this, again, I am using tcsh. This is the script

#!/usr/bin/tcsh
set n_running = `squeue -h -u $USER | awk '{print $5}' | grep R | wc -l`
set n_total = `squeue -h -u $USER | wc -l`
echo $n_running/$n_total

save it as jn and do chmod +x jn, then in ~/.cshrc file you write

alias jn '/path/to/jn'

and the path has to be absolute path. Then source ~/.cshrc file and you are ready to use jn!

This script will print out number of running jobs over number of total jobs.

4
  • 1
    You don't need 4 different programs; more efficient and simpler squeue -h -u $USER | awk -vs=/ '$5~/R/{++n} END{print +n s NR}' . But as above quoting this into an alias is clumsy in tcsh. Aug 17, 2020 at 5:14
  • @dave_thompson_085 This is brilliant! I learned a lot from your code, but could you maybe explain what does -vs=/ mean? I just could not find a reference to it. Also, it seems like in the print statement, having +n or n printed is equivalent, is that right?
    – zyy
    Aug 18, 2020 at 2:12
  • 1
    awk -vname=value sets a variable, like name=value within the script; here it sets s to (the string containing) a slash character. I used this instead of a doublequoted string literal because I was (initially) hoping to do it in an alias and didn't want to have conflicting quotes. awk variables start out as the empty string if not explicitly initialized, which is treated as zero in arithmetic but not for printing; thus if there are no matches print n prints nothing but print +n prints 0 which is clearer and more consistent. Aug 18, 2020 at 10:11
  • @dave_thompson_085 Thanks a lot! This is very helpful!
    – zyy
    Aug 18, 2020 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.