{"id":2875,"date":"2024-03-04T12:17:58","date_gmt":"2024-03-04T06:47:58","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=2875"},"modified":"2025-03-07T10:05:43","modified_gmt":"2025-03-07T04:35:43","slug":"linux-watch-command","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/linux-watch-command\/","title":{"rendered":"How to use Watch Command in Linux"},"content":{"rendered":"\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"446\" data-id=\"2899\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1-1024x446.png\" alt=\"How to use Watch Command in Linux\" class=\"wp-image-2899\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1-1024x446.png 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1-300x131.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1-768x335.png 768w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p>The Watch command in Linux is super helpful, especially if you&#8217;re someone who monitors Linux systems. Imagine you&#8217;re in charge of a busy website server that starts acting weird. Monitoring for errors manually is repetitive and exhausting. That&#8217;s where <code><a href=\"https:\/\/linux.die.net\/man\/1\/watch\" target=\"_blank\" rel=\"noopener\">watch<\/a><\/code> comes in like a superhero. \ud83e\uddb8<\/p>\n\n\n\n<p>Think of <code>watch<\/code> as your robot that keeps checking on things for you, automatically refreshing the information every few seconds. So, whether you&#8217;re watching to see if the server&#8217;s workload is too high, or if a certain service has stopped working, <code>watch<\/code> does all the repetitive work for you. This means you can pay attention to fixing problems or doing other important tasks, all while <code>watch<\/code> keeping you updated on what&#8217;s happening in the background. It&#8217;s like an untiring extra set of eyes, essential for anyone managing Linux systems.<\/p>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"basic-syntax-for-watch-command\">Basic Syntax for Watch Command:<\/h4>\n\n\n<p>The basic syntax of the <code>watch<\/code> command is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>watch &#91;options] &#91;command]<\/code><\/pre>\n\n\n\n<p>In the above command format, replace [command] with the repetitive Linux command you need to execute, and <code>[options]<\/code> are extra bits you can add to change how <code>watch<\/code> works.<\/p>\n\n\n\n<pre class=\"wp-block-verse\">The <code>watch<\/code> command refreshes its output every 2 seconds by default. However, you can adjust this frequency to your preference by using the <code>-n<\/code> or <code>--interval<\/code> option even to fractions of a second (minimum 0.1 seconds). Both period (.) and comma (,) are accepted for decimals in any locale.<\/pre>\n\n\n\n<p>Here&#8217;s a look at some of the key options you can use with <code>watch<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Option<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>-b<\/code>, <code>--beep<\/code><\/td><td>Beep if the command exits with a non-zero status, indicating an error or a noteworthy condition.<\/td><\/tr><tr><td><code>-c<\/code>, <code>--color<\/code><\/td><td>Interpret ANSI color and style sequences in the command&#8217;s output, allowing for colored text.<\/td><\/tr><tr><td><code>-d<\/code>, <code>--differences[=&lt;permanent&gt;]<\/code><\/td><td>Highlight changes between updates. Adding <code>=permanent<\/code> keep the changes highlighted.<\/td><\/tr><tr><td><code>-e<\/code>, <code>--errexit<\/code><\/td><td><br>Exit <code>watch<\/code> if the command exits with a non-zero status, useful for stopping when an error is detected.<\/td><\/tr><tr><td><code>-g<\/code>, <code>--chgexit<\/code><\/td><td>Exit <code>watch<\/code> when the command&#8217;s output changes, useful for monitoring for specific changes.<\/td><\/tr><tr><td><code>-n<\/code>, <code>--interval &lt;secs&gt;<\/code><\/td><td>Set the time (in seconds) between command executions. Adjusts how frequently <code>watch<\/code> updates.<\/td><\/tr><tr><td><code>-p<\/code>, <code>--precise<\/code><\/td><td>Attempt to run the command at precise intervals, aiming for accuracy in timing.<\/td><\/tr><tr><td><code>-t<\/code>, <code>--no-title<\/code><\/td><td>Turn off the header that shows the command, current time, and refresh interval.<\/td><\/tr><tr><td><code>-w<\/code>, <code>--no-wrap<\/code><\/td><td>Turn off line wrapping, which can help avoid visual clutter with long lines of text.<\/td><\/tr><tr><td><code>-x<\/code>, <code>--exec<\/code><\/td><td>Pass the command to <code>exec<\/code> instead of the default <code>sh -c<\/code>, affecting how the command is executed.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"practical-examples-for-watch-command\">Practical Examples for Watch Command:<\/h4>\n\n\n<p><strong>Monitoring System Load<\/strong>: To monitor the system load every 2 seconds, you can use the <code>uptime<\/code> command with <code>watch<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>watch uptime<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\"><em><strong>Sample Output:<\/strong><\/em><\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-125828.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"991\" height=\"69\" data-id=\"2885\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-125828.png\" alt=\"Monitor system load with watch command\" class=\"wp-image-2885\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-125828.png 991w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-125828-300x21.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-125828-768x53.png 768w\" sizes=\"auto, (max-width: 991px) 100vw, 991px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<pre class=\"wp-block-verse\">The watch command in Linux continues to execute within the terminal window, remaining active until it is manually terminated by pressing CTRL+C.<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Monitor Disk Space Every 10 Seconds:<\/strong> For monitoring disk space usage at 10-second intervals, you can pair the <a href=\"https:\/\/www.veeble.org\/kb\/linux-disk-usage-guide\/\" target=\"_blank\" rel=\"noopener\"><code>df -h<\/code> command<\/a> with <code>watch<\/code>, specifying the interval duration using the <code>-n<\/code> option<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>watch -n 10 df -h<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\"><strong><em>Sample Output:<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-3 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-131104.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"151\" data-id=\"2890\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-131104.png\" alt=\"Monitor disk space with watch command\" class=\"wp-image-2890\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-131104.png 682w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-131104-300x66.png 300w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p><strong>Monitor Changes in Real-Time:<\/strong> To observe changes in the running processes on your system and highlight these variations, you can utilize the <code>watch<\/code> command with the <code>-d<\/code> option alongside the <code>ps aux<\/code> command. This setup allows you to detect new processes that start and existing ones that stop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>watch -d date<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\"><strong><em>Sample Output:<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-4 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-130925.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"85\" data-id=\"2889\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-130925.png\" alt=\"Monitor changes in real time with watch command\" class=\"wp-image-2889\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-130925.png 684w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-130925-300x37.png 300w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Monitor for Changes in Network Connections:<\/strong> To automatically exit the <code>watch<\/code> command when there&#8217;s a change in your network connections, use the <code>-g<\/code> option along with the <a href=\"https:\/\/www.veeble.org\/kb\/essential-linux-network-commands\/#:~:text=5.%20netstat,monitoring%20network%20activity.\" target=\"_blank\" rel=\"noopener\"><code>netstat<\/code> command<\/a>. This method is effective for tracking new or terminated network connections.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>watch -g netstat -tuln<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-5 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-131253.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"239\" data-id=\"2891\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-131253.png\" alt=\"Monitor for Changes in Network Connections with watch command\" class=\"wp-image-2891\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-131253.png 680w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-04-131253-300x105.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Monitor Disk Usage with Colored Output:<\/strong> This option hides the usual header, showing only the command&#8217;s results for a cleaner and more focused output. If you&#8217;re using a command like <code>df<\/code> that supports color-coded output (through aliases or functions that add color to <code>df<\/code> output), you can monitor changes in disk space usage with enhanced readability:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>watch -c df -h<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-verse\">The <code>-c<\/code> option is effective when the monitored command supports ANSI color codes. Colors may vary with your terminal's settings and themes, so they can look different to others.<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Alert on File System Changes:<\/strong> This alert system quickly alerts you to errors, freeing you from having to watch the terminal non-stop. To monitor a directory for changes and get an audible alert if an error occurs during the monitoring process (for example, if the directory becomes inaccessible), you might use the <code>ls<\/code> command within <code>watch<\/code> like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>watch -b ls \/path\/to\/directory<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-verse\">If the beep function in the watch command doesn't work, you may need to install the beep package on your system. Permissions or audio configuration issues can also affect the beep function. Ensure your user has the necessary permissions and your audio system is configured correctly.<\/pre>\n\n\n\n<p><strong>Beep Alert on Unauthorized Access Attempts:<\/strong> Using <code>grep<\/code> with any command helps filter out specific details. To watch for unauthorized access attempts in <code>\/var\/log\/auth.log<\/code> and get beep alerts, combine <code>grep<\/code> with the <code>watch<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>watch -b \"grep 'Failed' \/var\/log\/auth.log\"<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>This guide offers a basic understanding of the <code>watch<\/code> command for real-time system monitoring. For deeper insights, explore advanced usage, scripting, performance, troubleshooting, and output customization. Enhance your Linux monitoring skills effectively.<\/p>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-0d7e6975 wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Powerful VPS Hosting, Without the Premium Price<\/h3><p class=\"uagb-cta__desc\">Get the performance you need at a price you&#8217;ll love. Explore our affordable VPS plans.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/vps-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">Choose Your Plan<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Watch command in Linux is super helpful, especially if you&#8217;re someone who monitors Linux systems. Imagine you&#8217;re in charge of a busy website [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":2899,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2875","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1.png",1200,523,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1-150x150.png",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1-300x131.png",300,131,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1-768x335.png",768,335,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1-1024x446.png",1024,446,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1.png",1200,523,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/03\/How-to-use-shell-genie-1.png",1200,523,false]},"uagb_author_info":{"display_name":"Adrian Antony","author_link":"https:\/\/www.veeble.com\/kb\/author\/adrian\/"},"uagb_comment_info":0,"uagb_excerpt":"The Watch command in Linux is super helpful, especially if you&#8217;re someone who monitors Linux systems. Imagine you&#8217;re in charge of a busy website [&hellip;]","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=2875"}],"version-history":[{"count":15,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2875\/revisions"}],"predecessor-version":[{"id":7740,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2875\/revisions\/7740"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/2899"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=2875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=2875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=2875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}