The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 2,172     Votes:  6     ✅ Solution
Tags: unity   bash   scripts   system-tray   eyesome   multi-timer  
Link: 🔍 See Original Answer on Ask Ubuntu 🔗

URL: https://askubuntu.com/q/882420
Title: Can BASH display in systray as application indicator?
ID: /2017/02/11/Can-BASH-display-in-systray-as-application-indicator_
Created: February 11, 2017    Edited:  December 12, 2021
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  true    Copy to clipboard:  false


Skip

System Monitor Indicator

The best method I’ve found is Sysmonitor Indicator from this article on the WEB UPD8 website:

It displays text on the Ubuntu System Tray (Systray) / Application Indicator Bar that your bash script updates with a single echo command.

Top ToS Skip

Different Desktop Environments

The above article is targeted to Ubuntu 14.04 to 20.04 with the Unity Desktop. If you don’t have Unity Desktop installed for Ubuntu 20.04 see these instructions.

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 Unity Dash (Alt+F2 or ⊞ Super aka ⊞ Win key). If you are using GNOME use ⊞ Super+A to open Show Applications instead of Dash.

Top ToS Skip

Sysmonitor Indicator in action

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

multi-timer sysmonitor indicator.gif

Top ToS Skip

Sysmonitor Indicator BASH script

Create a script similar to the following script called indicator-sysmonitor-display. Assign the script’s filename to the variable {Custom} in Sysmonitor Indicator:

#!/bin/bash

# UPDT: May 30 2018 - Cohesion with new multi-timer and old lock-screen-timer.
#       July 6 2020 - New eyesome sunlight percentage.

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 /usr/local/bin/.eyesome-percent ]; then
    Brightness=$(cat /usr/local/bin/.eyesome-percent)
    systray="$systray  eyesome: $Brightness"
else
    systray="$systray  eyesome: 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.

Top ToS Skip

Three bash scripts that output to Systray

The indicator-sysmonitor-display script displays Time Remaining and Display Brightness Level values. These values are set by other scripts documented within Ask Ubuntu:

These three bash scripts illustrate how multiple scripts can output to the Systray concurrently and share the same script file (indicator-sysmonitor-display) that updates the display.

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

Top ToS
⇧ Considerations for using `/tmp` directory on multi-user systems How to display something in unity systray from a bash script  ⇩