Repeat a command every x interval of time in terminal?How to run command every 15min?How do I create a script to restart a service every two hours?constantly monitor a process and display CPU usageHow to execute a program periodically? & disply difeerence in outputRun a program X timesHow can I run a script when the power supply is plugged-in or -out?How to make two directories identical?Is it important to install disk monitor on linux?watch with arp produce no outputxdotool How do I automate click to click 30 times each delayed by 30 secs then repeat again hourly?Logging every time a command is runHow can I get a variable from terminal to use it in my script?Run terminal command (python command) at start upTerminal Runs SSH Every TimeHow do i write a terminal script to setup ubuntu for me instead of spending days every time?Why does sudo require a password the second time in a bash script every time?From a bash script, send commands to a terminal windowHow to make a command not run as sudo in script“bash: warning” every time starting a terminalBash script to Run a command to X files at a time

Have researchers managed to "reverse time"? If so, what does that mean for physics?

Did Ender ever learn that he killed Stilson and/or Bonzo?

SOQL: Populate a Literal List in WHERE IN Clause

Official degrees of earth’s rotation per day

Creature kill and resurrect effects on the stack interaction?

PTIJ: Who should I vote for? (21st Knesset Edition)

Provisioning profile doesn't include the application-identifier and keychain-access-groups entitlements

Python if-else code style for reduced code for rounding floats

Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?

Define, (actually define) the "stability" and "energy" of a compound

Co-worker team leader wants to inject his friend's awful software into our development. What should I say to our common boss?

Are ETF trackers fundamentally better than individual stocks?

If I can solve Sudoku can I solve Travelling Salesman Problem(TSP)? If yes, how?

How to read the value of this capacitor?

Brexit - No Deal Rejection

How to create the Curved texte?

How to explain that I do not want to visit a country due to personal safety concern?

Happy pi day, everyone!

How to use deus ex machina safely?

Min function accepting varying number of arguments in C++17

Why is the President allowed to veto a cancellation of emergency powers?

how to write formula in word in latex

How to make healing in an exploration game interesting

In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf



Repeat a command every x interval of time in terminal?


How to run command every 15min?How do I create a script to restart a service every two hours?constantly monitor a process and display CPU usageHow to execute a program periodically? & disply difeerence in outputRun a program X timesHow can I run a script when the power supply is plugged-in or -out?How to make two directories identical?Is it important to install disk monitor on linux?watch with arp produce no outputxdotool How do I automate click to click 30 times each delayed by 30 secs then repeat again hourly?Logging every time a command is runHow can I get a variable from terminal to use it in my script?Run terminal command (python command) at start upTerminal Runs SSH Every TimeHow do i write a terminal script to setup ubuntu for me instead of spending days every time?Why does sudo require a password the second time in a bash script every time?From a bash script, send commands to a terminal windowHow to make a command not run as sudo in script“bash: warning” every time starting a terminalBash script to Run a command to X files at a time













129















How can I repeat a command every interval of time , so that it will allow me to run commands for checking or monitoring directories ?



There is no need for a script, i need just a simple command to be executed in terminal.










