{"id":93,"date":"2010-10-10T23:10:53","date_gmt":"2010-10-11T04:10:53","guid":{"rendered":"http:\/\/soljerome.com\/blog\/?p=93"},"modified":"2010-10-15T17:51:24","modified_gmt":"2010-10-15T22:51:24","slug":"typing-less-with-xargs","status":"publish","type":"post","link":"https:\/\/www.soljerome.com\/blog\/2010\/10\/10\/typing-less-with-xargs\/","title":{"rendered":"Typing less with xargs"},"content":{"rendered":"<p>The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Xargs\" target=\"_blank\">xargs<\/a> page on wikipedia talks mostly about the benefits of using it when running tasks on large lists. However, I have found it to be useful in other situations as well.<\/p>\n<p>Oftentimes I am tasked with suspending accounts at work due to CPU (over)use. During this process, I frequently come across accounts which are processing Awstats statistics for some excessive number of domains (as this is the default setting in CPanel). This is normally because the number of domains for a particular user is something &gt; 1500. In order to kill off the statistics processing, I usually just find and kill all the processes running for that user. I used to use the following to do that:<\/p>\n<p><code>for p in `ps aux | grep ^user | awk '{print $2}'`; do kill $p; done<br \/>\n<\/code><\/p>\n<p>This is actually pretty simple and works quite well. However, it normally requires use of the Home\/End keys along with &#8216;`&#8217; because it&#8217;s usually after I get the process listing that I realize awstats is running. This is where xargs comes in. Usually the command typed just before the above is this:<\/p>\n<p><code>ps aux | grep ^user<\/code><\/p>\n<p>From there, it would be nice to awk out the process ids and pipe them to kill. Here is some sample output of what I&#8217;m talking about:<\/p>\n<p><code>$ ps aux | grep ^solj | awk '{print $2}'<br \/>\n2190<br \/>\n2205<br \/>\n2237<br \/>\n2240<br \/>\n<\/code><\/p>\n<p>So, the result is simply a newline-delimited listing of the various process ids running for a particular user. Note that because I am on Linux, awk is actually <a href=\"http:\/\/www.gnu.org\/software\/gawk\/\" target=\"_blank\">gawk<\/a> so I don&#8217;t have to worry about the newline separation being an issue. Therefore, I finally came up with the following elegant solution to my problem:<\/p>\n<p><code>ps aux | grep ^solj | awk '{print $2}' | xargs kill<br \/>\n<\/code><\/p>\n<p>As you can see, this makes it extremely simple to go from the previous command to the one needed to kill off all the user&#8217;s processes. No special characters modifications at the beginning of the line are necessary. While this may seem like a miniscule amount of time saved, it actually adds up quite a bit with the number of suspensions made in this manner :-). I look forward to using xargs for inode abuse as well, but we&#8217;ll save that for another post&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The xargs page on wikipedia talks mostly about the benefits of using it when running tasks on large lists. However, I have found it to be useful in other situations as well. Oftentimes I am tasked with suspending accounts at work due to CPU (over)use. During this process, I frequently come across accounts which are <a href='https:\/\/www.soljerome.com\/blog\/2010\/10\/10\/typing-less-with-xargs\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts\/93"}],"collection":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":4,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts\/93\/revisions\/98"}],"wp:attachment":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}