{"id":64,"date":"2009-09-28T15:06:30","date_gmt":"2009-09-28T22:06:30","guid":{"rendered":"http:\/\/www.imaginarybillboards.com\/?p=64"},"modified":"2010-01-27T19:09:43","modified_gmt":"2010-01-28T02:09:43","slug":"building-useful-command-lines-step-by-step","status":"publish","type":"post","link":"http:\/\/www.imaginarybillboards.com\/?p=64","title":{"rendered":"Building useful command lines, step by step"},"content":{"rendered":"

1- I tend to have tons of things running at a time and want to watch out of a corner of my eye. \u00c2\u00a0Since I’m doing some pretty heavy stat stuff that can end up killing a machine’s memory in a short amount of time, I want to watch that too.<\/p>\n

Enter ‘watch’. \u00c2\u00a0Here’s how it goes – watch -n<number of seconds to wait before refreshing> <command to do this to><\/p>\n

And here’s my big watch command (which I’ve aliased to watcher). \u00c2\u00a0In this I’m looking at perl and python processes, the amount of free memory and the cpu usage, and any mysql processes going on.
\n
\nwatch -n1 ' ps aux | egrep \"(perl|python)\" | grep -v grep;uptime;free -m; echo \"show processlist\" | mysql -uroot | grep -v Sleep'
\n<\/code>
\nThis shows details on any perl or python processes (without the grep command itself), the load, the memory state, and any non-sleeping mysql processes. \u00c2\u00a0Every second. \u00c2\u00a0 I just keep it up in a corner of the monitor.<\/p>\n

2. Semi-related – killing a bunch of mysql processes.<\/p>\n

Sometimes I’ll fire up a bunch of processes and decide they should go away. \u00c2\u00a0Easy – killall perl<\/code> works wonders. \u00c2\u00a0But the mysql processes remain – enter another one:
\n
\necho \"show processlist\" | mysql |egrep -v \"(Id|processlist)\" | awk {'print \"kill \"$1\";\"'} | mysql
\n<\/code>
\nWhat it does: \u00c2\u00a0'echo \"show proesslist\" '<\/code> just echoes “show processlist” to standard output. \u00c2\u00a0That is then piped to the input of mysql.
\n
\n#echo \"show processlist\" | mysql
\nId User Host db Command Time State Info
\n455093 root localhost NULL Query 0 NULL show processlist
\n<\/code>
\nNext, it reverse-greps (as I like to call it) for ‘Id’ because I don’t want to see that.
\n
\n# echo \"show processlist\" | mysql |grep -v Id
\n455095 root localhost NULL Query 0 NULL show processlist
\n<\/code>
\nNext, it inputs it into a very short awk program – what this does is split it up by spaces, and set each of those to a $<number> variable. \u00c2\u00a0So we print \"kill $1;\"<\/code> to standard out – that’ll be the command we want to send to mysql to kill them all. So we end up with:
\n
\n# echo \"show processlist\" | mysql |grep -v Id| awk {'print \"kill \"$1\";\"'}
\nkill 455098;
\n<\/code>
\nFinally, pipe that into mysql, like so:
\n
\n# echo \"show processlist\" | mysql |grep -v Id| awk {'print \"kill \"$1\";\"'} | mysql
\nERROR 1094 (HY000) at line 1: Unknown thread id: 455101
\n<\/code>
\nWhat happened there? \u00c2\u00a0Well, since I’ve been killing threads for a good two minutes now while working on this shortcut the only thread left is the “show processlist” thread – which ends as soon as the processlist is shown. \u00c2\u00a0Which makes sense. \u00c2\u00a0So cheat and either add another grep -v<\/code> to get rid of it or \u00c2\u00a0egrep with a simple regex: egrep -v \"(Id|processlist)\"<\/code>
\n
\n#echo \"show processlist\" | mysql |egrep -v \"(Id|processlist)\" | awk {'print \"kill \"$1\";\"'} | mysql
\n#
\n<\/code>
\nYes, both of these could be done in better ways! \u00c2\u00a0 But in my defense, part of doing these things is to share them with others and help them be better. \u00c2\u00a0Just like using simple perl is better a lot of the time than really complex code, simple things that are easy to handle are easier to show others and let them modify. \u00c2\u00a0Plus, this is actually how I go about helping someone solve one of these problems so they can do it themselves in the future.<\/p>\n

Oh, and now I can just send this link…<\/p>\n","protected":false},"excerpt":{"rendered":"

1- I tend to have tons of things running at a time and want to watch out of a corner of my eye. \u00c2\u00a0Since I’m doing some pretty heavy stat stuff that can end up killing a machine’s memory in a short amount of time, I want to watch that too. Enter ‘watch’. \u00c2\u00a0Here’s how […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[10,19],"_links":{"self":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts\/64"}],"collection":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":5,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":68,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions\/68"}],"wp:attachment":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}