Format the output of "read_file uptime" in i3status
I’m trying to display my system uptime with i3status. This is what I have in my i3status.conf
file:
read_file uptime {
format = "%title: %content"
path = "/proc/uptime"
}
However the output is in seconds, as can be seen in this screenshot, which is obviously not ideal:
Is there a way I am able to format this, to display for example in the format of:
DD:HH:MM:SS
My solution
1. Create a very very short script
Just create a new file and rename it uptime.sh
; then just copy this:
!#/bin/sh
echo "$(uptime -p)" > ~/.config/i3/uptime
this will append or write the output of the command uptime -p
to a file which I chose to be in ~/.config/i3/uptime
. The command uptime -p
shows uptime in a pretty format.
If done correctly the contents of your ~/.config/i3/uptime
should be up x hours, x minutes
.
Another command you can try is uptime -s
which will show the date and time since your system was up.
2. Create a cron job
Just execute crontab -e
in the terminal then append this line:
* * * * * /home/user/path/to/uptime.sh
this will execute the script every minute, therefore refreshing or overwriting the contents of uptime every minute. So that we can get the most recent uptime possible.
If you don’t want the script to execute or to refresh the file every minute, you can replace * * * * *
with @hourly
for just an hourly refresh of uptime
file.
3. Lastly, edit your i3status
config file
Like this:
# uptime
read_file uptime {
color_good = "#ffffff"
format = "%content"
path = "/home/user/.config/i3/uptime"
}
And that’s it. Restart i3 and hopefully i3status uptime
shows up pretty.
I made it even prettier with this script:
#!/bin/sh
uptime=$(uptime -p)
days=$(echo "$uptime" | grep -oP 'd+(?= day)')
hours=$(echo "$uptime" | grep -oP 'd+(?= hour)')
minutes=$(echo "$uptime" | grep -oP 'd+(?= minute)')
formatted_uptime=""
if [ -n "$days" ]; then
formatted_uptime="${days}d "
fi
formatted_uptime="${formatted_uptime}${hours}h ${minutes}m"
echo "$formatted_uptime" > ~/.config/i3/scripts/uptimedm