[Linux] Scripting with xfce4-panel's "Generic Monitor" applet

griimnak

You're a slave to the money then you die
Jul 20, 2013
955
794
Scripting on the xfce4-panel
The xfce4-panel is extremely customizable, offering a wide variety of applets and addons to satisfy the user's needs.

HOWEVER, if you truly desire to make the panel your own, the most important applet, in my opinion atleast, is the Generic Monitor applet.

1ko94KC.png

All it takes is minutes of basic scripting knowledge to transform your panel with this simple applet.

Here's an example using my setup:
Xt68faP.png

Tutorial
Starting with a brand new panel:

8tArjRL.png



Click → + (Add new item to panel)
Search → "Generic Monitor"
Click → Add

KBJyyNb.png

You should now see the default text for the applet on your bar (genmon)XXX
Right-click → "(genmon)XXX" → Select "Properties"

ybnlv2U.png


Now the magic happens.
For starters, you can try simple shell commands such as uname -r.
These less complex commands such as uname -r may be ran without the creation of an external shell script.

gNPdOud.png

Now let's increase the complexity.

We need a place to store our more complex scripts, I'm going to choose a directory called ~/.genmonscripts, you can use any location and name you wish as long as it's accessible to your linux user.
Code:
mkdir ~/.genmonscripts && cd ~/.genmonscripts
touch freemem.sh

The main idea is to perform your work in the script and finally echo or print out the string you wish to display on the xfce4-panel.
freemem.sh:
Bash:
#!/bin/bash
# create variable FREE_MEM, then pipe data from free -h into awk which selects the part of the text you would want
FREE_MEM=`free -h | awk '/^Mem:/ {print $3 "/" $2}'`
echo "ram $FREE_MEM  "
# printed out to panel

Once you have the script written, we can test if it works on our panel.
Note: the ~ alias will not work, you must use the real path. (typically /home/yourusername/)

Code:
sh /home/griimnak/.genmonscripts/freemem.sh
(don't forget sh at the beginning)
eZZOltN.png


And that's basically it!
This may seem simple, but someone with advanced scripting knowledge could do some really impressive things with this applet.
For example, you could write a script that checks how many new emails you have, etc.

Linting your scripts
Linting your scripts are just generally considered good practice and minimize memory leaks from inefficient or invalid scripts.

terminal.png


Download and install

is a static linting tool for shell scripts.
Installation guide:

My scripts
To assist you further with understanding how to write scripts for genmon, i'll be providing the scripts i personally use.

name: cputemp.sh
desc: returns the temp of the cpu package
useage: sh cputemp.sh
Bash:
#!/bin/bash


if which sensors > /dev/null; then
    sensors | grep Core | awk '{print $3;}' | grep -oEi '[0-9]+.[0-9]+' | awk '{total+=$1; count+=1} END {print total/count,"C  "}'
else
    "??"
fi


name: sysload.sh
desc: returns both current cpu usage % and used ram/maxram ratio in one string.
usage: sh sysload.sh
Bash:
#!/bin/bash
CPU=`eval $(awk '/^cpu /{print "previdle=" $5 "; prevtotal=" $2+$3+$4+$5 }' /proc/stat); sleep 0.4; eval $(awk '/^cpu /{print "idle=" $5 "; total=" $2+$3+$4+$5 }' /proc/stat); intervaltotal=$((total-${prevtotal:-0})); echo "$((100*( (intervaltotal) - ($idle-${previdle:-0}) ) / (intervaltotal) ))"`
FREE_MEM=`free -h | awk '/^Mem:/ {print $3 "/" $2}'`
printf "cpu %.f%%  " $CPU
echo "ram $FREE_MEM  "


name: netload.sh
desc: returns current network usage (up and down)
usage: sh netload.sh <adapter>
Note: Set update interval to 1.6 or above on genmon
Bash:
#!/bin/bash


if [ -z "$1" ]; then
    echo
    echo usage: $0 network-interface
    echo
    echo e.g. $0 eth0
    echo
    exit
fi

IF=$1

R1=`cat /sys/class/net/$1/statistics/rx_bytes`
T1=`cat /sys/class/net/$1/statistics/tx_bytes`
sleep 1
R2=`cat /sys/class/net/$1/statistics/rx_bytes`
T2=`cat /sys/class/net/$1/statistics/tx_bytes`
TBPS=`expr $T2 - $T1`
RBPS=`expr $R2 - $R1`
TKBPS=`expr $TBPS / 1024`
RKBPS=`expr $RBPS / 1024`
printf "%s: ↓%dK ↑%dK  " $1 $RKBPS $TKBPS

Hopefully this was useful to someone out there, Happy scripting!
 
Last edited:

griimnak

You're a slave to the money then you die
Jul 20, 2013
955
794
you should use shellcheck
Seems pretty cool, basically linting for shell scripts?
Thanks for the suggestion, never knew of it until now

update: Added "Linting your scripts" section to original post.
 
Last edited:

Users who are viewing this thread

Top