{"id":13179,"date":"2019-04-01T05:07:42","date_gmt":"2019-04-01T05:07:42","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13179"},"modified":"2019-04-01T05:07:42","modified_gmt":"2019-04-01T05:07:42","slug":"how-to-run-or-repeat-a-linux-command-every-x-seconds-forever","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-run-or-repeat-a-linux-command-every-x-seconds-forever\/","title":{"rendered":"How to Run or Repeat a Linux Command Every X Seconds Forever"},"content":{"rendered":"<p>A system administrator often needs to run a command repeatedly in a certain periods of time. Often such tasks can be easily completed with simple\u00a0<a href=\"https:\/\/www.tecmint.com\/11-cron-scheduling-task-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cron\u00a0commands<\/a>. In most of the cases \u00a0this should work, but the shortest\u00a0period which you can run cron command is every 1 minute. Believe it or not, in many cases this is too slow.<\/p>\n<div id=\"attachment_18989\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Run-Linux-Command-Every-Second.png\" rel=\"attachment wp-att-18989\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18989\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Run-Linux-Command-Every-Second.png\" alt=\"Run Linux Command Every Second\" width=\"720\" height=\"342\" aria-describedby=\"caption-attachment-18989\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18989\" class=\"wp-caption-text\">Run Linux Command Every Second<\/p>\n<\/div>\n<p>In this tutorial, you will learn\u00a0a simple scripting techniques to monitor or keep a eye on a particular command in continuously running state similar to\u00a0<a href=\"https:\/\/www.tecmint.com\/12-top-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">top command (continuously monitor the process and memory utilization)<\/a>for every 3 seconds by default.<\/p>\n<p>We will not stop to discuss the reasons, why you would need to run commands this often. I believe everyone has different reasons for that in their daily jobs or even at home PCs and laptops.<\/p>\n<h3>1. Use watch Command<\/h3>\n<p><strong>Watch<\/strong>\u00a0is a Linux command that allows you to execute a command or program periodically and also shows you output on the screen. This means that you will be able to see the program output in time. By default watch re-runs the command\/program every 2 seconds. The interval can be easily changed to meet your requirements.<\/p>\n<h4>Monitor Memory Usage<\/h4>\n<p>\u201cWatch\u201d is extremely easy to use, to test it, you can fire up a Linux terminal right away and type the following command:<\/p>\n<pre># watch free -m\r\n<\/pre>\n<p>The above command will check your system free memory and update the results of the\u00a0<a href=\"https:\/\/www.tecmint.com\/check-memory-usage-in-linux\/\" target=\"_blank\" rel=\"noopener\">free command<\/a>\u00a0every two seconds.<\/p>\n<div id=\"attachment_18983\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Monitor-Memory-Usage-in-Linux.gif\" rel=\"attachment wp-att-18983\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18983\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Monitor-Memory-Usage-in-Linux.gif\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Monitor-Memory-Usage-in-Linux.gif 812w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Monitor-Memory-Usage-in-Linux-768x248.gif 768w\" alt=\"Monitor Memory Usage in Linux\" width=\"812\" height=\"262\" aria-describedby=\"caption-attachment-18983\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18983\" class=\"wp-caption-text\">Monitor Memory Usage in Linux<\/p>\n<\/div>\n<p>As\u00a0seen per the above output, you have a header, displaying information about (from left to right)\u00a0update interval, command that is being executed and current time. If you wish to hide this header, you can use the\u00a0<code>-t<\/code>option.<\/p>\n<p>The next logical question is \u2013 how to change the execution\u00a0interval. For that purpose,\u00a0you can use the\u00a0<code>-n<\/code>option, that specifies the interval with which the command will be executed. This interval is specified in seconds. So let\u2019s say you want to run your\u00a0<code>script.sh<\/code>\u00a0file every 10 seconds, you can do it like this:<\/p>\n<pre># watch -n 10 script.sh\r\n<\/pre>\n<p><strong>Note<\/strong>\u00a0that\u00a0if you run the command like shown above, you will need to\u00a0<code>cd<\/code>\u00a0to the directory (learn\u00a0<a href=\"https:\/\/www.tecmint.com\/cd-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">Learn 15 cd Command Examples<\/a>) where the script is located or otherwise specify the full path to that script.<\/p>\n<p>Other useful options of watch command are:<\/p>\n<ol>\n<li><code>-b<\/code>\u00a0\u2013 creates\u00a0a beep sound if the exit of the command is non-zero.<\/li>\n<li><code>-c<\/code>\u00a0\u2013 Interprets ANSI color sequences.<\/li>\n<li><code>-d<\/code>\u00a0\u2013 highlights the changes in the command output.<\/li>\n<\/ol>\n<h4>Monitor Logged-In Users, Uptime and Load Average<\/h4>\n<p>Let\u2019s say you want to monitor logged-in users, server uptime and load average output in continuously phase every few seconds, then use following command as shown:<\/p>\n<pre># watch uptime\r\n<\/pre>\n<div id=\"attachment_18580\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Watch-Linux-Load-Average.gif\" rel=\"attachment wp-att-18580\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18580\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Watch-Linux-Load-Average.gif\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Watch-Linux-Load-Average.gif 816w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Watch-Linux-Load-Average-768x115.gif 768w\" alt=\"Watch Linux Load Average\" width=\"816\" height=\"122\" aria-describedby=\"caption-attachment-18580\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18580\" class=\"wp-caption-text\">Watch Linux Load Average<\/p>\n<\/div>\n<p>To exit the command, press\u00a0<code>CTRL+C<\/code>.<\/p>\n<p>Here, the\u00a0<code>'uptime'<\/code>\u00a0command will run and display the updated results every 2 seconds by default.<\/p>\n<h4>Monitor Progress of Copy Command<\/h4>\n<p>In Linux, while copying files from one location to other using\u00a0<code>cp<\/code>\u00a0command, the progress of data is not shown, to see the progress of data being copied, you can use the\u00a0<code>watch<\/code>\u00a0command along with\u00a0<a href=\"https:\/\/www.tecmint.com\/check-linux-disk-usage-of-files-and-directories\/\" target=\"_blank\" rel=\"noopener\">du -s command to check the disk usage<\/a>\u00a0in real time.<\/p>\n<pre># cp ubuntu-15.10-desktop-amd64.iso \/home\/tecmint\/ &amp;\r\n# watch -n 0.1 du -s \/home\/tecmint\/ubuntu-15.10-desktop-amd64.iso \r\n<\/pre>\n<div id=\"attachment_18985\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Monitor-Progress-of-Copy-Command.gif\" rel=\"attachment wp-att-18985\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18985\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Monitor-Progress-of-Copy-Command.gif\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Monitor-Progress-of-Copy-Command.gif 812w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Monitor-Progress-of-Copy-Command-768x144.gif 768w\" alt=\"Monitor Progress of Copy Command\" width=\"812\" height=\"152\" aria-describedby=\"caption-attachment-18985\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18985\" class=\"wp-caption-text\">Monitor Progress of Copy Command<\/p>\n<\/div>\n<p>If you think that the above process is too complicated to achieve, then I suggest you to go for\u00a0<a href=\"https:\/\/www.tecmint.com\/advanced-copy-command-shows-progress-bar-while-copying-files\/\" target=\"_blank\" rel=\"noopener\">Advance copy command<\/a>, which shows progress of data while copying.<\/p>\n<h3>2. Use sleep Command<\/h3>\n<p>Sleep is often used to debug shell scripts, but it has many other useful purposes as well. For example, when combined with\u00a0<code>for<\/code>\u00a0or\u00a0<code>while<\/code>\u00a0loops, you can get pretty awesome results.<\/p>\n<p>If you are new to bash scripting, you can check our guide about bash loops\u00a0<a href=\"https:\/\/www.tecmint.com\/learning-shell-scripting-language-a-guide-from-newbies-to-system-administrator\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>In case\u00a0this is the first time you hear about the\u00a0<code>\"sleep\"<\/code>\u00a0command, it is used to delay something for a specified amount of time. In scripts, you can use it to tell your script to run command 1, wait for 10 seconds and then run command 2.<\/p>\n<p>With the above loops, you can tell bash to run a command, sleep for\u00a0<strong>N<\/strong>\u00a0amount of seconds and then run the command again.<\/p>\n<p>Below you can see examples of both loops:<\/p>\n<h4>for loop Example<\/h4>\n<pre># for i in {1..10}; do echo -n \"This is a test in loop $i \"; date ; sleep 5; done\r\n<\/pre>\n<p>The above one liner, will run the\u00a0<a href=\"https:\/\/www.tecmint.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a>\u00a0and display the current date, total of 10 times, with 5 seconds sleep between executions.<\/p>\n<p>Here is a sample output:<\/p>\n<pre>This is a test in loop 1 Wed Feb 17 20:49:47 EET 2016\r\nThis is a test in loop 2 Wed Feb 17 20:49:52 EET 2016\r\nThis is a test in loop 3 Wed Feb 17 20:49:57 EET 2016\r\nThis is a test in loop 4 Wed Feb 17 20:50:02 EET 2016\r\nThis is a test in loop 5 Wed Feb 17 20:50:07 EET 2016\r\nThis is a test in loop 6 Wed Feb 17 20:50:12 EET 2016\r\nThis is a test in loop 7 Wed Feb 17 20:50:17 EET 2016\r\nThis is a test in loop 8 Wed Feb 17 20:50:22 EET 2016\r\nThis is a test in loop 9 Wed Feb 17 20:50:27 EET 2016\r\nThis is a test in loop 10 Wed Feb 17 20:50:32 EET 2016\r\n<\/pre>\n<p>You can change the\u00a0<strong>echo<\/strong>\u00a0and\u00a0<strong>date<\/strong>\u00a0commands with your own commands or script and change the sleep interval per your needs.<\/p>\n<h4>while loop Example<\/h4>\n<pre># while true; do echo -n \"This is a test of while loop\";date ; sleep 5; done<\/pre>\n<p>Here is sample output:<\/p>\n<pre>This is a test of while loopWed Feb 17 20:52:32 EET 2016\r\nThis is a test of while loopWed Feb 17 20:52:37 EET 2016\r\nThis is a test of while loopWed Feb 17 20:52:42 EET 2016\r\nThis is a test of while loopWed Feb 17 20:52:47 EET 2016\r\nThis is a test of while loopWed Feb 17 20:52:52 EET 2016\r\nThis is a test of while loopWed Feb 17 20:52:57 EET 2016\r\n<\/pre>\n<p>The above\u00a0command will run until it is\u00a0<a href=\"https:\/\/www.tecmint.com\/find-and-kill-running-processes-pid-in-linux\/\" target=\"_blank\" rel=\"noopener\">either killed or interrupted by the user<\/a>. It can come in handy if you need to run a command running in the background and you don\u2019t want to count on\u00a0<strong>cron<\/strong>.<\/p>\n<p><strong>Important<\/strong>: When using the above methods, it is highly recommend that you set interval long enough to give enough time of your command to finish running, before the next execution.<\/p>\n<h3>Conclusion<\/h3>\n<p>The samples in this tutorial are useful, but are not meant to completely replace the\u00a0<a href=\"https:\/\/www.tecmint.com\/11-cron-scheduling-task-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cron utility<\/a>. It is up to you to find which one works better for you, but if we have to separate the usage of both techniques, I would say this:<\/p>\n<ol>\n<li>Use cron when you need to run commands periodically even after system reboots.<\/li>\n<li>Use the methods explained in this tutorial for programs\/scripts that are meant to run within the current user session.<\/li>\n<\/ol>\n<p>As always if you have any questions or comments, do not hesitate to submit them in the comment section below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/run-repeat-linux-command-every-x-seconds\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A system administrator often needs to run a command repeatedly in a certain periods of time. Often such tasks can be easily completed with simple\u00a0cron\u00a0commands. In most of the cases \u00a0this should work, but the shortest\u00a0period which you can run cron command is every 1 minute. Believe it or not, in many cases this is &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-run-or-repeat-a-linux-command-every-x-seconds-forever\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Run or Repeat a Linux Command Every X Seconds Forever&#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-13179","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\/13179","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=13179"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13179\/revisions"}],"predecessor-version":[{"id":13181,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13179\/revisions\/13181"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}