I'm using dwm tiling window manager in Arch Linux. I want to customize the top bar display. I already added date, memory, now playing and uptime stuff, but the CPU display won't work for me. When I try to run the code to display CPU usage in terminal, it works perfect, but it doesn't work when I put it in the top bar. It doesn't display anything at all. Here's the code:
while true; do
xsetroot -name "Memory $(free -m | grep '^Mem' | awk '{print "total: " $2 "MB used: " $3"MB"}') | CPU $(top -n 1 | grep '^Cpu' | tr -d 'usy,' | awk '{print "user " $2 ", sys " $3}') | Uptime: $(uptime | awk '{print $3}' | tr -d ',') | Now playing: $(ncmpcpp --now-playing | tr -d '(:[:digit:])') | $( date +"%F %R" )"
sleep 20s
done &
The part I'm talking about is after "CPU" string. The code is in my .xinitrc and, as I said, everything works good except CPU display.
You can fix this by executing top with the flag -b for batch-mode. Only in this mode you can send the output as normal text to other programs. So this loop will (hopefuly) do the job:
while true; do
xsetroot -name "Memory $(free -m | grep '^Mem' | awk '{print "total: " $2 "MB used: " $3"MB"}') | CPU $(top -bn 1 | grep '^Cpu' | tr -d 'usy,' | awk '{print "user " $2 ", sys " $3}') | Uptime: $(uptime | awk '{print $3}' | tr -d ',') | Now playing: $(ncmpcpp --now-playing | tr -d '(:[:digit:])') | $( date +"%F %R" )"
sleep 20s
done &
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With