Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeatedly run bash script every N seconds?

Tags:

I have a shell script printing some statistics like disk info, memory use and so on. But it shows information only once after the script runs and exits. Can I make this script be run repeatedly (like htop for example) or something like that? I want this info to be updated every 5-10 seconds.

like image 974
obohovyk Avatar asked Aug 06 '14 10:08

obohovyk


People also ask

How do I run a Linux script in 5 seconds?

What you could do is write a shell script with an infinite loop that runs your task, and then sleeps for 5 seconds. That way your task would be run more or less every 5 seconds, depending on how long the task itself takes. You can create a my-task.sh file with the contents above and run it with sh my-task.sh .

How do I run a script every 5 minutes in Linux?

basic 3. /usr/bin/vim. tiny 4. /bin/ed Choose 1-4 [1]: Make a new line at the bottom of this file and insert the following code. Of course, replace our example script with the command or script you wish to execute, but keep the */5 * * * * part as that is what tells cron to execute our job every 5 minutes.

How do I run a script every minute in Linux?

The asterisk (*) operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month. An asterisk in the every field means run given command/script every minute.


1 Answers

A slight improvement to my comment: if your script exits with true (e.g. when it ends with exit 0), you can run

while script; do sleep 10; done 

This is the canonical way to repeat a command as long as it doesn't fail.

like image 91
Jens Avatar answered Sep 23 '22 18:09

Jens