How to Run or Repeat a Linux Command Every X Seconds Forever

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.95/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Marin Todorov

I am a bachelor in computer science and a Linux Foundation Certified System Administrator. Currently working as a Senior Technical support in the hosting industry. In my free time I like testing new software and inline skating.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

11 Responses

  1. coringa says:

    Oh my god, thanks a lot, i was trying to use cron, but 1 minute is too long, you saved me, a simple command that do the job, thanks a lot man

  2. SanjayW says:

    Add screen to the mix and you can exit the terminal and the script continues to run…

  3. Richard says:

    I GOT REALLY STUCK.

    I tried to follow all the instructions above to set up my LCD screen to work with my pi Model B+, but it looks like I got myself into trouble. The LCD screen won’t work– and the pi won’t book up correctly. After step 3, I rebooted the Pi, but I could get back to a working environment- meaning I can’t get to the command-line to finish up the setup process.

    Could you help me with this?

  4. Kenton says:

    Instead of “while true; do (some command);sleep 3;done” You can just use “while sleep 3;do (some command);done”

    The sleep command returns true in a conditional.

  5. Michael says:

    Please how will i grep a string in a log file to output the lastest string in the file.

    Thanks for your answer as usual.

  6. Stef says:

    Another nice command is ‘at’ that executes the commands specified from the input stream at a given time. On Debian, it is provided by the package at.

    The nice thing about ‘at’ is that the time specification is quite flexible and intuitive
    echo notify-send Hoops | at now + 5 minute
    echo notify-send Hoops | at 10pm
    echo notify-send Hoops | at 10pm tomorrow
    echo notify-send Hoops | at 22:00 monday

    By itself t does not repeat but that can easily be done using a loop.

    for ((i=1;i<10;i++)) ; do echo echo notify-send "Hoops $i" | at now + $i minute ; done

    Be aware that 'at', like cron, executes its commands in a clean environment so without most environment variables such as DISPLAY.

  7. Shahid Khattak says:

    Nice article. especially to check free memory in real time and checking system average load

  8. Vicdeveloper says:

    Saved in Favorites. :D. Thanks

  9. John Thingstad says:

    How about a introduction to time dependent tasks in systemd?. cron is on the way out anyways.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *