Bash

Use dsh to exec commands on many machines

2 Feb 2016

For this example, I will try to get the total memory of all my servers:

$ cat myserverlist.txt
127.168.1.24
127.168.1.25
127.168.1.26
$ dsh -r ssh -f backendstate.txt -M -- grep MemTotal /proc/meminfo
127.168.1.24: MemTotal:       35058204 kB
127.168.1.25: MemTotal:       35058204 kB
127.168.1.26: MemTotal:       35058208 kB

How to survive with vi commands in someone else's Bash session

12 Sep 2015

This will set your Bash session to use vi commands:

$ set -o vi

This will tell programs that use libreadline to use vi editing mode, without disturbing the ~/.inputrc of the native user:

$ cat "set editing-mode vi" > /somedir/vi-inputrc
$ export INPUTRC=/somedir/vi-inputrc

How to ensure your terminal is using 256 colors

12 Sep 2015

Add this to .bashrc

export TERM=xterm-256color

so that

tput colors

says 256, and that

msgcat --color=test

shows many pretty colors.

Seconds since epoch as date

15 Aug 2015

$ date --date='@1439660811'
Sat Aug 15 13:46:51 EDT 2015

Current date (now) as seconds since epoch

15 Aug 2015

$ date +%s
1439660811

Good lines to put at the top of every bash file:

# Treat uninitialized vars as a reason to exit, rather than as null
set -u

# Exit on the first non-zero return code
set -e

# Exit on the first non-zero return code of any program in a pipeline
set -o pipefail

Find the exit status of programs in a pipeline:

How to find the exit status of foo, rather than bar?

foo | bar

Here's how:

echo ${PIPESTATUS[0]}

Here's how you would get the exit status of both programs:

foo | bar
echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}  # prints foo exit status followed by bar exit status

Get the directory that your script lives in

HERE=$(dirname ${BASH_SOURCE[0]})

Set the title of your terminal from Bash

8 Aug 2015

Handy for when you have a bunch of terminals open and you want to spot them by title.

PROMPT_COMMAND="echo -ne \"\033]0;SET YOUR TITLE HERE\007\""

A nice .bashrc script

26 Aug 2015


unalias -a

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

############################# PS1 support functions ##################

# print non-zero exit
_pnze() {
    local e=$1
    if [ "$e" -ne "0" ]; then
        echo "[$e]"
    fi
}

# print git branch
_pgb() {
    txt=$(git branch 2>&1)
    local e=$?
    if [ "$e" -eq "0" ]; then
        #local b=$(echo "$txt" | grep '*' | awk '{ print $1 }')
        # echo "$b"
        # local b=$(echo "$txt" | grep '*' | cut --delimiter=' ' --fields=2)
        # echo "$b"
        local b=$(echo "$txt" | grep '*')
        echo "${b:2}"
    fi
}

# print git status
_pgs() {
    txt=$(git status --porcelain 2>&1)
    local e=$?
    if [ "$e" -eq "0" ] && [ -n "$txt" ]; then
        echo "+"
    fi
}

# print git branch and status
_pgbs() {
    local b=$(_pgb)
    local s=$(_pgs)
    if [ -n "$b" ]; then
        echo "($b$s)"
    fi
}

############################# PS1 ##################

# IMPORTANT: must capture exit status $? of previous command
# FIRST, otherwise, later commands in PS1 will overwrite $?
PS1='$(_pnze $?)\u@\h:\w$(_pgbs)\$ '
# This changes the terminal window title
PROMPT_COMMAND='echo -ne "\033]0;$(_pnze $?)${PWD/$HOME/\~}$(_pgbs)\007"'

################## bash completion #################

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

############## "aliases" ####################

h() {
    if [ -n "$1" ]; then
        history | grep "$1"
    else
        history
    fi
}

# Runs vi as vim

vi() {
    vim "$@"
}


# Sets the terminal title

set-prompt() {
    PROMPT_COMMAND="echo -ne \"\033]0;${1:-TITLE NOT SET}\007\""
}

######## ls "aliases" ####################

# see http://blog.twistedcode.org/2008/04/lscolors-explained.html
export LS_COLORS='di=34:fi=0:ln=35:pi=31:so=31:bd=31:cd=31:or=41:mi=41:ex=32'

# don't forget 'command' and 'builtin'
# to get a list of builtins, run 'enable'

ls() {
    command ls --color --file-type "$@"
}

C() {
    command ls --classify "$@"
}

c() {
    command ls --color "$@"
}

L() {
    command ls --color --file-type -ltrh "$@"
}

############## HOMES #####################

export GROOVY_HOME=/usr/local/groovy-2.2.1
export JAVA_HOME=/usr/local/jdk1.7.0_45
export GRADLE_HOME=/usr/local/gradle-1.10
export ANT_HOME=/usr/local/apache-ant-1.9.3

############## PATH #####################

export PATH=/usr/local/pgsql-9.3/bin:$PATH
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$GRADLE_HOME/bin:$PATH
export PATH=$GROOVY_HOME/bin:$PATH
export PATH=$ANT_HOME/bin:$PATH

Ideal flags for strace

17 Oct 2015

strace -p 32670 -f -s 80
-p
pid
-f
follow what child procs and threads are doing
-s 80
show 80 chars of output instead of 32

Find which process is most likely to get killed by the OOM killer

17 Oct 2015

$ dstat --top-oom

How to find out which process was killed by the OOM killer

17 Oct 2015

$ grep -i 'killed process' /var/log/syslog

or

# dmesg -T | grep -i "Out of"

Sample feedback from above command:

[Wed Jan 27 17:26:39 2016] Out of memory: Kill process 41475 (java) score 169 or sacrifice child

Monitor what one particular process is doing

14 Nov 2015

### one process:
$ top -p <pid>
### a handful of processes:
$ top -p <pid>,<pid>,<pid>