This post is not only SSH related, but it can be very nicely utilized while working with the local terminal. It is good when you're on Windows host with Putty and don't want to create enormous amount of windows. Also this is very useful, if you don't want to keep your ssh session open while a big chunk of job is being executed.
Here is a picture of my screen session:
Some points to pay attention to:
The very last line belongs to screen. It says I have 5 sessions (or tabs, or windows, pick your name).
users:1(5) | 1 user with 5 open sessions |
uptime:2.0 | 2 days of uptime |
mem:387 | megabytes free |
cpu:18.17 | cpu load |
Mon, 08/30 | date and time (truncated because of the size of the window) |
All what you see on the last line is configurable and can be set to show different values. Say, for example, you might want to see your wifi status, or the CPU temperature.
All you need to do to get this is to install screen, and insert this into your ~/.screenrc
- # ~/.screenrc
- altscreen on # Return screen to original state when running
- # ANSI programs like less and vi
- # turn off startup message
- startup_message off
- # detach on hangup
- autodetach on
- # emulate .logout message
- pow_detach_msg "Screen session of \$LOGNAME \$:cr:\$:nl:ended."
- # Add stuff to xterm (and cousins) title bars. This is a moderate abuse of the
- # hardstatus feature--it just puts the hardstatus stuff into an xterm title
- # bar.
- setenv DISPLAY :0
- setenv TERM screen-256color
- backtick 1 10 0 /home/sealemar/etc/bin/screen/free.sh mem
- backtick 2 10 0 /home/sealemar/etc/bin/screen/free.sh swap
- backtick 3 7 7 /home/sealemar/etc/bin/screen/cpu_load.py
- backtick 4 300 0 /home/sealemar/etc/bin/screen/uptime.sh
- backtick 5 60 0 /home/sealemar/etc/bin/screen/date_time.sh
- backtick 6 15 0 /home/sealemar/etc/bin/screen/users_logged_in.sh
- #backtick 4 15 15 /home/sealemar/etc/bin/battery_life.pl --nocolor
- #backtick 4 30 30 /home/sealemar/etc/bin/screen/wifi.sh
- # Set the caption on the bottom line
- caption always
- #caption string "%{= kw}%-w%{= BW}%n %t%{-}%+w %-= wifi:%4` mem:%1` swap:%2` cpu:%3` - %LD %d %LM %Y - %c"
- caption string "%{= kw}%-w%{= BW}%n %t%{-}%+w %-= users:%6` uptime:%4`d mem:%1` swap:%2` cpu:%3` - %5`"
- # use xterm scrollback mechanism
- info xterm* ti@:te@
- # 256 color xterm
- attrcolor b ".I" # allow bold colors - necessary for some reason
- # don't use visual bell
- vbell off
- # replace ctrl-A by ctrl-O
- escape ^Oo
- # set a big scrolling buffer
- defscrollback 20000
- hardcopydir ~/.cache/screen
- # set how screen will highlight the text
- sorendition =r
- # fix the residual text
- #altscreen on
- bind ' ' windowlist -b
- # resizing
- bind = resize =
- bind + resize +1
- bind - resize -1
- bind _ resize max
- bind ^A eval "split" "focus" "next" # ^O-^A to split the window into a new region
- bind ^X remove # ^O-^X to remove the current region
- bindkey -k k5 focus # F5 to cycle through split regions
- bindkey -k k6 prev # F6 for previous window
- bindkey -k k7 next # F7 for next window
- bindkey -k k8 copy # F8 to enter scroll mode
- # create some screens
- screen -t booo1 1 bash
- screen -t booo2 2 bash
- screen -t booo3 3 bash
- screen -t booo4 4 bash
- screen -t Build 5 bash
Visible stuff is configured on lines 71-76 (sessions and their names which are created when screen start up) and lines 21-32 (counters, date time, etc.) I'm not going to elaborate on what every line does. The most interesting to tune, however, is backticks. It's obvious what it does from the documentation. Also, if you have any questions, don't hesitate to ask, I will do my best to answer everything.
Line #32 outputs a caption screen where user-defined backticks appear as a number between % and `, i.e "%6`"
The command below starts screen:
screen -dRRwhich detaches screen session from wherever it was last opened from and attaches to it. If no screen session has been opened, a new one is created. Check man screen for a list of options, but that one above is the best I've found so far.
Here is a list of commands and shortcuts to use with the configuration above:
Command / Shortcut | Meaning |
---|---|
screen -dRR | Detaches existing screen session and attaches to it or starts a new session |
F6 | Previous tab |
F7 | Next tab |
F8 | Select text |
<Ctrl+O><Ctrl+]> | Paste screen selection |
<Ctrl+O><Ctrl+A> | Split window |
<Ctrl+O><Ctrl+X> | Close split window |
F5 | Cycle through split windows |
One big plus of "ssh through screen" over "ssh without screen" is that you may start a job which takes hours to execute and then you can close your ssh client without worrying that the job will terminate and your session will end, because it won't. When you feel like you want to check how it goes, you just connect from the same place, or a different one, it doesn't matter, and attach to the existing screen session - that simple. If you are in the middle of something and you have a lot of stuff open, but you have to go right now and you don't want to close everything. It's all right, you don't have to, just close your ssh session. Next time, reattach to the screen with "screen -dRR" - done - everything is where you have left it.
Backticks
I publish all of my backticks with a right free to use, but without any warranties. References are appreciated.date_time.sh
- /usr/bin/date +"%a, %D %H:%M
free.sh
- #!/bin/bash
- case "$1" in
- "mem") free -m | grep buffers/cache | awk '{print $4}' ;;
- "swap") free -m | grep Swap | awk '{print $4}' ;;
- esac
cpu_load.py
- #!/usr/bin/env python2
- #
- # developed by Sergey Markelov (2013)
- import os, sys, time
- PIPE_WRITE_INTERVAL = 7
- PIPE_FILENAME = "/dev/shm/cpu_load.pipe"
- SINGLETON_FILE = "/dev/shm/cpu_load.py.singleton"
- def getStat():
- with open("/proc/stat", "r") as fd:
- times = fd.readline().split()[1:5]
- return([int(t) for t in times])
- def cpuLoad(interval):
- times1 = getStat()
- time.sleep(interval)
- times2 = getStat()
- for i in range(len(times1)):
- times2[i] -= times1[i]
- usage = times2[0] + times2[1] + times2[2]
- total = usage + times2[3]
- return(100 * float(usage) / total)
- def writeData():
- # ensure this is the only instance
- try:
- open(SINGLETON_FILE, "r").close()
- except IOError:
- open(SINGLETON_FILE, "w").close()
- else:
- sys.exit(2)
- try:
- while(True):
- load = cpuLoad(PIPE_WRITE_INTERVAL)
- with open(PIPE_FILENAME, "w") as fd:
- fd.write(str(load))
- except KeyboardInterrupt:
- pass
- finally:
- os.remove(PIPE_FILENAME)
- os.remove(SINGLETON_FILE)
- def readData():
- try:
- pipe = open(PIPE_FILENAME, "r")
- except IOError:
- print "N/A"
- else:
- print "%6.2f" % float(pipe.readline())
- pipe.close()
- if __name__ == "__main__":
- try:
- if(len(sys.argv) < 2 or sys.argv[1] != "write"):
- readData()
- else:
- writeData()
- except Exception as e:
- print("An unexpected exception has occured: %s" % str(e))
- sys.exit(1)
uptime.sh
- #!/bin/sh
- # prints uptime as fraction of days
- # Example:
- # 3.5
- # means uptime is 3.5 days
- /usr/bin/awk '{printf "%0.1f\n", $1 / 3600 / 24}' /proc/uptime
users_logged_in.sh
- #!/bin/sh
- # prints
- # total_users(total_sessions)
- # Example:
- # 2(5)
- # means 2 users and 5 sessions in total
- PATH=/usr/bin
- users=`w -h | awk '{print $1}' | sort | uniq | wc -l`
- sessions=`w -h | awk '{print $1}' | wc -l`
- echo "${users}(${sessions})"
No comments:
Post a Comment