{"id":492,"date":"2012-12-04T17:09:35","date_gmt":"2012-12-04T22:09:35","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2012\/12\/04\/create-mysql-query-monitoring-bash-script\/"},"modified":"2021-08-16T23:27:34","modified_gmt":"2021-08-17T03:27:34","slug":"create-mysql-query-monitoring-bash-script","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/","title":{"rendered":"Create MySQL query monitoring bash script"},"content":{"rendered":"<p>In this article we&#8217;ll discuss how you can configure a simple bash script to check on your MySQL query activity to ensure that long queries are not hanging on your server. This script will also be able to e-mail you a notice when it has encountered a long running query.<\/p>\n<p>In most cases a MySQL database query should be able to execute within a few seconds, if for some reason it takes longer than that, there could be a possibility of a long running MySQL query backing up that database queue and leading to your server slowing down, and possibly becoming unstable.<\/p>\n<p>For more information on types of MySQL usage problems you can read about <a href=\"\/support\/server\/databases\/excessive-mysql-activity\/\">excessive MySQL activity<\/a>.<\/p>\n<p class=\"alert\">This script can only be setup on a VPS or dedicated server hosting plan that you have <a href=\"\/support\/server\/ssh\/root-access-faq\/\">root access<\/a> to.<\/p>\n<h2>Create MySQL query monitoring script<\/h2>\n<ol class=\"article_list\">\n<li>Depending on the complexity of your database, how many websites you run, and how much traffic you get, the max amount of time that you&#8217;d like to allow a query to run could be different. I would recommend starting off with something conservative around 120 seconds or 2 minutes, and if you&#8217;re logging in to your server after being alerted and it seems to still be running stable, you could probably increase the trigger level to something higher.Start editing a new file for the bash MySQL query alert script, in this case I&#8217;ll be using the <strong>vim<\/strong> text-editor and making a new file called <strong>MySQLMon<\/strong> in my user&#8217;s home directory with the following command:<code>vim \/home\/userna1\/MySQLMon<\/code>Then you&#8217;ll want to hit <strong>i<\/strong> to enter <strong>Insert<\/strong> mode once vim is loaded up and enter in the following code:<br \/>\n<code>#!\/bin\/bash trigger=120 activeQcount=`mysql -e \"show full processlist;\" |<br \/>\negrep -v \"leechprotect|root|Time\" | awk -v T=$trigger '{ if ( $6 &gt; T ) print $0}' |<br \/>\nwc -l` if [ $activeQcount -gt 0 ] then echo<br \/>\n\"=====================================================================================\" &gt;<br \/>\n\/tmp\/MySQLMon echo \"= MySQLMon has found a new query running for longer than 120 seconds (2 mins)\"<br \/>\n>&gt; \/tmp\/MySQLMon echo<br \/>\n\"=====================================================================================\" &gt;&gt;<br \/>\n\/tmp\/MySQLMon date &gt;&gt; \/tmp\/MySQLMon echo \"\" &gt;&gt; \/tmp\/MySQLMon mysql -e \"show full processlist;\"<br \/>\n>&gt; \/tmp\/MySQLMon echo<br \/>\n\"=====================================================================================\"&gt;&gt;<br \/>\n\/tmp\/MySQLMon cat \/tmp\/MySQLMon | awk -v T=$trigger '{ if ( $6 &gt; T ) print $0}' |<br \/>\nmail -s\"Caught query running longer than $trigger seconds\" recipient@Domain.com -- -r<br \/>\n\"MySQLMon@Domain.com\" fi<\/code><br \/>\nThis code can look a bit overwhelming at first, but broken down it&#8217;s really quite simple. First we are declaring a <strong>trigger<\/strong> variable and setting that to <strong>120<\/strong> seconds. Then we are setting an <strong>activeQcount<\/strong> variable to basically hold any queries that are longer than our <strong>trigger<\/strong> value.<\/p>\n<p>We then use a bash <strong>if<\/strong> statement to check if our <strong>activeQcount<\/strong> value is above zero, showing that there are queries running longer than our set <strong>trigger<\/strong> value. Next we simply start <strong>echo&#8217;ing<\/strong> text into a placeholder file called <strong>\/tmp\/MySQLMon<\/strong>, then finally we <strong>cat<\/strong> that placeholder file which just reads it out and then we pipe it <strong>|<\/strong> to the <strong>mail<\/strong> function followed by a subject we&#8217;d like to use, the recipient&#8217;s address, then you can enter in <strong>&#8212; -r<\/strong> followed by the email address you&#8217;d like the message to come from.<\/li>\n<li>When you receive an e-mail alert it will look much like this example:<br \/>\n<code><br \/>\n======================================================================================<br \/>\nMySQLMon has found a new query running for longer than 120 seconds (2 mins)<br \/>\n=====================================================================================<br \/>\nTue Dec  4 15:07:42 EST 2012 40901   userna1_phpb1   localhost       userna1_phpb1   Query   342<br \/>\nSending data<br \/>\nINSERT INTO users (userID, name, base64_decode)<br \/>\n=====================================================================================<\/code><\/li>\n<\/ol>\n<h2>Setup cron job to run MySQLMon script<\/h2>\n<ol class=\"article_list\">\n<li>Now you should have your MySQL query monitoring bash script setup, next you&#8217;ll want to create a cron job to run this task. If you&#8217;re unframiliar with cron jobs you can read about <a href=\"\/support\/\">how to run a cron job<\/a>.You can use the cPanel drop-down for every 5 minutes, which should make the final cron job looking like:<code>*\/5 * * * * bash \/home\/userna1\/MySQLMon<\/code><\/li>\n<\/ol>\n<p>You should now have successfully setup a bash script to monitor your MySQL queries to catch any long running ones, and to alert you via e-mail when it catches any. You should also know how to setup a cron job to run that script on a set interval so that it will constantly run without further manual intervention from yourself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll discuss how you can configure a simple bash script to check on your MySQL query activity to ensure that long queries are not hanging on your server. This script will also be able to e-mail you a notice when it has encountered a long running query. In most cases a MySQL<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-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":[4305],"tags":[],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-databases"],"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 MySQL query monitoring bash script | InMotion Hosting<\/title>\n<meta name=\"description\" content=\"In this article we&#039;ll discuss how you can configure a simple bash script to check on your MySQL query activity to ensure that long queries are not hanging on your server. This script will also be able to e-mail you a notice when it has encountered a long running query.\" \/>\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\/databases\/create-mysql-query-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 MySQL query monitoring bash script | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"In this article we&#039;ll discuss how you can configure a simple bash script to check on your MySQL query activity to ensure that long queries are not hanging on your server. This script will also be able to e-mail you a notice when it has encountered a long running query.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-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-04T22:09:35+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=\"3 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\/databases\/create-mysql-query-monitoring-bash-script\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/\"},\"author\":{\"name\":\"InMotion Hosting Contributor\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644\"},\"headline\":\"Create MySQL query monitoring bash script\",\"datePublished\":\"2012-12-04T22:09:35+00:00\",\"dateModified\":\"2021-08-17T03:27:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/\"},\"wordCount\":572,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"articleSection\":[\"Working with Databases\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/\",\"name\":\"Create MySQL query monitoring bash script | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2012-12-04T22:09:35+00:00\",\"dateModified\":\"2021-08-17T03:27:34+00:00\",\"description\":\"In this article we'll discuss how you can configure a simple bash script to check on your MySQL query activity to ensure that long queries are not hanging on your server. This script will also be able to e-mail you a notice when it has encountered a long running query.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create MySQL query 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 MySQL query monitoring bash script | InMotion Hosting","description":"In this article we'll discuss how you can configure a simple bash script to check on your MySQL query activity to ensure that long queries are not hanging on your server. This script will also be able to e-mail you a notice when it has encountered a long running query.","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\/databases\/create-mysql-query-monitoring-bash-script\/","og_locale":"en_US","og_type":"article","og_title":"Create MySQL query monitoring bash script | InMotion Hosting","og_description":"In this article we'll discuss how you can configure a simple bash script to check on your MySQL query activity to ensure that long queries are not hanging on your server. This script will also be able to e-mail you a notice when it has encountered a long running query.","og_url":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2012-12-04T22:09:35+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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/"},"author":{"name":"InMotion Hosting Contributor","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/f9a4fc454cd1df128ee8e898d30d4644"},"headline":"Create MySQL query monitoring bash script","datePublished":"2012-12-04T22:09:35+00:00","dateModified":"2021-08-17T03:27:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/"},"wordCount":572,"commentCount":0,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"articleSection":["Working with Databases"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/","url":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/","name":"Create MySQL query monitoring bash script | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2012-12-04T22:09:35+00:00","dateModified":"2021-08-17T03:27:34+00:00","description":"In this article we'll discuss how you can configure a simple bash script to check on your MySQL query activity to ensure that long queries are not hanging on your server. This script will also be able to e-mail you a notice when it has encountered a long running query.","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/server\/databases\/create-mysql-query-monitoring-bash-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"Create MySQL query 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\/492","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=492"}],"version-history":[{"count":4,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":85236,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/492\/revisions\/85236"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}