Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Create Infinite Loop In Bash

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to run a command for ever in the Linux bash. Also I want to put 60 seconds between command execution.
 
lowercase baba
Posts: 12870
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?  We don't really hand out answers here, but will help you find the answer yourself.
 
Sheriff
Posts: 21971
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can simply use watch.
 
Sheriff
Posts: 4870
317
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Watch is a really handly command and one that I come back to time and time again. If you want to run it forever then run it within a Screen session so you can detach from the session and leave it running (so long as the machine is up of course)
 
Saloon Keeper
Posts: 22248
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The command-line utility for waiting for a set period of time is sleep. While the system-based documentation for bash is long and hard to locate stuff in, you can always google stupid bash tricks and look for "do forever".

However, if you are looking for a more integrated approach, you should consider using cron, which can do things like run a script once every 5 minutes.

Running stuff without holding your terminal hostage can be done via services like NOHUP and screen. Running services that are not dependent on your being logged in at all - especially ones that start automatically - is what daemons do. These days, daemons are generally managed via systemd.
 
Ranch Hand
Posts: 51
1
Redhat Notepad Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at my example:

 
fred rosenberger
lowercase baba
Posts: 12870
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the OP has never come back.  However, thinging about it, the specs aren't really clear.  If you run the first command at 00:00:00, and it takes it 10 seconds to run, should the next iteration start at 00:01:00 or 00:01:10?  In other words, do you issue the command every 60 seconds, or do you you issue the next command 60 seconds after the previous one finishes?

If the former, what if the command takes more than 60 seconds to run?
 
Tim Holloway
Saloon Keeper
Posts: 22248
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are all good questions and something I routinely consider when doing stuff like that.

The answer is that sometimes it doesn't matter and sometimes it does. I may be fine with fuzzy timing when I just want to run a periodic clean-up of a directory or something, but then again, there are times when I may need to do something at precisely the right time. So that would definitely shape the solution to the problem.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Gabriel Turqos,

If you want to run a command periodically, there's 3 ways :

using systemd timer

using the crontab command ex. * * * * * command (run every minutes)

using a loop like : while true; do ./scriptname.sh; sleep 60; done (not precise)



i hope this will help to you

 
    Bookmark Topic Watch Topic
  • New Topic