{"id":2334,"date":"2019-02-27T20:26:54","date_gmt":"2019-02-27T20:26:54","guid":{"rendered":"http:\/\/www.incredigeek.com\/home\/?p=2334"},"modified":"2020-01-17T13:06:37","modified_gmt":"2020-01-17T19:06:37","slug":"linux-bash-script-to-monitor-system-service","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/linux-bash-script-to-monitor-system-service\/","title":{"rendered":"Bash script to monitor system service"},"content":{"rendered":"\n<p>This bash script runs and checks to see if a service like httpd, or mysql is running and alerts if it is not.<\/p>\n\n\n\n<p><strong>Script Usage<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">servicemonitor.sh httpd mariadb<\/pre>\n\n\n\n<p>Where httpd and mariadb are the services you want to monitor\/check.<\/p>\n\n\n\n<p><strong>Setup Script<\/strong><\/p>\n\n\n\n<p>Create servicemonitor.sh file and paste the following contents in.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n\ntimeHour=<code>`date +%H`<\/code> # date\/time just shows the hour\nquietHour=\"02\"    # If it is this hour, then exit program, useful if services are expected to go down during a particular time for maintenance\nif ( echo ${timeHour} | grep ${quietHour}); then\n         echo \"Is during quiet time.  Quiting.\"\n         exit\nfi\n\n function ALERT {\n msg=\"~\/teams.sh -b\"  # Sends a message to Microsoft Teams channel.  Needs the teams.sh script in the users home directory.\n ${msg} \"$1\"\n }\n function SERVICECHECK {\n serviceName=\"${1}\"\n if (systemctl status ${serviceName} | grep Active | grep inactive); then\n         ALERT \"ERROR: $(hostname) - ${serviceName} - ${0} is inactive\"\n         echo \"ERROR: ${serviceName} is inactive!\"\n else\n         echo \"Running!\"\n fi\n }\n for i in $@\n do\n echo Checking ${i}\n SERVICECHECK ${i}\n done<\/pre>\n\n\n\n<p><em>Note the teams.sh script that is called is another script that is called that sends an alert to Microsoft Teams. Is not needed for this script to run, but allows for remote alerting.<\/em><\/p>\n\n\n\n<p>Save file and make it executable<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">chmod +x servicemonitor.sh<br><\/pre>\n\n\n\n<p><strong>Add script to crontab (Optional)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">crontab -e<\/pre>\n\n\n\n<p>The following runs the script every 5 minutes.  Can change the 5 to 1 to run every minute.  Change httpd and mariadb to the service you want to monitor.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">*\/5 * * * * \/home\/UserName\/servicemonitor.sh httpd mariadb<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This bash script runs and checks to see if a service like httpd, or mysql is running and alerts if it is not. Script Usage servicemonitor.sh httpd mariadb Where httpd and mariadb are the services you want to monitor\/check. Setup &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/linux-bash-script-to-monitor-system-service\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,266],"tags":[211,245,7,243],"class_list":["post-2334","post","type-post","status-publish","format-standard","hentry","category-linux","category-scripts","tag-bash","tag-bash-scripts","tag-linux-2","tag-scripts"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=2334"}],"version-history":[{"count":6,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2334\/revisions"}],"predecessor-version":[{"id":2963,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/2334\/revisions\/2963"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=2334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=2334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=2334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}