tmux just saved my bacon

As an administrator of systems, I live in the console, or the command-line, or as I call it “the shell”.

Many moons ago, I would open many connections in tabs between my workstation and a host. This allowed me to run many commands in parallel. Over time, I learn about SSH multiplexing which is some serious nerdy hotness. That, in a nutshell, had all of those session running on one SSH connection. Bad ass.

I was then put on to terminal multiplexing. In my case, this was tmux. There are many others, ‘screen’ being the venerable old bastard we’ve all heard of.

Over time, I learned to live in tmux. I have one connection to a host and tmux running on the far end. If I live in the host for more than a few minutes, tmux gets installed and up goes my tmux.conf.

Such was life and such is my life. And then something amazing happened. A few moments ago, I was mucking with saltstack and iptables on a VPS host. I kicked off the highstate and blammo! my connection dropped. FML. The new iptables rules applied and I just locked myself out of the host. Except, I didn’t. I was running the highstate in tmux, so while my ssh connection dropped, tmux finished the highstate and re-added the ssh rule to iptables.

ssh {hostname}

and I was back on the machine.

Thank you tmux. You just saved my bacon.

Which command do I use the most?

After waking to a ringing phone, I was attempting to fall back asleep. As I drifted, I wondered which command I used the most? I thought it a neat exercise, so I found out.

As I type this, I suspect it is “ls”. Which is second and third should be interesting.

I gathered up .zsh_history and .bash_history files from 5 different machines I work on a fair bit and put them in a pile, erm, directory.

 $ cat bash_history? > big_bash_history
 $ wc -l big_bash_history              
 2897 big_bash_history
 $ sed -e 's/^.*;//g' zsh_history? >big_zsh_history
 $ wc -l big_zsh_history
 31188 big_zsh_history

Wow! 35 thousand commands. Wait… There’s a snafu in there. SUDO. Hmm….

$ awk '{print $1}' big_zsh_history|grep sudo |wc -l
$ awk '{print $1}' big_bash_history|grep sudo |wc -l

Okay. I’ve got some numbers around that…Let’s see about stripping those and get the underlying command.

$ sed -r -e '/sudo -i/d' -e 's/sudo //g' big_bash_history big_zsh_history|awk '{print $1}'|while read CMD;do basename "$CMD"; done |sort |uniq -c |sort -rn |head -20
   4571 cd
   3845 ls
   2884 vi
   2436 git
   1719 ssh
   1195 cat
   1032 grep
    928 rm
    797 ansible-playbook
    659 mv
    619 pass
    619 cp
    443 python
    438 scp
    394 apt-get
    373 salt
    372 mkdir
    339 more
    297 find
    291 done

Okay. No surprises in the top five. ‘ansible-playbook’ surprised me. Didn’t realize I entered that so much. There were more points of interest after the first twenty, but I won’t post those.

Interesting ‘done’ showed up in the top twenty. Thinking about this, it makes sense, as the first command is usually input to while, ie:

 ls | while read FILE; do echo $FILE; done

No ‘if’ or ‘fi’ in the top twenty. How about that.

Actually, thinking about this more, there are probably many more missing commands that were piped into another or, say ‘rm’ used in an ‘exec’ for ‘find’.