share|improve this question




























    129















    How can I repeat a command every interval of time , so that it will allow me to run commands for checking or monitoring directories ?



    There is no need for a script, i need just a simple command to be executed in terminal.










    share|improve this question


























      129












      129








      129


      69






      How can I repeat a command every interval of time , so that it will allow me to run commands for checking or monitoring directories ?



      There is no need for a script, i need just a simple command to be executed in terminal.










      share|improve this question
















      How can I repeat a command every interval of time , so that it will allow me to run commands for checking or monitoring directories ?



      There is no need for a script, i need just a simple command to be executed in terminal.







      command-line bash scripts monitoring






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 27 '16 at 7:59









      muru

      1




      1










      asked Mar 6 '14 at 15:52







      user239745



























          9 Answers
          9






          active

          oldest

          votes


















          201














          You can use watch command, watch is used to run any designated command at regular intervals.



          Open Terminal and type:



          watch -n x <your command>


          change x to be the time in seconds you want.



          For more help using the watch command and its options, run man watch or visit this Link.



          For example : the following will list, every 60s, on the same Terminal, the contents of the Desktop directory so that you can know if any changes took place:



          watch -n 60 ls -l ~/Desktop





          share|improve this answer




















          • 33





            +1 but be careful when using expansions. For example, try the difference between watch -n 1 'echo $COLUMNS' and watch -n 1 echo $COLUMNS when resizing your terminal - the former is expanded every second, but the latter is expanded only once before watch starts.

            – l0b0
            Mar 6 '14 at 16:06












          • It Works like a charm ! Ty nux

            – user239745
            Mar 6 '14 at 16:28











          • Problem I have with this solution is that you can't run watch as a process and just leave it running in the background (for example with &disown)

            – Cestarian
            May 13 '15 at 2:06






          • 1





            Is there any way to use watch with "history enabled" type command? I love using watch, but sometimes I'd prefer to see a log of previous executions as well, instead of just the last one. And yes, I know I can use scripting (while true) to accomplish this, but using the watch utilitiy is so much cleaner!

            – rinogo
            Sep 14 '15 at 18:44












          • this worked for simpler commands but with pipelined commands chaining this didn't work for me.. following was the command I tried =>cat api.log | grep 'calling' | wc -l

            – Sudip Bhandari
            Sep 29 '16 at 7:43



















          83














          You can also use this command in terminal, apart from nux's answer :



          while true; do <your_command>; sleep <interval_in_seconds>; done


          Example



          while true; do ls; sleep 2; done


          This command will print output of ls at an interval of 2 sec.



          Use Ctrl+C to stop the process.



          There is few drawbacks of watch



          • It can not use any aliased commands.

          • If the output of any command is quite long, scrolling does not work properly.

          • There is some trouble to set maximum time interval beyond certain value.


          • watch will interpret ANSI color sequences passing escape characters using -c or --color option. For example output of pygmentize will work but it will fail for ls --color=auto.

          In the above circumstances this may appear as a better option.






          share|improve this answer

























          • watch exists for that, this is a bit useless I would say

            – Bruno Pereira
            Mar 6 '14 at 16:36






          • 14





            I am not claiming this answer is to be used at first place. watch is good in most cases. That is why I mentioned "apart from nux's answer" at the beginning. But there are few problems with watch for example One can not use any aliased commands with watch. Take for example ll which is aliased to ls -laF but can not be used with watch. Also in case if the output of any command is quite long you will be in trouble in scrolling using watch. In these few special cases this answer may appear a better option.

            – souravc
            Mar 6 '14 at 17:25











          • @souravc My version of watch at least allows the -c or --color options for colorized output.

            – ikdc
            Mar 7 '14 at 1:57






          • 8





            while sleep x is better - it's easier to kill.

            – d33tah
            Mar 8 '14 at 15:43






          • 2





            This is a nice alternative and, unlike watch, it keeps the command history.

            – adelriosantiago
            Feb 14 '17 at 18:31


















          34














          Just wanted to pitch in to souravc and nux's answers:



          1. While watch will work perfectly on Ubuntu, you might want to avoid that if you want your "Unix-fu" to be pure - on FreeBSD for example, watch is a command to "snoop on another tty line".


          2. while true; do command; sleep SECONDS; done also has a caveat - your command might be harder to kill using CTR+C. You might want to prefer while sleep SECONDS; do command; done - it's not only shorter, but also easier to interrupt. The caveat is that it will first sleep, then run your command, so you'll need to wait some SECONDS before the first occurrence of the command will happen.





          share|improve this answer




















          • 2





            Hopefully it's acceptable as an answer instead of a comment - I wanted to show another solution here and commenting would actually give me less attention.

            – d33tah
            Mar 8 '14 at 15:52











          • Why exactly does it matter where you put sleep in the while loop? I couldn't find any difference, Ctrl+C broke the loop instantly no matter what.

            – dessert
            Nov 17 '17 at 8:03











          • @dessert: depends on what you're trying to break out from I guess. Normally, ctrl+c would just kill your command and sleep and only break if you kill true.

            – d33tah
            Dec 11 '17 at 17:08


















          11














          Sounds like the ideal task for the cron daemon which allows for running periodic commands. Run the crontab -e command to start editing your user's cron configuration. Its format is documented in crontab(5). Basically you have five time-related, space-separated fields followed by a command:



          The time and date fields are:

          field allowed values
          ----- --------------
          minute 0-59
          hour 0-23
          day of month 1-31
          month 1-12 (or names, see below)
          day of week 0-7 (0 or 7 is Sunday, or use names)


          For example, if you would like to run a Python script on every Tuesday, 11 AM:



          0 11 * * 1 python ~/yourscript.py


          There are also some special names that replace the time, like @reboot. Very helpful if you need to create a temporary directory. From my crontab (listed with crontab -l):



          # Creates a temporary directory for ~/.distcc at boot
          @reboot ln -sfn "$(mktemp -d "/tmp/distcc.XXXXXXXX")" "$HOME/.distcc"





          share|improve this answer


















          • 4





            The question asks how to run something periodically in the terminal. cron runs behind the scenes rather than in the terminal

            – northern-bradley
            Dec 15 '14 at 19:22


















          5














          If you are monitoring the file system, then inotifywait is brilliant and certainly adds less load on your system.



          Example :



          In 1st terminal type this command :



          $ inotifywait .


          Then in 2nd terminal, any command that affects the current directory,



          $ touch newfile


          Then in original terminal inotifywait will wake up and report the event



          ./ CREATE newfile2


          Or in a loop



          $ while true ; do inotifywait . ; done
          Setting up watches.
          Watches established.
          ./ OPEN newfile2
          Setting up watches.
          Watches established.
          ./ OPEN newfile2
          Setting up watches.
          Watches established.
          ./ DELETE newfile
          Setting up watches.
          Watches established.
          ./ CREATE,ISDIR newdir
          Setting up watches.
          Watches established.





          share|improve this answer

























          • the user told you , no script , and maybe he dont want to monitor anything

            – nux
            Mar 6 '14 at 16:36






          • 3





            I didn't tell him to write a script, I suggested that if they are looping inorder to watch for particular filesystem event, then inotifywait is useful, and uses less resources than repeating a command. I often run several commands on a command line eg grep something InALogFile|less is that a script ?

            – X Tian
            Mar 6 '14 at 16:43












          • its a good answer , try to edit it to look more simple .

            – nux
            Mar 6 '14 at 16:45






          • 3





            What could be simpler than . I can't leave out the command.

            – X Tian
            Mar 6 '14 at 16:48











          • Thanks @XTian, a great command. I also now saw in the man page that you can add -m to continually monitor without a loop.

            – yoniLavi
            Jul 21 '14 at 16:06


















          4














          You can create your own repeat command doing the following steps; credits here:



          First, open your .bash_aliases file:



          $ xdg-open ~/.bash-aliases


          Second, paste these lines at the bottom of the file and save:



          repeat() 
          n=$1
          shift
          while [ $(( n -= 1 )) -ge 0 ]
          do
          "$@"
          done



          Third, either close and open again your terminal, or type:



          $ source ~/.bash_aliases


          Et voilà ! You can now use it like this:



          $ repeat 5 echo Hello World !!!


          or



          $ repeat 5 ./myscript.sh





          share|improve this answer























          • there is a small typo in the line xdg-open ~/.bash-aliases. it should be: xdg-open ~/.bash_aliases (ie: underscore)

            – ssinfod
            Feb 4 '18 at 1:21



















          4














          you can use crontab. run the command crontab -e and open it with your preferred text editor, then add this line



          */10 * * * * /path-to-your-command


          This will run your command every 10 minutes



          * */4 * * * /path-to-your-command


          This will run your command every 4 hours



          Another possible solution



          $ ..some command...; for i in $(seq X); do $cmd; sleep Y; done


          X number of times to repeat.



          Y time to wait to repeat.



          Example :



          $ echo; for i in $(seq 5); do $cmd "This is echo number: $i"; sleep 1;done





          share|improve this answer

























          • Why is this an improvement? You just added an extra, needless, step by saving the command as a variable. The only things this does is i) make it longer to type ii) forces you to use only simple commands, no pipes or redirects etc.

            – terdon
            Jul 3 '14 at 11:52











          • Remove the extra variable

            – Maythux
            May 14 '15 at 11:24


















          2














          Another concern with the "watch" approach proposed above is that it does display the result only when the process is done.
          "date;sleep 58;date" will display the 2 dates only after 59 seconds... If you start something running for 4 minutes, that display slowly multiple pages of content, you will not really see it.



          On the other hand, the concern with the "while" approach is that it doesn't take the task duration into consideration.



          while true; do script_that_take_between_10s_to_50s.sh; sleep 50; done


          With this, the script will run sometime every minutes, sometime might take 1m40. So even if a cron will be able to run it every minutes, here, it will not.



          So to see the output on the shell as it's generated and wait for the exact request time, you need to look at the time before, and after, and loop with the while.



          Something like:



          while ( true ); do
          echo Date starting `date`
          before=`date +%s`
          sleep `echo $(( ( RANDOM % 30 ) + 1 ))`
          echo Before waiting `date`
          after=`date +%s`
          DELAY=`echo "60-($after-$before)" | bc`
          sleep $DELAY
          echo Done waiting `date`
          done


          This will output this:



          As you can see, the command runs every minutes:



          Date starting Mon Dec 14 15:49:34 EST 2015
          Before waiting Mon Dec 14 15:49:52 EST 2015
          Done waiting Mon Dec 14 15:50:34 EST 2015

          Date starting Mon Dec 14 15:50:34 EST 2015
          Before waiting Mon Dec 14 15:50:39 EST 2015
          Done waiting Mon Dec 14 15:51:34 EST 2015


          So just replace the "sleep echo $(( ( RANDOM % 30 ) + 1 ))" command with what ever you want and that will be run, on the terminal/shell, exactly every minute. If you want another schedule, just change the "60" seconds with what ever you need.



          Shorter version without the debug lines:



          while ( true ); do
          before=`date +%s`
          sleep `echo $(( ( RANDOM % 30 ) + 1 ))` # Place you command here
          after=`date +%s`
          DELAY=`echo "60-($after-$before)" | bc`
          sleep $DELAY
          done





          share|improve this answer























          • Replying to myself... If your command takes more than the delay you configure, $DELAY will get a negative value and the sleep command will fail so the script will restart right away. Need to be aware of that.

            – jmspaggi
            Dec 14 '15 at 21:06


















          0















          run below script accordingly.

          #!/bin/bash
          while true
          do
          /usr/bin/mkdir /root/sec/$(date "+%T")
          sleep 2
          done





          share|improve this answer






















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "89"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f430382%2frepeat-a-command-every-x-interval-of-time-in-terminal%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown
























            9 Answers
            9






            active

            oldest

            votes








            9 Answers
            9






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            201














            You can use watch command, watch is used to run any designated command at regular intervals.



            Open Terminal and type:



            watch -n x <your command>


            change x to be the time in seconds you want.



            For more help using the watch command and its options, run man watch or visit this Link.



            For example : the following will list, every 60s, on the same Terminal, the contents of the Desktop directory so that you can know if any changes took place:



            watch -n 60 ls -l ~/Desktop





            share|improve this answer




















            • 33





              +1 but be careful when using expansions. For example, try the difference between watch -n 1 'echo $COLUMNS' and watch -n 1 echo $COLUMNS when resizing your terminal - the former is expanded every second, but the latter is expanded only once before watch starts.

              – l0b0
              Mar 6 '14 at 16:06












            • It Works like a charm ! Ty nux

              – user239745
              Mar 6 '14 at 16:28











            • Problem I have with this solution is that you can't run watch as a process and just leave it running in the background (for example with &disown)

              – Cestarian
              May 13 '15 at 2:06






            • 1





              Is there any way to use watch with "history enabled" type command? I love using watch, but sometimes I'd prefer to see a log of previous executions as well, instead of just the last one. And yes, I know I can use scripting (while true) to accomplish this, but using the watch utilitiy is so much cleaner!

              – rinogo
              Sep 14 '15 at 18:44












            • this worked for simpler commands but with pipelined commands chaining this didn't work for me.. following was the command I tried =>cat api.log | grep 'calling' | wc -l

              – Sudip Bhandari
              Sep 29 '16 at 7:43
















            201














            You can use watch command, watch is used to run any designated command at regular intervals.



            Open Terminal and type:



            watch -n x <your command>


            change x to be the time in seconds you want.



            For more help using the watch command and its options, run man watch or visit this Link.



            For example : the following will list, every 60s, on the same Terminal, the contents of the Desktop directory so that you can know if any changes took place:



            watch -n 60 ls -l ~/Desktop





            share|improve this answer




















            • 33





              +1 but be careful when using expansions. For example, try the difference between watch -n 1 'echo $COLUMNS' and watch -n 1 echo $COLUMNS when resizing your terminal - the former is expanded every second, but the latter is expanded only once before watch starts.

              – l0b0
              Mar 6 '14 at 16:06












            • It Works like a charm ! Ty nux

              – user239745
              Mar 6 '14 at 16:28











            • Problem I have with this solution is that you can't run watch as a process and just leave it running in the background (for example with &disown)

              – Cestarian
              May 13 '15 at 2:06






            • 1





              Is there any way to use watch with "history enabled" type command? I love using watch, but sometimes I'd prefer to see a log of previous executions as well, instead of just the last one. And yes, I know I can use scripting (while true) to accomplish this, but using the watch utilitiy is so much cleaner!

              – rinogo
              Sep 14 '15 at 18:44












            • this worked for simpler commands but with pipelined commands chaining this didn't work for me.. following was the command I tried =>cat api.log | grep 'calling' | wc -l

              – Sudip Bhandari
              Sep 29 '16 at 7:43














            201












            201








            201







            You can use watch command, watch is used to run any designated command at regular intervals.



            Open Terminal and type:



            watch -n x <your command>


            change x to be the time in seconds you want.



            For more help using the watch command and its options, run man watch or visit this Link.



            For example : the following will list, every 60s, on the same Terminal, the contents of the Desktop directory so that you can know if any changes took place:



            watch -n 60 ls -l ~/Desktop





            share|improve this answer















            You can use watch command, watch is used to run any designated command at regular intervals.



            Open Terminal and type:



            watch -n x <your command>


            change x to be the time in seconds you want.



            For more help using the watch command and its options, run man watch or visit this Link.



            For example : the following will list, every 60s, on the same Terminal, the contents of the Desktop directory so that you can know if any changes took place:



            watch -n 60 ls -l ~/Desktop






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 26 '16 at 16:31









            Brian Moths

            28628




            28628










            answered Mar 6 '14 at 15:54









            nuxnux

            23k3096117




            23k3096117







            • 33





              +1 but be careful when using expansions. For example, try the difference between watch -n 1 'echo $COLUMNS' and watch -n 1 echo $COLUMNS when resizing your terminal - the former is expanded every second, but the latter is expanded only once before watch starts.

              – l0b0
              Mar 6 '14 at 16:06












            • It Works like a charm ! Ty nux

              – user239745
              Mar 6 '14 at 16:28











            • Problem I have with this solution is that you can't run watch as a process and just leave it running in the background (for example with &disown)

              – Cestarian
              May 13 '15 at 2:06






            • 1





              Is there any way to use watch with "history enabled" type command? I love using watch, but sometimes I'd prefer to see a log of previous executions as well, instead of just the last one. And yes, I know I can use scripting (while true) to accomplish this, but using the watch utilitiy is so much cleaner!

              – rinogo
              Sep 14 '15 at 18:44












            • this worked for simpler commands but with pipelined commands chaining this didn't work for me.. following was the command I tried =>cat api.log | grep 'calling' | wc -l

              – Sudip Bhandari
              Sep 29 '16 at 7:43













            • 33





              +1 but be careful when using expansions. For example, try the difference between watch -n 1 'echo $COLUMNS' and watch -n 1 echo $COLUMNS when resizing your terminal - the former is expanded every second, but the latter is expanded only once before watch starts.

              – l0b0
              Mar 6 '14 at 16:06












            • It Works like a charm ! Ty nux

              – user239745
              Mar 6 '14 at 16:28











            • Problem I have with this solution is that you can't run watch as a process and just leave it running in the background (for example with &disown)

              – Cestarian
              May 13 '15 at 2:06






            • 1





              Is there any way to use watch with "history enabled" type command? I love using watch, but sometimes I'd prefer to see a log of previous executions as well, instead of just the last one. And yes, I know I can use scripting (while true) to accomplish this, but using the watch utilitiy is so much cleaner!

              – rinogo
              Sep 14 '15 at 18:44












            • this worked for simpler commands but with pipelined commands chaining this didn't work for me.. following was the command I tried =>cat api.log | grep 'calling' | wc -l

              – Sudip Bhandari
              Sep 29 '16 at 7:43








            33




            33





            +1 but be careful when using expansions. For example, try the difference between watch -n 1 'echo $COLUMNS' and watch -n 1 echo $COLUMNS when resizing your terminal - the former is expanded every second, but the latter is expanded only once before watch starts.

            – l0b0
            Mar 6 '14 at 16:06






            +1 but be careful when using expansions. For example, try the difference between watch -n 1 'echo $COLUMNS' and watch -n 1 echo $COLUMNS when resizing your terminal - the former is expanded every second, but the latter is expanded only once before watch starts.

            – l0b0
            Mar 6 '14 at 16:06














            It Works like a charm ! Ty nux

            – user239745
            Mar 6 '14 at 16:28





            It Works like a charm ! Ty nux

            – user239745
            Mar 6 '14 at 16:28













            Problem I have with this solution is that you can't run watch as a process and just leave it running in the background (for example with &disown)

            – Cestarian
            May 13 '15 at 2:06





            Problem I have with this solution is that you can't run watch as a process and just leave it running in the background (for example with &disown)

            – Cestarian
            May 13 '15 at 2:06




            1




            1





            Is there any way to use watch with "history enabled" type command? I love using watch, but sometimes I'd prefer to see a log of previous executions as well, instead of just the last one. And yes, I know I can use scripting (while true) to accomplish this, but using the watch utilitiy is so much cleaner!

            – rinogo
            Sep 14 '15 at 18:44






            Is there any way to use watch with "history enabled" type command? I love using watch, but sometimes I'd prefer to see a log of previous executions as well, instead of just the last one. And yes, I know I can use scripting (while true) to accomplish this, but using the watch utilitiy is so much cleaner!

            – rinogo
            Sep 14 '15 at 18:44














            this worked for simpler commands but with pipelined commands chaining this didn't work for me.. following was the command I tried =>cat api.log | grep 'calling' | wc -l

            – Sudip Bhandari
            Sep 29 '16 at 7:43






            this worked for simpler commands but with pipelined commands chaining this didn't work for me.. following was the command I tried =>cat api.log | grep 'calling' | wc -l

            – Sudip Bhandari
            Sep 29 '16 at 7:43














            83














            You can also use this command in terminal, apart from nux's answer :



            while true; do <your_command>; sleep <interval_in_seconds>; done


            Example



            while true; do ls; sleep 2; done


            This command will print output of ls at an interval of 2 sec.



            Use Ctrl+C to stop the process.



            There is few drawbacks of watch



            • It can not use any aliased commands.

            • If the output of any command is quite long, scrolling does not work properly.

            • There is some trouble to set maximum time interval beyond certain value.


            • watch will interpret ANSI color sequences passing escape characters using -c or --color option. For example output of pygmentize will work but it will fail for ls --color=auto.

            In the above circumstances this may appear as a better option.






            share|improve this answer

























            • watch exists for that, this is a bit useless I would say

              – Bruno Pereira
              Mar 6 '14 at 16:36






            • 14





              I am not claiming this answer is to be used at first place. watch is good in most cases. That is why I mentioned "apart from nux's answer" at the beginning. But there are few problems with watch for example One can not use any aliased commands with watch. Take for example ll which is aliased to ls -laF but can not be used with watch. Also in case if the output of any command is quite long you will be in trouble in scrolling using watch. In these few special cases this answer may appear a better option.

              – souravc
              Mar 6 '14 at 17:25











            • @souravc My version of watch at least allows the -c or --color options for colorized output.

              – ikdc
              Mar 7 '14 at 1:57






            • 8





              while sleep x is better - it's easier to kill.

              – d33tah
              Mar 8 '14 at 15:43






            • 2





              This is a nice alternative and, unlike watch, it keeps the command history.

              – adelriosantiago
              Feb 14 '17 at 18:31















            83














            You can also use this command in terminal, apart from nux's answer :



            while true; do <your_command>; sleep <interval_in_seconds>; done


            Example



            while true; do ls; sleep 2; done


            This command will print output of ls at an interval of 2 sec.



            Use Ctrl+C to stop the process.



            There is few drawbacks of watch



            • It can not use any aliased commands.

            • If the output of any command is quite long, scrolling does not work properly.

            • There is some trouble to set maximum time interval beyond certain value.


            • watch will interpret ANSI color sequences passing escape characters using -c or --color option. For example output of pygmentize will work but it will fail for ls --color=auto.

            In the above circumstances this may appear as a better option.






            share|improve this answer

























            • watch exists for that, this is a bit useless I would say

              – Bruno Pereira
              Mar 6 '14 at 16:36






            • 14





              I am not claiming this answer is to be used at first place. watch is good in most cases. That is why I mentioned "apart from nux's answer" at the beginning. But there are few problems with watch for example One can not use any aliased commands with watch. Take for example ll which is aliased to ls -laF but can not be used with watch. Also in case if the output of any command is quite long you will be in trouble in scrolling using watch. In these few special cases this answer may appear a better option.

              – souravc
              Mar 6 '14 at 17:25











            • @souravc My version of watch at least allows the -c or --color options for colorized output.

              – ikdc
              Mar 7 '14 at 1:57






            • 8





              while sleep x is better - it's easier to kill.

              – d33tah
              Mar 8 '14 at 15:43






            • 2





              This is a nice alternative and, unlike watch, it keeps the command history.

              – adelriosantiago
              Feb 14 '17 at 18:31













            83












            83








            83







            You can also use this command in terminal, apart from nux's answer :



            while true; do <your_command>; sleep <interval_in_seconds>; done


            Example



            while true; do ls; sleep 2; done


            This command will print output of ls at an interval of 2 sec.



            Use Ctrl+C to stop the process.



            There is few drawbacks of watch



            • It can not use any aliased commands.

            • If the output of any command is quite long, scrolling does not work properly.

            • There is some trouble to set maximum time interval beyond certain value.


            • watch will interpret ANSI color sequences passing escape characters using -c or --color option. For example output of pygmentize will work but it will fail for ls --color=auto.

            In the above circumstances this may appear as a better option.






            share|improve this answer















            You can also use this command in terminal, apart from nux's answer :



            while true; do <your_command>; sleep <interval_in_seconds>; done


            Example



            while true; do ls; sleep 2; done


            This command will print output of ls at an interval of 2 sec.



            Use Ctrl+C to stop the process.



            There is few drawbacks of watch



            • It can not use any aliased commands.

            • If the output of any command is quite long, scrolling does not work properly.

            • There is some trouble to set maximum time interval beyond certain value.


            • watch will interpret ANSI color sequences passing escape characters using -c or --color option. For example output of pygmentize will work but it will fail for ls --color=auto.

            In the above circumstances this may appear as a better option.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 13 '17 at 12:37









            Community

            1




            1










            answered Mar 6 '14 at 16:00









            souravcsouravc

            27.5k1377108




            27.5k1377108












            • watch exists for that, this is a bit useless I would say

              – Bruno Pereira
              Mar 6 '14 at 16:36






            • 14





              I am not claiming this answer is to be used at first place. watch is good in most cases. That is why I mentioned "apart from nux's answer" at the beginning. But there are few problems with watch for example One can not use any aliased commands with watch. Take for example ll which is aliased to ls -laF but can not be used with watch. Also in case if the output of any command is quite long you will be in trouble in scrolling using watch. In these few special cases this answer may appear a better option.

              – souravc
              Mar 6 '14 at 17:25











            • @souravc My version of watch at least allows the -c or --color options for colorized output.

              – ikdc
              Mar 7 '14 at 1:57






            • 8





              while sleep x is better - it's easier to kill.

              – d33tah
              Mar 8 '14 at 15:43






            • 2





              This is a nice alternative and, unlike watch, it keeps the command history.

              – adelriosantiago
              Feb 14 '17 at 18:31

















            • watch exists for that, this is a bit useless I would say

              – Bruno Pereira
              Mar 6 '14 at 16:36






            • 14





              I am not claiming this answer is to be used at first place. watch is good in most cases. That is why I mentioned "apart from nux's answer" at the beginning. But there are few problems with watch for example One can not use any aliased commands with watch. Take for example ll which is aliased to ls -laF but can not be used with watch. Also in case if the output of any command is quite long you will be in trouble in scrolling using watch. In these few special cases this answer may appear a better option.

              – souravc
              Mar 6 '14 at 17:25











            • @souravc My version of watch at least allows the -c or --color options for colorized output.

              – ikdc
              Mar 7 '14 at 1:57






            • 8





              while sleep x is better - it's easier to kill.

              – d33tah
              Mar 8 '14 at 15:43






            • 2





              This is a nice alternative and, unlike watch, it keeps the command history.

              – adelriosantiago
              Feb 14 '17 at 18:31
















            watch exists for that, this is a bit useless I would say

            – Bruno Pereira
            Mar 6 '14 at 16:36





            watch exists for that, this is a bit useless I would say

            – Bruno Pereira
            Mar 6 '14 at 16:36




            14




            14





            I am not claiming this answer is to be used at first place. watch is good in most cases. That is why I mentioned "apart from nux's answer" at the beginning. But there are few problems with watch for example One can not use any aliased commands with watch. Take for example ll which is aliased to ls -laF but can not be used with watch. Also in case if the output of any command is quite long you will be in trouble in scrolling using watch. In these few special cases this answer may appear a better option.

            – souravc
            Mar 6 '14 at 17:25





            I am not claiming this answer is to be used at first place. watch is good in most cases. That is why I mentioned "apart from nux's answer" at the beginning. But there are few problems with watch for example One can not use any aliased commands with watch. Take for example ll which is aliased to ls -laF but can not be used with watch. Also in case if the output of any command is quite long you will be in trouble in scrolling using watch. In these few special cases this answer may appear a better option.

            – souravc
            Mar 6 '14 at 17:25













            @souravc My version of watch at least allows the -c or --color options for colorized output.

            – ikdc
            Mar 7 '14 at 1:57





            @souravc My version of watch at least allows the -c or --color options for colorized output.

            – ikdc
            Mar 7 '14 at 1:57




            8




            8





            while sleep x is better - it's easier to kill.

            – d33tah
            Mar 8 '14 at 15:43





            while sleep x is better - it's easier to kill.

            – d33tah
            Mar 8 '14 at 15:43




            2




            2





            This is a nice alternative and, unlike watch, it keeps the command history.

            – adelriosantiago
            Feb 14 '17 at 18:31





            This is a nice alternative and, unlike watch, it keeps the command history.

            – adelriosantiago
            Feb 14 '17 at 18:31











            34














            Just wanted to pitch in to souravc and nux's answers:



            1. While watch will work perfectly on Ubuntu, you might want to avoid that if you want your "Unix-fu" to be pure - on FreeBSD for example, watch is a command to "snoop on another tty line".


            2. while true; do command; sleep SECONDS; done also has a caveat - your command might be harder to kill using CTR+C. You might want to prefer while sleep SECONDS; do command; done - it's not only shorter, but also easier to interrupt. The caveat is that it will first sleep, then run your command, so you'll need to wait some SECONDS before the first occurrence of the command will happen.





            share|improve this answer




















            • 2





              Hopefully it's acceptable as an answer instead of a comment - I wanted to show another solution here and commenting would actually give me less attention.

              – d33tah
              Mar 8 '14 at 15:52











            • Why exactly does it matter where you put sleep in the while loop? I couldn't find any difference, Ctrl+C broke the loop instantly no matter what.

              – dessert
              Nov 17 '17 at 8:03











            • @dessert: depends on what you're trying to break out from I guess. Normally, ctrl+c would just kill your command and sleep and only break if you kill true.

              – d33tah
              Dec 11 '17 at 17:08















            34














            Just wanted to pitch in to souravc and nux's answers:



            1. While watch will work perfectly on Ubuntu, you might want to avoid that if you want your "Unix-fu" to be pure - on FreeBSD for example, watch is a command to "snoop on another tty line".


            2. while true; do command; sleep SECONDS; done also has a caveat - your command might be harder to kill using CTR+C. You might want to prefer while sleep SECONDS; do command; done - it's not only shorter, but also easier to interrupt. The caveat is that it will first sleep, then run your command, so you'll need to wait some SECONDS before the first occurrence of the command will happen.





            share|improve this answer




















            • 2





              Hopefully it's acceptable as an answer instead of a comment - I wanted to show another solution here and commenting would actually give me less attention.

              – d33tah
              Mar 8 '14 at 15:52











            • Why exactly does it matter where you put sleep in the while loop? I couldn't find any difference, Ctrl+C broke the loop instantly no matter what.

              – dessert
              Nov 17 '17 at 8:03











            • @dessert: depends on what you're trying to break out from I guess. Normally, ctrl+c would just kill your command and sleep and only break if you kill true.

              – d33tah
              Dec 11 '17 at 17:08













            34












            34








            34







            Just wanted to pitch in to souravc and nux's answers:



            1. While watch will work perfectly on Ubuntu, you might want to avoid that if you want your "Unix-fu" to be pure - on FreeBSD for example, watch is a command to "snoop on another tty line".


            2. while true; do command; sleep SECONDS; done also has a caveat - your command might be harder to kill using CTR+C. You might want to prefer while sleep SECONDS; do command; done - it's not only shorter, but also easier to interrupt. The caveat is that it will first sleep, then run your command, so you'll need to wait some SECONDS before the first occurrence of the command will happen.





            share|improve this answer















            Just wanted to pitch in to souravc and nux's answers:



            1. While watch will work perfectly on Ubuntu, you might want to avoid that if you want your "Unix-fu" to be pure - on FreeBSD for example, watch is a command to "snoop on another tty line".


            2. while true; do command; sleep SECONDS; done also has a caveat - your command might be harder to kill using CTR+C. You might want to prefer while sleep SECONDS; do command; done - it's not only shorter, but also easier to interrupt. The caveat is that it will first sleep, then run your command, so you'll need to wait some SECONDS before the first occurrence of the command will happen.






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 6 '15 at 19:09

























            answered Mar 8 '14 at 15:51









            d33tahd33tah

            450310




            450310







            • 2





              Hopefully it's acceptable as an answer instead of a comment - I wanted to show another solution here and commenting would actually give me less attention.

              – d33tah
              Mar 8 '14 at 15:52











            • Why exactly does it matter where you put sleep in the while loop? I couldn't find any difference, Ctrl+C broke the loop instantly no matter what.

              – dessert
              Nov 17 '17 at 8:03











            • @dessert: depends on what you're trying to break out from I guess. Normally, ctrl+c would just kill your command and sleep and only break if you kill true.

              – d33tah
              Dec 11 '17 at 17:08












            • 2





              Hopefully it's acceptable as an answer instead of a comment - I wanted to show another solution here and commenting would actually give me less attention.

              – d33tah
              Mar 8 '14 at 15:52











            • Why exactly does it matter where you put sleep in the while loop? I couldn't find any difference, Ctrl+C broke the loop instantly no matter what.

              – dessert
              Nov 17 '17 at 8:03











            • @dessert: depends on what you're trying to break out from I guess. Normally, ctrl+c would just kill your command and sleep and only break if you kill true.

              – d33tah
              Dec 11 '17 at 17:08







            2




            2





            Hopefully it's acceptable as an answer instead of a comment - I wanted to show another solution here and commenting would actually give me less attention.

            – d33tah
            Mar 8 '14 at 15:52





            Hopefully it's acceptable as an answer instead of a comment - I wanted to show another solution here and commenting would actually give me less attention.

            – d33tah
            Mar 8 '14 at 15:52













            Why exactly does it matter where you put sleep in the while loop? I couldn't find any difference, Ctrl+C broke the loop instantly no matter what.

            – dessert
            Nov 17 '17 at 8:03





            Why exactly does it matter where you put sleep in the while loop? I couldn't find any difference, Ctrl+C broke the loop instantly no matter what.

            – dessert
            Nov 17 '17 at 8:03













            @dessert: depends on what you're trying to break out from I guess. Normally, ctrl+c would just kill your command and sleep and only break if you kill true.

            – d33tah
            Dec 11 '17 at 17:08





            @dessert: depends on what you're trying to break out from I guess. Normally, ctrl+c would just kill your command and sleep and only break if you kill true.

            – d33tah
            Dec 11 '17 at 17:08











            11














            Sounds like the ideal task for the cron daemon which allows for running periodic commands. Run the crontab -e command to start editing your user's cron configuration. Its format is documented in crontab(5). Basically you have five time-related, space-separated fields followed by a command:



            The time and date fields are:

            field allowed values
            ----- --------------
            minute 0-59
            hour 0-23
            day of month 1-31
            month 1-12 (or names, see below)
            day of week 0-7 (0 or 7 is Sunday, or use names)


            For example, if you would like to run a Python script on every Tuesday, 11 AM:



            0 11 * * 1 python ~/yourscript.py


            There are also some special names that replace the time, like @reboot. Very helpful if you need to create a temporary directory. From my crontab (listed with crontab -l):



            # Creates a temporary directory for ~/.distcc at boot
            @reboot ln -sfn "$(mktemp -d "/tmp/distcc.XXXXXXXX")" "$HOME/.distcc"





            share|improve this answer


















            • 4





              The question asks how to run something periodically in the terminal. cron runs behind the scenes rather than in the terminal

              – northern-bradley
              Dec 15 '14 at 19:22















            11














            Sounds like the ideal task for the cron daemon which allows for running periodic commands. Run the crontab -e command to start editing your user's cron configuration. Its format is documented in crontab(5). Basically you have five time-related, space-separated fields followed by a command:



            The time and date fields are:

            field allowed values
            ----- --------------
            minute 0-59
            hour 0-23
            day of month 1-31
            month 1-12 (or names, see below)
            day of week 0-7 (0 or 7 is Sunday, or use names)


            For example, if you would like to run a Python script on every Tuesday, 11 AM:



            0 11 * * 1 python ~/yourscript.py


            There are also some special names that replace the time, like @reboot. Very helpful if you need to create a temporary directory. From my crontab (listed with crontab -l):



            # Creates a temporary directory for ~/.distcc at boot
            @reboot ln -sfn "$(mktemp -d "/tmp/distcc.XXXXXXXX")" "$HOME/.distcc"





            share|improve this answer


















            • 4





              The question asks how to run something periodically in the terminal. cron runs behind the scenes rather than in the terminal

              – northern-bradley
              Dec 15 '14 at 19:22













            11












            11








            11







            Sounds like the ideal task for the cron daemon which allows for running periodic commands. Run the crontab -e command to start editing your user's cron configuration. Its format is documented in crontab(5). Basically you have five time-related, space-separated fields followed by a command:



            The time and date fields are:

            field allowed values
            ----- --------------
            minute 0-59
            hour 0-23
            day of month 1-31
            month 1-12 (or names, see below)
            day of week 0-7 (0 or 7 is Sunday, or use names)


            For example, if you would like to run a Python script on every Tuesday, 11 AM:



            0 11 * * 1 python ~/yourscript.py


            There are also some special names that replace the time, like @reboot. Very helpful if you need to create a temporary directory. From my crontab (listed with crontab -l):



            # Creates a temporary directory for ~/.distcc at boot
            @reboot ln -sfn "$(mktemp -d "/tmp/distcc.XXXXXXXX")" "$HOME/.distcc"





            share|improve this answer













            Sounds like the ideal task for the cron daemon which allows for running periodic commands. Run the crontab -e command to start editing your user's cron configuration. Its format is documented in crontab(5). Basically you have five time-related, space-separated fields followed by a command:



            The time and date fields are:

            field allowed values
            ----- --------------
            minute 0-59
            hour 0-23
            day of month 1-31
            month 1-12 (or names, see below)
            day of week 0-7 (0 or 7 is Sunday, or use names)


            For example, if you would like to run a Python script on every Tuesday, 11 AM:



            0 11 * * 1 python ~/yourscript.py


            There are also some special names that replace the time, like @reboot. Very helpful if you need to create a temporary directory. From my crontab (listed with crontab -l):



            # Creates a temporary directory for ~/.distcc at boot
            @reboot ln -sfn "$(mktemp -d "/tmp/distcc.XXXXXXXX")" "$HOME/.distcc"






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 7 '14 at 18:54









            LekensteynLekensteyn

            123k49270361




            123k49270361







            • 4





              The question asks how to run something periodically in the terminal. cron runs behind the scenes rather than in the terminal

              – northern-bradley
              Dec 15 '14 at 19:22












            • 4





              The question asks how to run something periodically in the terminal. cron runs behind the scenes rather than in the terminal

              – northern-bradley
              Dec 15 '14 at 19:22







            4




            4





            The question asks how to run something periodically in the terminal. cron runs behind the scenes rather than in the terminal

            – northern-bradley
            Dec 15 '14 at 19:22





            The question asks how to run something periodically in the terminal. cron runs behind the scenes rather than in the terminal

            – northern-bradley
            Dec 15 '14 at 19:22











            5














            If you are monitoring the file system, then inotifywait is brilliant and certainly adds less load on your system.



            Example :



            In 1st terminal type this command :



            $ inotifywait .


            Then in 2nd terminal, any command that affects the current directory,



            $ touch newfile


            Then in original terminal inotifywait will wake up and report the event



            ./ CREATE newfile2


            Or in a loop



            $ while true ; do inotifywait . ; done
            Setting up watches.
            Watches established.
            ./ OPEN newfile2
            Setting up watches.
            Watches established.
            ./ OPEN newfile2
            Setting up watches.
            Watches established.
            ./ DELETE newfile
            Setting up watches.
            Watches established.
            ./ CREATE,ISDIR newdir
            Setting up watches.
            Watches established.





            share|improve this answer

























            • the user told you , no script , and maybe he dont want to monitor anything

              – nux
              Mar 6 '14 at 16:36






            • 3





              I didn't tell him to write a script, I suggested that if they are looping inorder to watch for particular filesystem event, then inotifywait is useful, and uses less resources than repeating a command. I often run several commands on a command line eg grep something InALogFile|less is that a script ?

              – X Tian
              Mar 6 '14 at 16:43












            • its a good answer , try to edit it to look more simple .

              – nux
              Mar 6 '14 at 16:45






            • 3





              What could be simpler than . I can't leave out the command.

              – X Tian
              Mar 6 '14 at 16:48











            • Thanks @XTian, a great command. I also now saw in the man page that you can add -m to continually monitor without a loop.

              – yoniLavi
              Jul 21 '14 at 16:06















            5














            If you are monitoring the file system, then inotifywait is brilliant and certainly adds less load on your system.



            Example :



            In 1st terminal type this command :



            $ inotifywait .


            Then in 2nd terminal, any command that affects the current directory,



            $ touch newfile


            Then in original terminal inotifywait will wake up and report the event



            ./ CREATE newfile2


            Or in a loop



            $ while true ; do inotifywait . ; done
            Setting up watches.
            Watches established.
            ./ OPEN newfile2
            Setting up watches.
            Watches established.
            ./ OPEN newfile2
            Setting up watches.
            Watches established.
            ./ DELETE newfile
            Setting up watches.
            Watches established.
            ./ CREATE,ISDIR newdir
            Setting up watches.
            Watches established.





            share|improve this answer

























            • the user told you , no script , and maybe he dont want to monitor anything

              – nux
              Mar 6 '14 at 16:36






            • 3





              I didn't tell him to write a script, I suggested that if they are looping inorder to watch for particular filesystem event, then inotifywait is useful, and uses less resources than repeating a command. I often run several commands on a command line eg grep something InALogFile|less is that a script ?

              – X Tian
              Mar 6 '14 at 16:43












            • its a good answer , try to edit it to look more simple .

              – nux
              Mar 6 '14 at 16:45






            • 3





              What could be simpler than . I can't leave out the command.

              – X Tian
              Mar 6 '14 at 16:48











            • Thanks @XTian, a great command. I also now saw in the man page that you can add -m to continually monitor without a loop.

              – yoniLavi
              Jul 21 '14 at 16:06













            5












            5








            5







            If you are monitoring the file system, then inotifywait is brilliant and certainly adds less load on your system.



            Example :



            In 1st terminal type this command :



            $ inotifywait .


            Then in 2nd terminal, any command that affects the current directory,



            $ touch newfile


            Then in original terminal inotifywait will wake up and report the event



            ./ CREATE newfile2


            Or in a loop



            $ while true ; do inotifywait . ; done
            Setting up watches.
            Watches established.
            ./ OPEN newfile2
            Setting up watches.
            Watches established.
            ./ OPEN newfile2
            Setting up watches.
            Watches established.
            ./ DELETE newfile
            Setting up watches.
            Watches established.
            ./ CREATE,ISDIR newdir
            Setting up watches.
            Watches established.





            share|improve this answer















            If you are monitoring the file system, then inotifywait is brilliant and certainly adds less load on your system.



            Example :



            In 1st terminal type this command :



            $ inotifywait .


            Then in 2nd terminal, any command that affects the current directory,



            $ touch newfile


            Then in original terminal inotifywait will wake up and report the event



            ./ CREATE newfile2


            Or in a loop



            $ while true ; do inotifywait . ; done
            Setting up watches.
            Watches established.
            ./ OPEN newfile2
            Setting up watches.
            Watches established.
            ./ OPEN newfile2
            Setting up watches.
            Watches established.
            ./ DELETE newfile
            Setting up watches.
            Watches established.
            ./ CREATE,ISDIR newdir
            Setting up watches.
            Watches established.






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 18 '14 at 2:13









            nux

            23k3096117




            23k3096117










            answered Mar 6 '14 at 16:35









            X TianX Tian

            21516




            21516












            • the user told you , no script , and maybe he dont want to monitor anything

              – nux
              Mar 6 '14 at 16:36






            • 3





              I didn't tell him to write a script, I suggested that if they are looping inorder to watch for particular filesystem event, then inotifywait is useful, and uses less resources than repeating a command. I often run several commands on a command line eg grep something InALogFile|less is that a script ?

              – X Tian
              Mar 6 '14 at 16:43












            • its a good answer , try to edit it to look more simple .

              – nux
              Mar 6 '14 at 16:45






            • 3





              What could be simpler than . I can't leave out the command.

              – X Tian
              Mar 6 '14 at 16:48











            • Thanks @XTian, a great command. I also now saw in the man page that you can add -m to continually monitor without a loop.

              – yoniLavi
              Jul 21 '14 at 16:06

















            • the user told you , no script , and maybe he dont want to monitor anything

              – nux
              Mar 6 '14 at 16:36






            • 3





              I didn't tell him to write a script, I suggested that if they are looping inorder to watch for particular filesystem event, then inotifywait is useful, and uses less resources than repeating a command. I often run several commands on a command line eg grep something InALogFile|less is that a script ?

              – X Tian
              Mar 6 '14 at 16:43












            • its a good answer , try to edit it to look more simple .

              – nux
              Mar 6 '14 at 16:45






            • 3





              What could be simpler than . I can't leave out the command.

              – X Tian
              Mar 6 '14 at 16:48











            • Thanks @XTian, a great command. I also now saw in the man page that you can add -m to continually monitor without a loop.

              – yoniLavi
              Jul 21 '14 at 16:06
















            the user told you , no script , and maybe he dont want to monitor anything

            – nux
            Mar 6 '14 at 16:36





            the user told you , no script , and maybe he dont want to monitor anything

            – nux
            Mar 6 '14 at 16:36




            3




            3





            I didn't tell him to write a script, I suggested that if they are looping inorder to watch for particular filesystem event, then inotifywait is useful, and uses less resources than repeating a command. I often run several commands on a command line eg grep something InALogFile|less is that a script ?

            – X Tian
            Mar 6 '14 at 16:43






            I didn't tell him to write a script, I suggested that if they are looping inorder to watch for particular filesystem event, then inotifywait is useful, and uses less resources than repeating a command. I often run several commands on a command line eg grep something InALogFile|less is that a script ?

            – X Tian
            Mar 6 '14 at 16:43














            its a good answer , try to edit it to look more simple .

            – nux
            Mar 6 '14 at 16:45





            its a good answer , try to edit it to look more simple .

            – nux
            Mar 6 '14 at 16:45




            3




            3





            What could be simpler than . I can't leave out the command.

            – X Tian
            Mar 6 '14 at 16:48





            What could be simpler than . I can't leave out the command.

            – X Tian
            Mar 6 '14 at 16:48













            Thanks @XTian, a great command. I also now saw in the man page that you can add -m to continually monitor without a loop.

            – yoniLavi
            Jul 21 '14 at 16:06





            Thanks @XTian, a great command. I also now saw in the man page that you can add -m to continually monitor without a loop.

            – yoniLavi
            Jul 21 '14 at 16:06











            4














            You can create your own repeat command doing the following steps; credits here:



            First, open your .bash_aliases file:



            $ xdg-open ~/.bash-aliases


            Second, paste these lines at the bottom of the file and save:



            repeat() 
            n=$1
            shift
            while [ $(( n -= 1 )) -ge 0 ]
            do
            "$@"
            done



            Third, either close and open again your terminal, or type:



            $ source ~/.bash_aliases


            Et voilà ! You can now use it like this:



            $ repeat 5 echo Hello World !!!


            or



            $ repeat 5 ./myscript.sh





            share|improve this answer























            • there is a small typo in the line xdg-open ~/.bash-aliases. it should be: xdg-open ~/.bash_aliases (ie: underscore)

              – ssinfod
              Feb 4 '18 at 1:21
















            4














            You can create your own repeat command doing the following steps; credits here:



            First, open your .bash_aliases file:



            $ xdg-open ~/.bash-aliases


            Second, paste these lines at the bottom of the file and save:



            repeat() 
            n=$1
            shift
            while [ $(( n -= 1 )) -ge 0 ]
            do
            "$@"
            done



            Third, either close and open again your terminal, or type:



            $ source ~/.bash_aliases


            Et voilà ! You can now use it like this:



            $ repeat 5 echo Hello World !!!


            or



            $ repeat 5 ./myscript.sh





            share|improve this answer























            • there is a small typo in the line xdg-open ~/.bash-aliases. it should be: xdg-open ~/.bash_aliases (ie: underscore)

              – ssinfod
              Feb 4 '18 at 1:21














            4












            4








            4







            You can create your own repeat command doing the following steps; credits here:



            First, open your .bash_aliases file:



            $ xdg-open ~/.bash-aliases


            Second, paste these lines at the bottom of the file and save:



            repeat() 
            n=$1
            shift
            while [ $(( n -= 1 )) -ge 0 ]
            do
            "$@"
            done



            Third, either close and open again your terminal, or type:



            $ source ~/.bash_aliases


            Et voilà ! You can now use it like this:



            $ repeat 5 echo Hello World !!!


            or



            $ repeat 5 ./myscript.sh





            share|improve this answer













            You can create your own repeat command doing the following steps; credits here:



            First, open your .bash_aliases file:



            $ xdg-open ~/.bash-aliases


            Second, paste these lines at the bottom of the file and save:



            repeat() 
            n=$1
            shift
            while [ $(( n -= 1 )) -ge 0 ]
            do
            "$@"
            done



            Third, either close and open again your terminal, or type:



            $ source ~/.bash_aliases


            Et voilà ! You can now use it like this:



            $ repeat 5 echo Hello World !!!


            or



            $ repeat 5 ./myscript.sh






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 8 '14 at 19:38









            BlufterBlufter

            411




            411












            • there is a small typo in the line xdg-open ~/.bash-aliases. it should be: xdg-open ~/.bash_aliases (ie: underscore)

              – ssinfod
              Feb 4 '18 at 1:21


















            • there is a small typo in the line xdg-open ~/.bash-aliases. it should be: xdg-open ~/.bash_aliases (ie: underscore)

              – ssinfod
              Feb 4 '18 at 1:21

















            there is a small typo in the line xdg-open ~/.bash-aliases. it should be: xdg-open ~/.bash_aliases (ie: underscore)

            – ssinfod
            Feb 4 '18 at 1:21






            there is a small typo in the line xdg-open ~/.bash-aliases. it should be: xdg-open ~/.bash_aliases (ie: underscore)

            – ssinfod
            Feb 4 '18 at 1:21












            4














            you can use crontab. run the command crontab -e and open it with your preferred text editor, then add this line



            */10 * * * * /path-to-your-command


            This will run your command every 10 minutes



            * */4 * * * /path-to-your-command


            This will run your command every 4 hours



            Another possible solution



            $ ..some command...; for i in $(seq X); do $cmd; sleep Y; done


            X number of times to repeat.



            Y time to wait to repeat.



            Example :



            $ echo; for i in $(seq 5); do $cmd "This is echo number: $i"; sleep 1;done





            share|improve this answer

























            • Why is this an improvement? You just added an extra, needless, step by saving the command as a variable. The only things this does is i) make it longer to type ii) forces you to use only simple commands, no pipes or redirects etc.

              – terdon
              Jul 3 '14 at 11:52











            • Remove the extra variable

              – Maythux
              May 14 '15 at 11:24















            4














            you can use crontab. run the command crontab -e and open it with your preferred text editor, then add this line



            */10 * * * * /path-to-your-command


            This will run your command every 10 minutes



            * */4 * * * /path-to-your-command


            This will run your command every 4 hours



            Another possible solution



            $ ..some command...; for i in $(seq X); do $cmd; sleep Y; done


            X number of times to repeat.



            Y time to wait to repeat.



            Example :



            $ echo; for i in $(seq 5); do $cmd "This is echo number: $i"; sleep 1;done





            share|improve this answer

























            • Why is this an improvement? You just added an extra, needless, step by saving the command as a variable. The only things this does is i) make it longer to type ii) forces you to use only simple commands, no pipes or redirects etc.

              – terdon
              Jul 3 '14 at 11:52











            • Remove the extra variable

              – Maythux
              May 14 '15 at 11:24













            4












            4








            4







            you can use crontab. run the command crontab -e and open it with your preferred text editor, then add this line



            */10 * * * * /path-to-your-command


            This will run your command every 10 minutes



            * */4 * * * /path-to-your-command


            This will run your command every 4 hours



            Another possible solution



            $ ..some command...; for i in $(seq X); do $cmd; sleep Y; done


            X number of times to repeat.



            Y time to wait to repeat.



            Example :



            $ echo; for i in $(seq 5); do $cmd "This is echo number: $i"; sleep 1;done





            share|improve this answer















            you can use crontab. run the command crontab -e and open it with your preferred text editor, then add this line



            */10 * * * * /path-to-your-command


            This will run your command every 10 minutes



            * */4 * * * /path-to-your-command


            This will run your command every 4 hours



            Another possible solution



            $ ..some command...; for i in $(seq X); do $cmd; sleep Y; done


            X number of times to repeat.



            Y time to wait to repeat.



            Example :



            $ echo; for i in $(seq 5); do $cmd "This is echo number: $i"; sleep 1;done






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 3 '15 at 10:19

























            answered Mar 7 '14 at 5:57









            MaythuxMaythux

            51.7k33172219




            51.7k33172219












            • Why is this an improvement? You just added an extra, needless, step by saving the command as a variable. The only things this does is i) make it longer to type ii) forces you to use only simple commands, no pipes or redirects etc.

              – terdon
              Jul 3 '14 at 11:52











            • Remove the extra variable

              – Maythux
              May 14 '15 at 11:24

















            • Why is this an improvement? You just added an extra, needless, step by saving the command as a variable. The only things this does is i) make it longer to type ii) forces you to use only simple commands, no pipes or redirects etc.

              – terdon
              Jul 3 '14 at 11:52











            • Remove the extra variable

              – Maythux
              May 14 '15 at 11:24
















            Why is this an improvement? You just added an extra, needless, step by saving the command as a variable. The only things this does is i) make it longer to type ii) forces you to use only simple commands, no pipes or redirects etc.

            – terdon
            Jul 3 '14 at 11:52





            Why is this an improvement? You just added an extra, needless, step by saving the command as a variable. The only things this does is i) make it longer to type ii) forces you to use only simple commands, no pipes or redirects etc.

            – terdon
            Jul 3 '14 at 11:52













            Remove the extra variable

            – Maythux
            May 14 '15 at 11:24





            Remove the extra variable

            – Maythux
            May 14 '15 at 11:24











            2














            Another concern with the "watch" approach proposed above is that it does display the result only when the process is done.
            "date;sleep 58;date" will display the 2 dates only after 59 seconds... If you start something running for 4 minutes, that display slowly multiple pages of content, you will not really see it.



            On the other hand, the concern with the "while" approach is that it doesn't take the task duration into consideration.



            while true; do script_that_take_between_10s_to_50s.sh; sleep 50; done


            With this, the script will run sometime every minutes, sometime might take 1m40. So even if a cron will be able to run it every minutes, here, it will not.



            So to see the output on the shell as it's generated and wait for the exact request time, you need to look at the time before, and after, and loop with the while.



            Something like:



            while ( true ); do
            echo Date starting `date`
            before=`date +%s`
            sleep `echo $(( ( RANDOM % 30 ) + 1 ))`
            echo Before waiting `date`
            after=`date +%s`
            DELAY=`echo "60-($after-$before)" | bc`
            sleep $DELAY
            echo Done waiting `date`
            done


            This will output this:



            As you can see, the command runs every minutes:



            Date starting Mon Dec 14 15:49:34 EST 2015
            Before waiting Mon Dec 14 15:49:52 EST 2015
            Done waiting Mon Dec 14 15:50:34 EST 2015

            Date starting Mon Dec 14 15:50:34 EST 2015
            Before waiting Mon Dec 14 15:50:39 EST 2015
            Done waiting Mon Dec 14 15:51:34 EST 2015


            So just replace the "sleep echo $(( ( RANDOM % 30 ) + 1 ))" command with what ever you want and that will be run, on the terminal/shell, exactly every minute. If you want another schedule, just change the "60" seconds with what ever you need.



            Shorter version without the debug lines:



            while ( true ); do
            before=`date +%s`
            sleep `echo $(( ( RANDOM % 30 ) + 1 ))` # Place you command here
            after=`date +%s`
            DELAY=`echo "60-($after-$before)" | bc`
            sleep $DELAY
            done





            share|improve this answer























            • Replying to myself... If your command takes more than the delay you configure, $DELAY will get a negative value and the sleep command will fail so the script will restart right away. Need to be aware of that.

              – jmspaggi
              Dec 14 '15 at 21:06















            2














            Another concern with the "watch" approach proposed above is that it does display the result only when the process is done.
            "date;sleep 58;date" will display the 2 dates only after 59 seconds... If you start something running for 4 minutes, that display slowly multiple pages of content, you will not really see it.



            On the other hand, the concern with the "while" approach is that it doesn't take the task duration into consideration.



            while true; do script_that_take_between_10s_to_50s.sh; sleep 50; done


            With this, the script will run sometime every minutes, sometime might take 1m40. So even if a cron will be able to run it every minutes, here, it will not.



            So to see the output on the shell as it's generated and wait for the exact request time, you need to look at the time before, and after, and loop with the while.



            Something like:



            while ( true ); do
            echo Date starting `date`
            before=`date +%s`
            sleep `echo $(( ( RANDOM % 30 ) + 1 ))`
            echo Before waiting `date`
            after=`date +%s`
            DELAY=`echo "60-($after-$before)" | bc`
            sleep $DELAY
            echo Done waiting `date`
            done


            This will output this:



            As you can see, the command runs every minutes:



            Date starting Mon Dec 14 15:49:34 EST 2015
            Before waiting Mon Dec 14 15:49:52 EST 2015
            Done waiting Mon Dec 14 15:50:34 EST 2015

            Date starting Mon Dec 14 15:50:34 EST 2015
            Before waiting Mon Dec 14 15:50:39 EST 2015
            Done waiting Mon Dec 14 15:51:34 EST 2015


            So just replace the "sleep echo $(( ( RANDOM % 30 ) + 1 ))" command with what ever you want and that will be run, on the terminal/shell, exactly every minute. If you want another schedule, just change the "60" seconds with what ever you need.



            Shorter version without the debug lines:



            while ( true ); do
            before=`date +%s`
            sleep `echo $(( ( RANDOM % 30 ) + 1 ))` # Place you command here
            after=`date +%s`
            DELAY=`echo "60-($after-$before)" | bc`
            sleep $DELAY
            done





            share|improve this answer























            • Replying to myself... If your command takes more than the delay you configure, $DELAY will get a negative value and the sleep command will fail so the script will restart right away. Need to be aware of that.

              – jmspaggi
              Dec 14 '15 at 21:06













            2












            2








            2







            Another concern with the "watch" approach proposed above is that it does display the result only when the process is done.
            "date;sleep 58;date" will display the 2 dates only after 59 seconds... If you start something running for 4 minutes, that display slowly multiple pages of content, you will not really see it.



            On the other hand, the concern with the "while" approach is that it doesn't take the task duration into consideration.



            while true; do script_that_take_between_10s_to_50s.sh; sleep 50; done


            With this, the script will run sometime every minutes, sometime might take 1m40. So even if a cron will be able to run it every minutes, here, it will not.



            So to see the output on the shell as it's generated and wait for the exact request time, you need to look at the time before, and after, and loop with the while.



            Something like:



            while ( true ); do
            echo Date starting `date`
            before=`date +%s`
            sleep `echo $(( ( RANDOM % 30 ) + 1 ))`
            echo Before waiting `date`
            after=`date +%s`
            DELAY=`echo "60-($after-$before)" | bc`
            sleep $DELAY
            echo Done waiting `date`
            done


            This will output this:



            As you can see, the command runs every minutes:



            Date starting Mon Dec 14 15:49:34 EST 2015
            Before waiting Mon Dec 14 15:49:52 EST 2015
            Done waiting Mon Dec 14 15:50:34 EST 2015

            Date starting Mon Dec 14 15:50:34 EST 2015
            Before waiting Mon Dec 14 15:50:39 EST 2015
            Done waiting Mon Dec 14 15:51:34 EST 2015


            So just replace the "sleep echo $(( ( RANDOM % 30 ) + 1 ))" command with what ever you want and that will be run, on the terminal/shell, exactly every minute. If you want another schedule, just change the "60" seconds with what ever you need.



            Shorter version without the debug lines:



            while ( true ); do
            before=`date +%s`
            sleep `echo $(( ( RANDOM % 30 ) + 1 ))` # Place you command here
            after=`date +%s`
            DELAY=`echo "60-($after-$before)" | bc`
            sleep $DELAY
            done





            share|improve this answer













            Another concern with the "watch" approach proposed above is that it does display the result only when the process is done.
            "date;sleep 58;date" will display the 2 dates only after 59 seconds... If you start something running for 4 minutes, that display slowly multiple pages of content, you will not really see it.



            On the other hand, the concern with the "while" approach is that it doesn't take the task duration into consideration.



            while true; do script_that_take_between_10s_to_50s.sh; sleep 50; done


            With this, the script will run sometime every minutes, sometime might take 1m40. So even if a cron will be able to run it every minutes, here, it will not.



            So to see the output on the shell as it's generated and wait for the exact request time, you need to look at the time before, and after, and loop with the while.



            Something like:



            while ( true ); do
            echo Date starting `date`
            before=`date +%s`
            sleep `echo $(( ( RANDOM % 30 ) + 1 ))`
            echo Before waiting `date`
            after=`date +%s`
            DELAY=`echo "60-($after-$before)" | bc`
            sleep $DELAY
            echo Done waiting `date`
            done


            This will output this:



            As you can see, the command runs every minutes:



            Date starting Mon Dec 14 15:49:34 EST 2015
            Before waiting Mon Dec 14 15:49:52 EST 2015
            Done waiting Mon Dec 14 15:50:34 EST 2015

            Date starting Mon Dec 14 15:50:34 EST 2015
            Before waiting Mon Dec 14 15:50:39 EST 2015
            Done waiting Mon Dec 14 15:51:34 EST 2015


            So just replace the "sleep echo $(( ( RANDOM % 30 ) + 1 ))" command with what ever you want and that will be run, on the terminal/shell, exactly every minute. If you want another schedule, just change the "60" seconds with what ever you need.



            Shorter version without the debug lines:



            while ( true ); do
            before=`date +%s`
            sleep `echo $(( ( RANDOM % 30 ) + 1 ))` # Place you command here
            after=`date +%s`
            DELAY=`echo "60-($after-$before)" | bc`
            sleep $DELAY
            done






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 14 '15 at 20:56









            jmspaggijmspaggi

            211




            211












            • Replying to myself... If your command takes more than the delay you configure, $DELAY will get a negative value and the sleep command will fail so the script will restart right away. Need to be aware of that.

              – jmspaggi
              Dec 14 '15 at 21:06

















            • Replying to myself... If your command takes more than the delay you configure, $DELAY will get a negative value and the sleep command will fail so the script will restart right away. Need to be aware of that.

              – jmspaggi
              Dec 14 '15 at 21:06
















            Replying to myself... If your command takes more than the delay you configure, $DELAY will get a negative value and the sleep command will fail so the script will restart right away. Need to be aware of that.

            – jmspaggi
            Dec 14 '15 at 21:06





            Replying to myself... If your command takes more than the delay you configure, $DELAY will get a negative value and the sleep command will fail so the script will restart right away. Need to be aware of that.

            – jmspaggi
            Dec 14 '15 at 21:06











            0















            run below script accordingly.

            #!/bin/bash
            while true
            do
            /usr/bin/mkdir /root/sec/$(date "+%T")
            sleep 2
            done





            share|improve this answer



























              0















              run below script accordingly.

              #!/bin/bash
              while true
              do
              /usr/bin/mkdir /root/sec/$(date "+%T")
              sleep 2
              done





              share|improve this answer

























                0












                0








                0








                run below script accordingly.

                #!/bin/bash
                while true
                do
                /usr/bin/mkdir /root/sec/$(date "+%T")
                sleep 2
                done





                share|improve this answer














                run below script accordingly.

                #!/bin/bash
                while true
                do
                /usr/bin/mkdir /root/sec/$(date "+%T")
                sleep 2
                done






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 21 mins ago









                linux.cnflinux.cnf

                13




                13



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f430382%2frepeat-a-command-every-x-interval-of-time-in-terminal%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Möglingen Índice Localización Historia Demografía Referencias Enlaces externos Menú de navegación48°53′18″N 9°07′45″E / 48.888333333333, 9.129166666666748°53′18″N 9°07′45″E / 48.888333333333, 9.1291666666667Sitio web oficial Mapa de Möglingen«Gemeinden in Deutschland nach Fläche, Bevölkerung und Postleitzahl am 30.09.2016»Möglingen

                    Virtualbox - Configuration error: Querying “UUID” failed (VERR_CFGM_VALUE_NOT_FOUND)“VERR_SUPLIB_WORLD_WRITABLE” error when trying to installing OS in virtualboxVirtual Box Kernel errorFailed to open a seesion for the virtual machineFailed to open a session for the virtual machineUbuntu 14.04 LTS Virtualbox errorcan't use VM VirtualBoxusing virtualboxI can't run Linux-64 Bit on VirtualBoxUnable to insert the virtual optical disk (VBoxguestaddition) in virtual machine for ubuntu server in win 10VirtuaBox in Ubuntu 18.04 Issues with Win10.ISO Installation

                    Torre de la Isleta Índice Véase también Referencias Bibliografía Enlaces externos Menú de navegación38°25′58″N 0°23′02″O / 38.43277778, -0.3838888938°25′58″N 0°23′02″O / 38.43277778, -0.38388889Torre de la Illeta de l’Horta o Torre Saleta. Base de datos de bienes inmuebles. Patrimonio Cultural. Secretaría de Estado de CulturaFicha BIC Torre de la Illeta de l’Horta. Dirección General de Patrimonio Cultural. Generalitat ValencianaLugares de interés. Ayuntamiento del CampelloTorre de la Isleta en CastillosNet.org