Ubuntu – Can BASH display in systray as application indicator

bashscriptssystem-trayunity

I have a bash script that locks the screen after a set amount of time (Application that will lock screen after a set amount of time for Ubuntu). I would like to display the time remaining in Ubuntu's System Tray / Application Indicator bar.

Best Answer

The best method I've found is System Monitor Indicator from this article: webupd8.org - Ubuntu application indicator that displays bash. It displays text on the Unity system tray / application indicator bar that your bash script "echos".

The above article is targeted to Ubuntu 16.04 with Unity. For more information on Xubuntu, Gnome-Shell + app-indicator extension, and Budgie, go to the Developers website: fossfreedom / indicator-sysmonitor. Also visit the site for more detailed installation and configuration instructions.

Install and Configure indicator-sysmonitor

To install System Monitor Indicator you need to first specify the PPA where indicator-sysmonitor can be found:

sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor
sudo apt-get update
sudo apt-get install indicator-sysmonitor

Now run the "indicator-sysmonitor" GUI from Dash (Alt+F2).

  • Click on the systray area that appears with "cpu: 99% mem: 99%"
  • Select "Preferences"
  • The "General" tab is initially active, click the "Run on startup" box
  • Select the "Advanced" tab
  • Click New button to add a new control
  • In Sensor field enter custom
  • In Description field type Bash Indicator _ In Command field type name of your bash script, ie /mnt/e/bin/indicator-sysmonitor-display
  • Save your new custom indicator
  • Highlight the custom line and click the Add button to activate it.
  • You can remove the default variables for "CPU" and "Mem" which might not be helpful to you.
  • I changed the refresh time interval from 2 seconds to .3 second. To support "spinning pizza" explained below.
  • Now click the Save button.

Sysmonitor Indicator in action

This .gif shows how it looks when Ubuntu’s Unity System Tray is updated.

multi-timer sysmonitor indicator.gif

  • At the beginning of the animation our systray output contains "Brightness: 3000".
  • Then multi-timer (link below) is started and steps through multiple timers.
  • A spinning pizza appears along with a count down of remaining time.

NOTE: System Monitor Indicator also displays "Brightness: 3000". This is the daytime setting for my Intel Backlight hardware brightness level (link below).

Sysmonitor Indicator BASH script

Create a script similar to the following and assign it to the variable {Custom} in Sysmonitor Indicator:

#!/bin/bash

# UPDT: May 30 2018 - Cohesion with new multi-timer and old lock-screen-timer.

if [ -f ~/.lock-screen-timer-remaining ]; then
    text-spinner
    Spinner=$(cat ~/.last-text-spinner) # read last text spinner used
    String=$(cat ~/.lock-screen-timer-remaining)
    systray="$Spinner  $String"
else
    systray=""
fi

if [ -f /tmp/display-current-brightness ]; then
    Brightness=$(cat /tmp/display-current-brightness)
    systray="$systray  Brightness: $Brightness"
else
    systray="$systray  Brightness: OFF"
fi

# Below for AU answer: https://askubuntu.com/questions/1024866/is-it-possible-to-show-ip-address-on-top-bar-near-the-time
# default_interface=$(route -n | awk '$1 == "0.0.0.0" {print $8; exit}')
# ip_address=$(ifconfig "$default_interface" | awk 'sub(/.* inet addr:/, "") {print $1}')
# systray="$systray  $ip_address"
        
echo "$systray" # sysmon-indidicator will put echo string into systray for us.

exit 0

After telling Sysmonitor Indicator the name of your bash script by setting the {Custom} variable it runs every refresh interval. Whatever your bash script outputs via echo command appears in Ubuntu's System Tray.

NOTE: The script displays Time Remaining and Display Brightness Level values. These value are set by scripts documented in Ask Ubuntu: Application that will lock screen after a set amount of time for Ubuntu, A timer to set up different alarms simultaneosly and Automatically adjust display brightness based on sunrise and sunset respectively.

Spinning pizza--text-spinner BASH script

The text-spinner bash script creates a spinning pizza effect by cycling through the characters |, /, and \. This effect highlights the fact something is "working" or "thinking". To get the "spinning effect" you want to change the Sysmonitor Indicator refresh interval from the default 2 seconds to be about 0.30 seconds.

Here is the text-spinner bash script:

#!/bin/bash

# return '|', '/', '─', '\' sequentially with each call to this script.
# Use ~/.last-text-spinner to store last used

FILE=~/.last-text-spinner

if ! [ -f $FILE ]; then
    echo '|' > $FILE
    exit 124 # ASCII equivalent for '|'. Bash doesn't allow character return codes
fi

LAST=$(cat $FILE) # read last character used

if [[ $LAST == '|' ]]; then
    echo '/' > $FILE
    exit 47 # ASCII equivalent of "/"
elif [[ $LAST == '/' ]]; then  # NOTE: you must have spaces around " == " else code breaks
    echo '─' > $FILE
    exit 9472 # ASCII equivalent
elif [[ $LAST == '─' ]]; then
    echo '\' > $FILE # NOTE: must use single quote because double quote BASH reinterprets
    exit 92 # ASCII
else
    echo '|' > $FILE
    exit 124 # ASCII
fi
Related Question