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.

boudiccas
  • 393
  • 1
  • 4
  • 12

2 Answers2

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

slm
  • 363,520
  • 117
  • 767
  • 871
  • 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. – Paulo Almeida Jul 17 '13 at 20:00
  • Once uptime is more than one day the `awk` fields will change... – jasonwryan Jul 17 '13 at 20:12
  • @PauloAlmeida - thanks for the feedback. I completely missed that 8-). – slm Jul 17 '13 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 Jul 17 '13 at 20:23
  • @jasonwryan - thanks for pointing that out. Do you see any issues with using `/proc/uptime` as I've described? – slm Jul 17 '13 at 20:24
  • A day increase would break my version too. – Paulo Almeida Jul 17 '13 at 20:36
  • No: that seems like a good approach (especially if it works internally). – jasonwryan Jul 17 '13 at 21:02
  • @slm I can see at least one drawback with `/proc/uptime`, it's Linux only, at least none of my FreeBSD and SunOS boxes have it. Parsing `uptime`s output will probably not be any more portable, though. – Marco Jul 17 '13 at 21:20
  • I've tried several of these solutions, and they work on the command-line but they don't work in the tmux conf file, and that's where I need them to work. Sorry folks to rain on your parade. – boudiccas Jul 17 '13 at 21:58
  • 3
    @user205787 `tmux` is notoriously unforgiving of quotation marks in the statusbar: you can see the [FAQ on escaping them](http://sourceforge.net/p/tmux/tmux-code/ci/master/tree/FAQ) or just call the `awk` script from an external file... – jasonwryan Jul 17 '13 at 22:01
  • Thanks jasonwryan, I've now got it working from a bash script, and its showing the correct uptime. Lovely job, thank you. – boudiccas Jul 17 '13 at 22:18
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

techraf
  • 5,831
  • 10
  • 33
  • 51