{"id":3695,"date":"2010-12-13T10:53:30","date_gmt":"2010-12-13T15:53:30","guid":{"rendered":"http:\/\/g33kinfo.com\/info\/?p=3695"},"modified":"2010-12-13T10:53:30","modified_gmt":"2010-12-13T15:53:30","slug":"poor-mans-processes-monitor","status":"publish","type":"post","link":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/","title":{"rendered":"Poor man\u2019s processes monitor"},"content":{"rendered":"<p>From <a href=\"http:\/\/andreinc.net\/2010\/11\/07\/poor-mans-processes-monitor-bash-awk-script\/\" target=\"_blank\" rel=\"noopener noreferrer\">andreinc.net<\/a><\/p>\n<p>Recently a member of the Romanian Ubuntu Community asked for a script to monitor the running processes on his server . He didn\u2019t requested for anything fancy, just a small utility that will be able to detect a hanging application (a process that is \u201ceating\u201d more than 80% of the CPU for a long period of time) and then log the results. <\/p>\n<p>I am no sysadmin, but I am sure there are a lot of dedicated open-source solutions for monitoring a server .Still the functionality he asked for can be easily achieved by combining bash and awk . One of the things I like Linux for is the power of the shell and the full-control over the your system . You can write a script for every repeating task that arise as bash is easy to learn but of course, hard to master .  <\/p>\n<p>More as an exercise for myself I\u2019ve proposed the following solution :<\/p>\n<p><code><br \/>\n#!\/bin\/bash<\/p>\n<p>#DATE: Nov 5, 2010<br \/>\n#AUTHOR: nomemory<\/p>\n<p>#Maximum memory for a process (%)<br \/>\ndeclare -i MEM_LIMIT=1<\/p>\n<p>#Maximum CPU for a process (%)<br \/>\ndeclare -i CPU_LIMIT=1<\/p>\n<p>#Loop sleep interval<br \/>\ndeclare -i SEC_INT=30<\/p>\n<p>while true; do<br \/>\nps aux | awk -v MEM_LIMIT=${MEM_LIMIT} \\<br \/>\n                -v CPU_LIMIT=${CPU_LIMIT} \\<br \/>\n                -v CDATE=\"`date`\" '{<br \/>\n                    if ($3 > CPU_LIMIT) {<br \/>\n                        printf \"%s [ %10s %d %40s ] CPU LIMIT EXCEED: %2.2f (MAX: %2.2f) \\n\", \\<br \/>\n                            CDATE, $1, $2, $11, $3, CPU_LIMIT<br \/>\n                    }<br \/>\n                    if ($4 > MEM_LIMIT) {<br \/>\n                        printf \"%s [ %10s %d %40s ] MEM LIMIT EXCEED: %2.2f (MAX: %2.2f) \\n\", \\<br \/>\n                            CDATE, $1, $2, $11, $4, MEM_LIMIT<br \/>\n                    }<br \/>\n                }'<br \/>\nsleep ${SEC_INT}<br \/>\ndone<br \/>\n<\/code><\/p>\n<p>If you run this script the output will probably look similar to this one :<br \/>\n<code><br \/>\n?<br \/>\nMon Nov  8 00:01:08 EET 2010 [     andrei 1718         \/opt\/google\/chrome\/google-chrome ] MEM LIMIT EXCEED: 2.20 (MAX: 1.00)<br \/>\nMon Nov  8 00:01:08 EET 2010 [     andrei 1726                                   pidgin ] MEM LIMIT EXCEED: 1.40 (MAX: 1.00)<br \/>\nMon Nov  8 00:01:08 EET 2010 [     andrei 1853                \/opt\/google\/chrome\/chrome ] CPU LIMIT EXCEED: 5.70 (MAX: 1.00)<br \/>\nMon Nov  8 00:01:08 EET 2010 [     andrei 1853                \/opt\/google\/chrome\/chrome ] MEM LIMIT EXCEED: 2.70 (MAX: 1.00)<br \/>\nMon Nov  8 00:01:08 EET 2010 [     andrei 2054                           gnome-terminal ] CPU LIMIT EXCEED: 1.50 (MAX: 1.00)<br \/>\nMon Nov  8 00:01:08 EET 2010 [     andrei 2058                                     bash ] CPU LIMIT EXCEED: 1.70 (MAX: 1.00)<br \/>\n<\/code><\/p>\n<p>The output can then be redirected to a file (>>) and interpreted as:<br \/>\n<code> <date> [ <owner> <pid> <command> ] MEM\/CPU LIMIT EXCEED: <cpu \/mem> (max: MAXIMUM_LIMIT for CPU\/MEM)<\/cpu><\/command><\/pid><\/owner><\/date><\/code><\/p>\n<p>In this form the script support the following variables:<\/p>\n<p>MEM_LIMIT<br \/>\nA float number (integers are accepted) representing the maximum memory percent a process can use before triggering the alarm .<br \/>\nCPU_LIMIT<br \/>\nA float number (integers are accepted) representing the maximum<br \/>\nSEC_INT<br \/>\nThe pause in the main loop . Every SEC_INT the process will be scanned<br \/>\nThose variables are passed as awk variables whilst using the \u2018 -v \u2018 flag .<\/p>\n<p>The shortcomings of the script are obvious: sometimes a process can have a short spike of CPU consumption, so false positives may appear . Probably the best thing to do will be to write another script to analyze the log, and see how many times a certain command is repeated . For example the log should be \u2018grep\u2019ed to find a certain command, then use the \u2018wc\u2019 utility the count how many times the process triggered the alarm . All in all the problem worthed a try !<\/p>\n<p>Written by: Andrei Ciobanu on November 7, 2010.<br \/>\nLast revised by: Andrei Ciobanu on November 14, 2010.<\/p>\n<p>From <a href=\"http:\/\/andreinc.net\/2010\/11\/07\/poor-mans-processes-monitor-bash-awk-script\/\" target=\"_blank\" rel=\"noopener noreferrer\">andreinc.net<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>From andreinc.net Recently a member of the Romanian Ubuntu Community asked for a script to monitor the running processes on his server . He didn\u2019t requested for anything fancy, just a small utility that will be able to detect a hanging application (a process that is \u201ceating\u201d more than 80% of the CPU for a&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/\">Read More<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-3695","post","type-post","status-publish","format-standard","hentry","category-info"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Poor man\u2019s processes monitor - Linux Shtuff<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Poor man\u2019s processes monitor - Linux Shtuff\" \/>\n<meta property=\"og:description\" content=\"From andreinc.net Recently a member of the Romanian Ubuntu Community asked for a script to monitor the running processes on his server . He didn\u2019t requested for anything fancy, just a small utility that will be able to detect a hanging application (a process that is \u201ceating\u201d more than 80% of the CPU for a... Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Shtuff\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:author\" content=\"https:\/\/fb.me\/g33kinf0\" \/>\n<meta property=\"article:published_time\" content=\"2010-12-13T15:53:30+00:00\" \/>\n<meta name=\"author\" content=\"g33kadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/drsinger1111\" \/>\n<meta name=\"twitter:site\" content=\"@drsinger1111\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/poor-mans-processes-monitor\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/poor-mans-processes-monitor\\\/\"},\"author\":{\"name\":\"g33kadmin\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"headline\":\"Poor man\u2019s processes monitor\",\"datePublished\":\"2010-12-13T15:53:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/poor-mans-processes-monitor\\\/\"},\"wordCount\":356,\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"articleSection\":[\"General Info\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/poor-mans-processes-monitor\\\/\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/poor-mans-processes-monitor\\\/\",\"name\":\"Poor man\u2019s processes monitor - Linux Shtuff\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\"},\"datePublished\":\"2010-12-13T15:53:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/poor-mans-processes-monitor\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/g33kinfo.com\\\/info\\\/poor-mans-processes-monitor\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/poor-mans-processes-monitor\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Poor man\u2019s processes monitor\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/\",\"name\":\"Linux Shtuff\",\"description\":\"Because I have CRS Syndrome...\",\"publisher\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/#\\\/schema\\\/person\\\/c022e4c40b13ea1b678e6f020756f547\",\"name\":\"g33kadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"url\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"contentUrl\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\",\"width\":512,\"height\":512,\"caption\":\"g33kadmin\"},\"logo\":{\"@id\":\"https:\\\/\\\/g33kinfo.com\\\/info\\\/wp-content\\\/uploads\\\/2022\\\/07\\\/minion-researchA.gif\"},\"description\":\"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\\\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....\",\"sameAs\":[\"https:\\\/\\\/thelinuxreport.com\",\"https:\\\/\\\/fb.me\\\/g33kinf0\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/drsinger1111\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Poor man\u2019s processes monitor - Linux Shtuff","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/","og_locale":"en_US","og_type":"article","og_title":"Poor man\u2019s processes monitor - Linux Shtuff","og_description":"From andreinc.net Recently a member of the Romanian Ubuntu Community asked for a script to monitor the running processes on his server . He didn\u2019t requested for anything fancy, just a small utility that will be able to detect a hanging application (a process that is \u201ceating\u201d more than 80% of the CPU for a... Read More","og_url":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/","og_site_name":"Linux Shtuff","article_publisher":"https:\/\/fb.me\/g33kinf0","article_author":"https:\/\/fb.me\/g33kinf0","article_published_time":"2010-12-13T15:53:30+00:00","author":"g33kadmin","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/drsinger1111","twitter_site":"@drsinger1111","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/#article","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/"},"author":{"name":"g33kadmin","@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"headline":"Poor man\u2019s processes monitor","datePublished":"2010-12-13T15:53:30+00:00","mainEntityOfPage":{"@id":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/"},"wordCount":356,"publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"articleSection":["General Info"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/","url":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/","name":"Poor man\u2019s processes monitor - Linux Shtuff","isPartOf":{"@id":"https:\/\/g33kinfo.com\/info\/#website"},"datePublished":"2010-12-13T15:53:30+00:00","breadcrumb":{"@id":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/g33kinfo.com\/info\/poor-mans-processes-monitor\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/g33kinfo.com\/info\/"},{"@type":"ListItem","position":2,"name":"Poor man\u2019s processes monitor"}]},{"@type":"WebSite","@id":"https:\/\/g33kinfo.com\/info\/#website","url":"https:\/\/g33kinfo.com\/info\/","name":"Linux Shtuff","description":"Because I have CRS Syndrome...","publisher":{"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/g33kinfo.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/g33kinfo.com\/info\/#\/schema\/person\/c022e4c40b13ea1b678e6f020756f547","name":"g33kadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","url":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","contentUrl":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif","width":512,"height":512,"caption":"g33kadmin"},"logo":{"@id":"https:\/\/g33kinfo.com\/info\/wp-content\/uploads\/2022\/07\/minion-researchA.gif"},"description":"I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com\/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....","sameAs":["https:\/\/thelinuxreport.com","https:\/\/fb.me\/g33kinf0","https:\/\/x.com\/https:\/\/twitter.com\/drsinger1111"]}]}},"_links":{"self":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/3695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/comments?post=3695"}],"version-history":[{"count":0,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/posts\/3695\/revisions"}],"wp:attachment":[{"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/media?parent=3695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/categories?post=3695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/g33kinfo.com\/info\/wp-json\/wp\/v2\/tags?post=3695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}