{"id":154,"date":"2007-12-18T21:14:37","date_gmt":"2007-12-18T20:14:37","guid":{"rendered":"http:\/\/www.nobugs.org\/blog\/archives\/2007\/12\/18\/154\/"},"modified":"2007-12-18T22:02:41","modified_gmt":"2007-12-18T21:02:41","slug":"154","status":"publish","type":"post","link":"https:\/\/www.nobugs.org\/blog\/archives\/2007\/12\/18\/154\/","title":{"rendered":"Unix tools, and how I use them"},"content":{"rendered":"<p>I love the unix philosophy: carry around a small set of tools and use them to build bigger custom tools to solve problems.  Over the last few years, I&#8217;ve the following programs to my &#8216;must have&#8217; list:<\/p>\n<h3>strace &#8211; trace system calls<\/h3>\n<p><a title=\"Strace\" href=\"http:\/\/en.wikipedia.org\/wiki\/Strace\">Strace<\/a> tells you which system calls a process is making.  It gives loads of information about errant processes &#8211; is it blocked on network or file i\/o?  Is it stuck in a loop?  I used this recently to find out why ssh logins were slow on one of my machines.  I used &#8216;ps ax | grep sshd&#8217; to find the pid of sshd, then ran &#8220;sudo strace -f -t -p PID&#8221;.  The &#8216;-f&#8217; means to also trace any child processes, and &#8216;-t&#8217; gives timestamps.  This showed that sshd was doing a reverse DNS lookup when I logged in (wrongly set up dns) and also that the default ubuntu .bashrc takes a good while to run.<\/p>\n<h3>lsof &#8211; list open files<\/h3>\n<p><a title=\"lsof\" href=\"http:\/\/en.wikipedia.org\/wiki\/Lsof\">lsof<\/a> is useful in conjunction with strace; strace will show you that a process is reading on file descriptor 7, but what is that used for?  Running &#8220;lsof -p PID&#8221; will tell you what each file descriptor is connected to.<\/p>\n<h3>cstream &#8211; filter, monitor and bandwidth-limit stream<\/h3>\n<p><a title=\"cstream\" href=\"http:\/\/www.cons.org\/cracauer\/cstream.html\">cstream<\/a> is great for monitoring long running jobs.  I use this often to monitor the progress of mysql imports from mysqldump files.  Eg. &#8220;cat dump.sql | cstream -l -T1 | mysql DATABASE&#8221; lets me know how much of the file has been processed so far.<\/p>\n<p>You can also use cstream to bandwidth-limit a stream, but I tend to do my bandwidth limiting via rsync (&#8211;bwlimit) or scp (-l).<\/p>\n<h3>socat &#8211; like netcat, but better<\/h3>\n<p><a title=\"socat\" href=\"http:\/\/www.dest-unreach.org\/socat\/\">Socat<\/a> is netcat for network, processes, files, sockets, etc, etc.  It also doesn&#8217;t buffer output in the same annoying way that netcat does, which makes it more useful for creating mock servers.  For example, I recently used it to create a dummy HTTP server for <a title=\"testing erlang's inets\" href=\"http:\/\/www.erlang.org\/pipermail\/erlang-bugs\/2007-November\/000504.html\">testing erlang&#8217;s inets<\/a> library:<\/p>\n<p>Create a script called &#8220;reply-204&#8221; containing<\/p>\n<pre>#!\/bin\/bash\r\nsleep 1\r\necho -ne 'HTTP\/1.1 204 No Content\\r\\nSomeHeader: foornrn'\r\nsleep 100<\/pre>\n<p>.. then run &#8220;socat tcp-listen:9999,reuseaddr exec:.\/reply-204&#8221;.<\/p>\n<h3>watch &#8211; run a command repeatedly<\/h3>\n<p>I used to write this loop lots: &#8220;while true; do ls -l somefile; sleep 1; done&#8221;.  Now I just use <a title=\"watch\" href=\"http:\/\/en.wikipedia.org\/wiki\/Watch_(Unix)\">watch<\/a>, for example &#8220;watch -n1 &#8216;ls -l somefile'&#8221;.  The &#8220;-d&#8221; flag is also useful &#8211; it highlights difference between each run.<\/p>\n<p>Commands which run other commands are the happiest commands in the world.<\/p>\n<h3>iftop &#8211; what traffic is going where?<\/h3>\n<p><a title=\"iftop\" href=\"http:\/\/www.ex-parrot.com\/~pdw\/iftop\/\">iftop<\/a> is like top, but for network traffic.  Great for getting a quick overview of why your network connection has suddenly slowed to a crawl.  Also good for noticing weird connections (aka, why is my machine sending traffic there?).<\/p>\n<h3>tcpflow<\/h3>\n<p>Like tcpdump, <a title=\"tcpflow\" href=\"http:\/\/www.circlemud.org\/~jelson\/software\/tcpflow\/\">tcpflow<\/a> captures network packets.  Additionally, it stores each &#8220;conversation&#8221; in a seperate file which makes it easy to futher analyze.<\/p>\n<p>Whilst running tcpflow a minute ago, my browser happened to request <a title=\"this page\" href=\"http:\/\/maps.amung.us\/\">this page<\/a> and tcpflow let me see that it returns this header: &#8220;Server: Modified Atari-ST&#8221;.  Do you think it&#8217;s true?<\/p>\n<h3>iperf &#8211; how fast can my network go?<\/h3>\n<p><a title=\"iperf\" href=\"http:\/\/dast.nlanr.net\/Projects\/Iperf\/\">iperf<\/a> is a simple end-to-end network performance tool.  Answers the question: What&#8217;s the maximum transfer rate between two machines?  I recently moved all my photos and videos onto a separate media server box, and loading up big jpegs was taking a few seconds.  I used iperf to check my actual network speed, but sadly the performance was pretty close to the theoretical maximum .. sadly, moving lots of bits still takes a while!<\/p>\n<h3>Not forgetting<\/h3>\n<ul>\n<li>My favourite grep flags: &#8220;-o&#8221; to only show matching text, and &#8220;-P&#8221; to get perl regexps (eg. non-greedy quantifiers)<\/li>\n<li>My favourite cat flags: &#8220;-T&#8221; to show tabs as &#8220;^I&#8221; &#8230; useful for eyeballing tab-separated files<\/li>\n<li>My favourite less flags: &#8220;-S&#8221; makes long lines get truncated, rather than wrapping.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I love the unix philosophy: carry around a small set of tools and use them to build bigger custom tools to solve problems. Over the last few years, I&#8217;ve the following programs to my &#8216;must have&#8217; list: strace &#8211; trace system calls Strace tells you which system calls a process is making. It gives loads [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-154","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/comments?post=154"}],"version-history":[{"count":0,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}