Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch with Process Substitution

I often run the command

squeue -u $USER | tee >(wc -l)

where squeue is a Slurm command to see how many jobs you are running. This gives me both the output from squeue and automatically tells how many lines are in it.

How can I watch this command?

watch -n.1 "squeue -u $USER | tee >(wc -l)" results in

Every 0.1s: squeue -u randoms | tee >(wc -l)                                                                                                                                                                                                                                                                                                        Wed May  9 14:46:36 2018

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `squeue -u randoms | tee >(wc -l)'
like image 749
Randoms Avatar asked Apr 08 '26 11:04

Randoms


1 Answers

From the watch man page:

Note that command is given to "sh -c" which means that you may need to use extra quoting to get the desired effect.

sh -c also does not support process substitution, the syntax you're using here as >().


Fortunately, that syntax isn't actually needed for what you're doing:

watch -n.1 'out=$(squeue -u "$USER"); echo "$out"; { echo "$out" | wc -l; }'

...or, if you really want to use your original code even at a heavy performance penalty (starting not just one but two new shells every tenth of a second -- first sh, and then bash):

bash_cmd() { squeue -u "$USER" | tee >(wc -l); } # create a function
export -f bash_cmd            # export function to the environment
watch -n.1 'bash -c bash_cmd' # call function from bash started from sh started by watch
like image 162
Charles Duffy Avatar answered Apr 11 '26 13:04

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!