10

I want to show the uptime in the tmux status bar in the format DD:HH:MM, without any of the other cruft that the uptime command shows.

I've tried these:

#set -g status-left '#[fg=green]#H #[fg=black]•#[fg=green,bright]#(uptime|awk '{split(substr($0, index($0, "load")),a, ":"); print a[2]})'#[default]'
set -g status-right '#[fg=red,bg=default]#(uptime) #[fg=blue]%a%d-%m-%Y %H:%M:%S'

Neither works as I want. I want just a straight uptime of DD:HH:MM, updated every 30 seconds with the normal statusbar update.

0

2 Answers 2

9

Here you can use awk to parse the output of uptime to suit your needs like this:

set -g status-right '#[fg=red,bg=default]#(uptime | awk '{print $3}'|sed 's/,//') #[fg=blue]%a%d-%m-%Y %H:%M:%S'

Normally uptime returns this type of output:

$ uptime
 15:30:24 up  1:59,  4 users,  load average: 2.39, 2.08, 2.12

By using awk we can get rid of everything around the uptime.

$ uptime | awk '{print $3}'|sed 's/,//'
2:47

You could also use /proc/uptime to get the actual seconds that the system has been up and then use awk or perl to convert the seconds to days, hours, minutes, etc.

$ awk '{printf("%d:%02d:%02d:%02d",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}' /proc/uptime
0:02:49:55

This shows the seconds of uptime in DD:HH:MM:SS.

You could also show them using Perl:

$ cat /proc/uptime |  perl -ne '/(\d*)/ ; printf "%02d:%02d:%02d:%02d\n",int($1/86400),int(($1%86400)/36003600)/60),$1%60' 
00:02:50:53

tmux quoting?

As @JasonwRyan mentioned in the comments, tmux can be notoriously difficult to quote the commands just right. Here's how you could quote the awk example that I provided above.

set -g status-right '#[fg=red,bg=default]#(uptime | awk \"{print \\$3}\"|sed \"s/,//\") #[fg=blue]%a%d-%m-%Y %H:%M:%S'

However it's probably easier to just put the commands in a shell script and call that from within the tmux config file:

set -g status-right '#[fg=red,bg=default]#(somecommand.bash) #[fg=blue]%a%d-%m-%Y %H:%M:%S'

References

11
  • You're cutting the current time :) This should do it: uptime| cut -d" " -f4-|cut -d, -f1. It's not always DD:HH:MM though, at least on my system. Commented Jul 17, 2013 at 20:00
  • Once uptime is more than one day the awk fields will change...
    – jasonwryan
    Commented Jul 17, 2013 at 20:12
  • @PauloAlmeida - thanks for the feedback. I completely missed that 8-).
    – slm
    Commented Jul 17, 2013 at 20:13
  • @PauloAlmeida - I've modified my approach so that it deals with uptime using awk and sed. Your approach would work too. I've also adapted my answer to include a solution using /proc/uptime instead.
    – slm
    Commented Jul 17, 2013 at 20:23
  • 3
    @user205787 tmux is notoriously unforgiving of quotation marks in the statusbar: you can see the FAQ on escaping them or just call the awk script from an external file...
    – jasonwryan
    Commented Jul 17, 2013 at 22:01
3

An alternative is to use uptime -p:

#(uptime -p)

Sample output:

up 4 hours, 17 minutes

I know that's not exactly what you wanted but close

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .