{"id":491,"date":"2012-12-04T16:30:25","date_gmt":"2012-12-04T21:30:25","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2012\/12\/04\/create-server-load-monitoring-bash-script\/"},"modified":"2021-08-16T23:27:34","modified_gmt":"2021-08-17T03:27:34","slug":"create-server-load-monitoring-bash-script","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/","title":{"rendered":"Create server load monitoring bash script"},"content":{"rendered":"<p>In this article we&#8217;ll discuss how you can setup a small <strong>server load monitoring bash script<\/strong> with a scheduled cron job to monitor your server&#8217;s load average, and e-mail you a notice if it&#8217;s above the limit that you&#8217;ve set.<\/p>\n<p>A lot of times your server will probably be running at a load much lower than it can actually handle. Although at certain times your server&#8217;s usage might suddenly spike leading to the server possibly going unstable and it could potentially need a reboot to recover from.<\/p>\n<p>Our system administration team has monitors setup on all of our dedicated servers for extreme load issues. However you might be interested in setting up some monitoring of your own so that you can be alerted via e-mail as soon as your server is spiking so that you can login to investigate that usage spike more closely.<\/p>\n<p class=\"alert\">This would primarily be used for a dedicated server. While it could also be used on a VPS the load average of a VPS typically shouldn&#8217;t exceed a 1.00 and it can temporarily fluctuate up and down as it&#8217;s a virtualized environment. So you might end up getting too many notices and by the time you get in to investigate it could have dropped back down.<\/p>\n<h2>Create bash load monitoring script<\/h2>\n<ol class=\"article_list\">\n<li>First you&#8217;ll want to figure out how many CPU cores you have on your dedicated server as this determines the optimal load your server can operate at.\n<p style=\"margin: -5px;\">\u00c2&nbsp;<\/p>\n<p>You can <a href=\"\/support\/server\/ssh\/how-to-login-ssh\/\">login to your server via SSH<\/a> and then run the following command to find this out:<\/p>\n<p class=\"cli\" style=\"width: 155px;\">grep pro \/proc\/cpuinfo -c<\/p>\n<p>You should get back the number of cores on your server such as <span class=\"cli\">\u00c2&nbsp;4\u00c2&nbsp;<\/span>.<\/li>\n<li>You won&#8217;t want your load average spiking much over how many CPU cores your server has, in this case I know this particular server isn&#8217;t doing anything other than serving up one static website, so if the load gets even up to a 4.00 I&#8217;ll want to investigate it. So I&#8217;m going to set my trigger value for load at a 4.00.\n<p style=\"margin: -5px;\">\u00c2&nbsp;<\/p>\n<p>Start editing a new file to create your bash alert script, in this case I&#8217;m using the <b>vim<\/b> text-editor and making a new file called <b>loadMon<\/b> in my user&#8217;s home directory with the following command:<\/p>\n<p class=\"cli\" style=\"width: 250px;\">vim \/home\/userna1\/loadMon<\/p>\n<p>Then you&#8217;ll want to hit <b>i<\/b> to enter <b>Insert<\/b> mode once vim is loaded up and enter in the following code:<\/p>\n<pre class=\"cli\" style=\"width: 500px;\">#!\/bin\/bash trigger=4.00 load=`cat \/proc\/loadavg | awk '{print $1}'` response=`echo | awk -v T=$trigger -v L=$load 'BEGIN{if ( L &gt; T){ print \"greater\"}}'` if [[ $response = \"greater\" ]] then sar -q | mail -s\"High load on server - [ $load ]\" recipient@example.com fi<\/pre>\n<p>Here is the same code again with comments walking through what each line is doing:<\/p>\n<p class=\"cli\" style=\"width: 500px;\">#!\/bin\/bash<\/p>\n<p> <span style=\"color: blue;\">We set a <b>trigger<\/b> for how high the load can get before we&#8217;re<\/span><br \/>\n<span style=\"color: blue;\">alerted via e-mail from this script.<\/span><br \/>\ntrigger=4.00<\/p>\n<p><span style=\"color: blue;\">We set a <b>load<\/b> variable to read the current server load from<\/span><br \/>\n<span style=\"color: blue;\"><b>\/proc\/loadavg<\/b> and only from the first column which is the live load.<\/span><br \/>\nload=`cat \/proc\/loadavg | awk &#8216;{print $1}&#8217;`<\/p>\n<p><span style=\"color: blue;\">We set a <b>response<\/b> variable to the word <b>&#8220;greater&#8221;<\/b> if the current<\/span><br \/>\n<span style=\"color: blue;\"><b>load<\/b> is greater than our <b>trigger<\/b> that we set.<\/span><br \/>\nresponse=`echo | awk -v T=$trigger -v L=$load &#8216;BEGIN{if ( L &gt; T){ print &#8220;greater&#8221;}}&#8217;`<\/p>\n<p><span style=\"color: blue;\">If the <b>response<\/b> is set to <b>&#8220;greater&#8221;<\/b> we run the <b>sar -q<\/b> command<\/span><br \/>\n<span style=\"color: blue;\">and pipe <b>|<\/b> that data to the <b>mail<\/b> command for <b>recipient@example.com<\/b><\/span><br \/>\n<span style=\"color: blue;\">this sends an e-mail with the server&#8217;s recent load averages there.<\/span><br \/>\nif [[ $response = &#8220;greater&#8221; ]]\nthen<br \/>\nsar -q | mail -s&#8221;High load on server &#8211; [ $load ]&#8221; recipient@example.com<br \/>\nfi<\/li>\n<\/ol>\n<h2>Setup cron job to run monitoring script<\/h2>\n<ol class=\"article_list\">\n<li>Now that you have your load monitoring bash script setup, the next thing you&#8217;ll want to do is create a cron job to run this task, you can read about <a href=\"\/support\/\">how to run a cron job<\/a>.\n<p style=\"margin: -5px;\">\u00c2&nbsp;<\/p>\n<p>Typically setting up a cron job for monitoring of this type would be fine at every 5 minutes, you can select that premade option in cPanel&#8217;s cron jobs section, it should end up looking like the following once you&#8217;re done:<\/p>\n<p><span class=\"cli\">*\/5 * * * * bash \/home\/userna1\/loadMon<\/span><\/li>\n<\/ol>\n<p>You should now have successfully setup a small bash script to monitor your servers load average, and a cron job to run that monitoring script on a set interval to send you an e-mail if the server&#8217;s load has exceeded the limit you&#8217;ve set.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll discuss how you can setup a small server load monitoring bash script with a scheduled cron job to monitor your server&#8217;s load average, and e-mail you a notice if it&#8217;s above the limit that you&#8217;ve set. A lot of times your server will probably be running at a load much lower<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/\"> Read More ><\/a><\/p>\n","protected":false},"author":57014,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4364],"tags":[],"class_list":["post-491","post","type-post","status-publish","format-standard","hentry","category-server-usage"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Create server load monitoring bash script | InMotion Hosting<\/title>\n<meta name=\"description\" content=\"In this article we&#039;ll discuss how you can setup a small bash script on a scheduled cron job to monitor your server&#039;s load average, and e-mail you a notice if it&#039;s above the limit that you&#039;ve set.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create server load monitoring bash script | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"In this article we&#039;ll discuss how you can setup a small bash script on a scheduled cron job to monitor your server&#039;s load average, and e-mail you a notice if it&#039;s above the limit that you&#039;ve set.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2012-12-04T21:30:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-17T03:27:34+00:00\" \/>\n<meta name=\"author\" content=\"InMotion Hosting Contributor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InMotion Hosting Contributor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/\"},\"author\":{\"name\":\"InMotion Hosting Contributor\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644\"},\"headline\":\"Create server load monitoring bash script\",\"datePublished\":\"2012-12-04T21:30:25+00:00\",\"dateModified\":\"2021-08-17T03:27:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/\"},\"wordCount\":719,\"commentCount\":15,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"articleSection\":[\"Server Usage\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/\",\"name\":\"Create server load monitoring bash script | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2012-12-04T21:30:25+00:00\",\"dateModified\":\"2021-08-17T03:27:34+00:00\",\"description\":\"In this article we'll discuss how you can setup a small bash script on a scheduled cron job to monitor your server's load average, and e-mail you a notice if it's above the limit that you've set.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create server load monitoring bash script\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644\",\"name\":\"InMotion Hosting Contributor\",\"description\":\"InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/inmotion-hosting\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/InMotionHosting\"],\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/inmotion-hosting-contributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create server load monitoring bash script | InMotion Hosting","description":"In this article we'll discuss how you can setup a small bash script on a scheduled cron job to monitor your server's load average, and e-mail you a notice if it's above the limit that you've set.","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:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/","og_locale":"en_US","og_type":"article","og_title":"Create server load monitoring bash script | InMotion Hosting","og_description":"In this article we'll discuss how you can setup a small bash script on a scheduled cron job to monitor your server's load average, and e-mail you a notice if it's above the limit that you've set.","og_url":"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2012-12-04T21:30:25+00:00","article_modified_time":"2021-08-17T03:27:34+00:00","author":"InMotion Hosting Contributor","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"InMotion Hosting Contributor","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/"},"author":{"name":"InMotion Hosting Contributor","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644"},"headline":"Create server load monitoring bash script","datePublished":"2012-12-04T21:30:25+00:00","dateModified":"2021-08-17T03:27:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/"},"wordCount":719,"commentCount":15,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"articleSection":["Server Usage"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/","url":"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/","name":"Create server load monitoring bash script | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2012-12-04T21:30:25+00:00","dateModified":"2021-08-17T03:27:34+00:00","description":"In this article we'll discuss how you can setup a small bash script on a scheduled cron job to monitor your server's load average, and e-mail you a notice if it's above the limit that you've set.","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/server\/server-usage\/create-server-load-monitoring-bash-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"Create server load monitoring bash script"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644","name":"InMotion Hosting Contributor","description":"InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!","sameAs":["https:\/\/www.linkedin.com\/company\/inmotion-hosting\/","https:\/\/x.com\/https:\/\/twitter.com\/InMotionHosting"],"url":"https:\/\/www.inmotionhosting.com\/support\/author\/inmotion-hosting-contributor\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/57014"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=491"}],"version-history":[{"count":3,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/491\/revisions"}],"predecessor-version":[{"id":85237,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/491\/revisions\/85237"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}