Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I have a bunch of terminals open in my WM it would be nice if terminal window titles reflected current user and host (easy) along with dynamic server load details, updated at some interval (hard-er).

Using PROMPT_COMMAND I can get the desired data into terminal window title; however, it requires my doing something (changing directories for example) for the window title to update with latest information. Ideally, like many, I would prefer to do nothing.

So, is there some kind of set timeout interval-y magic I can apply to get the window title dynamically updated without user interaction?

function curr_load {
  # interval wrapper here, or somewhere at any rate ;-)
  uptime
}
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME} [`curr_load`]\007"'

Thanks for clues

share|improve this question

2 Answers 2

up vote 2 down vote accepted

The only way I'd know of would be a background process. You could either start this automatically with .bash_profile or similar (but you would probably want to check you were in an xterm and also this will cause load as every window will display it), or start it once manually when you login.

So for example using your code above, create a file in the PATH called printload set to exec containing:

#!/bin/bash
function curr_load {
  # interval wrapper here, or somewhere at any rate ;-)
  uptime
}
while true; 
do 
   echo -ne "\033]0;${USER}@${HOSTNAME} [`curr_load`]\007"
   sleep 3
done

And then just start printload with:

printload &

Which you can of course put itself into a script to be called automatically or manually.

** NOTE: Not tested for syntax but should work in theory **

share|improve this answer
    
+1 interesting, now, how about simply grep'ing top command for load line in batch mode + delay time in the curr_load function? That would seem a simpler and less resource intensive approach –  virtualeyes Mar 7 '13 at 1:02
    
I'd have thought uptime would be less intensive than something like top -b -n 1 | grep "top -" and also that has some other info in it I think? But any way you generate what to display the method (background loop) is the easiest way I can think of to do a periodic update. –  Dave C Mar 7 '13 at 1:11
    
right, uptime displays a limited set of information, and the loop idea is a good one in principle. Was just reading up on top's batch mode and thought that combining the trim flag -i (to only display summary) and the interval -d flag to output every, say, 30 seconds, would be a non-issue resource wise. I'll try out your approach, whatever gets the job done, none of this is going to bring the server to a grinding halt ;-) –  virtualeyes Mar 7 '13 at 1:19
    
Of course it all depends what information you want. Running with the trim flag on top still returns multiple lines of data, the first of which just seems to have the same information as uptime but will still need to be grepped. Of course if you want to get multiple information from different lines then combine then top would be great. Either way as you say nothing should bring the world grinding to a halt. Good luck. –  Dave C Mar 7 '13 at 1:32
    
Please edit your answer and I'll check it off -- as is, your runlevel appears to be an infinite loop, I think you meant to have "printload" in place of "runload" in this line: "runload 2>&1 > /dev/null &" in the runload file. Also, redirecting standard out and err to /dev/null prevents terminal title from displaying. It seems that with the shell escape sequences "\033]0;" and "\007" there is no output from echo, or at least not any that can be piped. For example, "./runload > foo.txt" produces nothing but an empty file, while "./runload &" generates dynamic terminal title as desired. –  virtualeyes Mar 8 '13 at 14:54

Here's what I wound up doing, using @Dave C's background task approach with 2 executable bash scripts on the remote server, term-status and term-status-run.

#!/bin/bash
function curr_load { cat /proc/loadavg | awk '{print $1 " " $2 " " $3}' }
function mem_free {
  cat /proc/meminfo | grep MemFree|awk '{print sprintf("%.0f", $2/1024)" MB"}'    
}
while true; 
do
  echo -ne "\033]0;${USER}@${HOSTNAME} `mem_free` [`curr_load`]\007"
  sleep 5
done

******
#!/bin/bash
~/scripts/./term-status &

With the files in place on remote server I modify existing bash aliases to ssh into target remote machines, appending command to run on login like so:
alias goapache='ssh -i ${key} user@some-ip -t ~/scripts/./term-status-run; bash --login'

In my Linux window manager (i3) stacked terminals connected to remote server now have titles like:

foo@serv1 226MB [0.43 0.50 0.35]
bar@serv2 332MB [0.23 0.38 0.27]
baz@serv3 295MB [0.15 0.22 0.26]

reflecting free memory and load averages at 5 second intervals.

Does the trick, kind of nice to see server status in terminal title bar, not to mention being far easier to distinguish local terminals from remote.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.