{"id":17040,"date":"2024-05-14T13:02:46","date_gmt":"2024-05-14T13:02:46","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=17040"},"modified":"2024-05-14T13:08:59","modified_gmt":"2024-05-14T13:08:59","slug":"shell-script-to-monitor-linux-server","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2024\/05\/14\/shell-script-to-monitor-linux-server\/","title":{"rendered":"Shell Script to Monitor Linux Server."},"content":{"rendered":"\n<p>In this article, we will learn how to monitor linux server using various shell commands and tools. Monitor linux server is crucial for maintaining the health and performance of your servers, workstations, or any computing devices. We\u2019ll cover the creation of a basic script that tracks CPU usage, memory consumption, and disk space.<\/p>\n\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prerequisites<\/li>\n\n\n\n<li>Step #1:Create a file in Ubuntu<\/li>\n\n\n\n<li>Step #2:Write Shell script to monitor linux server<\/li>\n\n\n\n<li>Step #3:Make file executable<\/li>\n\n\n\n<li>Step #4:Run the script<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Prerequisites<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>AWS Account with Ubuntu 24.04 LTS EC2 Instance.<\/li>\n\n\n\n<li>Basic knowledge of Shell scripting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step #1:Create a file in Ubuntu<\/strong><\/h3>\n\n\n\n<p>Open the terminal and use the&nbsp;<strong>nano&nbsp;<\/strong>command to create a new file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>nano monitor_resources.sh<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step #2:Write Shell script to monitor linux server<\/strong><\/h3>\n\n\n\n<p>Write the script to monitor linux server into the file.<\/p>\n\n\n\n<p>Following is a shell script to monitor system resources such as CPU usage, memory usage, disk space of linux server:<\/p>\n\n\n\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"530\" height=\"423\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/2-1.png\" alt=\"\" class=\"wp-image-17042\" style=\"width:674px;height:auto\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/2-1.png 530w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/2-1-300x239.png 300w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>------------------------------------\n\n<strong>#!\/bin\/bash\n\nwhile true; do\n clear\n echo \"System Resource Monitoring\"\n echo \"--------------------------\"\n\n # Display CPU usage\n echo \"CPU Usage:\"\n top -n 1 -b | grep \"Cpu\"\n\n # Display memory usage\n echo -e \"\\nMemory Usage:\"\n free -h\n\n # Display disk space usage\n echo -e \"\\nDisk Space Usage:\"\n df -h\n\n sleep 5 # Wait for 5 seconds before the next update\ndone<\/strong>\n---------------------------------\n\nSave the file and exit the editor.\n\n\n\n<strong>Explanation of the Script:<\/strong>\n\n<strong>Shebang (#!\/bin\/bash)<\/strong>: Specifies the shell to be used for interpreting the script.\n<strong>while Loop<\/strong>: Creates an infinite loop that repeatedly gathers and displays system resource information.\n<strong>clear<\/strong>: Clears the terminal screen for a cleaner display.\n<strong>Display CPU Usage<\/strong>: Uses the top command with the -n 1 flag to display a single iteration of CPU usage information. The grep \u201cCpu\u201d command filters out the relevant line.\n<strong>Display Memory Usage<\/strong>: Uses the free command with the -h flag to display memory usage in a human-readable format.\n<strong>Display Disk Space Usage<\/strong>: Uses the df command with the -h flag to display disk space usage in a human-readable format.\n<strong>sleep 5<\/strong>: Pauses the loop for 5 seconds before gathering and displaying resource information again.\n\n----------------------------------\n\n<strong>Step #3:Make file executable<\/strong>\nChange the file permissions to make it executable using the chmod command.\n\n<strong>chmod +x monitor_resources.sh<\/strong>\n\nRun the script by executing the following command:\n\n<strong>.\/monitor_resources.sh<\/strong>\n\n<strong>Output:<\/strong><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"382\" src=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/monitor-linux-server.png\" alt=\"\" class=\"wp-image-17041\" srcset=\"https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/monitor-linux-server.png 816w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/monitor-linux-server-300x140.png 300w, https:\/\/www.appservgrid.com\/paw92\/wp-content\/uploads\/2024\/05\/monitor-linux-server-768x360.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>The terminal will now display system resource information, updating every 5 seconds. To stop the script, press <strong>CTRL + C<\/strong>.<br><br><strong>Conclusion:<\/strong><br><br>In conclusion, to monitor linux server using shell scripts on Ubuntu provides a straightforward and efficient way to keep track of CPU usage, memory consumption, and disk space. By following the steps to create, write, make executable, and run the monitor_resources.sh script, you can easily gather and display essential system information in real-time.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.fosstechnix.com\/shell-script-to-monitor-linux-server\/\" data-type=\"link\" data-id=\"https:\/\/www.fosstechnix.com\/shell-script-to-monitor-linux-server\/\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn how to monitor linux server using various shell commands and tools. Monitor linux server is crucial for maintaining the health and performance of your servers, workstations, or any computing devices. We\u2019ll cover the creation of a basic script that tracks CPU usage, memory consumption, and disk space. Table of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2024\/05\/14\/shell-script-to-monitor-linux-server\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Shell Script to Monitor Linux Server.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17040","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/17040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=17040"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/17040\/revisions"}],"predecessor-version":[{"id":17044,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/17040\/revisions\/17044"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=17040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=17040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=17040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}