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.
Asked
Active
Viewed 3.9k times
30
-
If it suffices to run once a minute, you could use coron to run your script. Otherwise youl have to do some loop and actually run you script like a service. http://stackoverflow.com/questions/3430330/best-way-to-make-a-shell-script-daemon – Mithrandir Aug 06 '14 at 10:12
-
Yeah, but it will close current tty everytime. I mean to work without closing... – obohovyk Aug 06 '14 at 10:13
-
4What about wrapping your script (or its contents) in `while :; do script; sleep 10; done`? – Jens Aug 06 '14 at 10:14
-
very close subset http://stackoverflow.com/questions/9299704/run-command-every-second – Ciro Santilli OurBigBook.com May 18 '16 at 09:45
2 Answers
41
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.

Jens
- 69,818
- 15
- 125
- 179
-
2My variant looks like this: `while true; do` *code goes here...* `sleep 10` `done` It works well! – obohovyk Aug 06 '14 at 11:46
-
@alexkowalski Perfect! The next step would be to *accept* the answer you like best by clicking the check mark below the arrows. – Jens Aug 06 '14 at 12:34
-
1I have a python script that gives output called test.png. is it possible to change the name for every output (after 10 seconds) like test.png, tes1.png, tes2.png ....? – ferrelwill Mar 03 '16 at 14:59
-
@user1883491 Yes; but that's an entirely diffent question. You'd have to introduce a variable such as `i=0` and increment it in the loop with `: $((++i))`. – Jens Mar 03 '16 at 15:23
-
This one liner is superior , while sleep 1; do echo "Hi"; done - found at http://unix.stackexchange.com/questions/10646/repeat-a-unix-command-every-x-seconds-forever – MarcoZen Jun 12 '16 at 15:21
36
In linux you can use the watch
program to repeat an action. Assuming that script.sh
is executable:
watch -n 10 path/to/script.sh
Would run it every 10 seconds.
To make your script executable, you can use chmod +x script.sh
. Don't forget to add the shebang
#!/bin/bash
to the first line (assuming that it's a bash script).
If you're running the script from your current directory, you can then do:
watch -n 10 ./script.sh

Tom Fenech
- 72,334
- 12
- 107
- 141
-
Yeah, it also works but i have to use "sh" additional! `watch -n 10 sh script.sh` – obohovyk Aug 06 '14 at 11:42
-
Or you could add a shebang `#!/bin/bash` to the top of your script and make it executable. Note that in general, if you mean `bash`, don't use `sh` instead as it may be a different shell. – Tom Fenech Aug 06 '14 at 11:53
-
I wrote as example, but if i use `watch -n 10` without `sh` or `bash` it doesn't work ( – obohovyk Aug 06 '14 at 12:25
-
Is your script executable? Try `chmod +x script` (and don't forget to add the shebang as I stated above). If the script is in the current directory, you may also need to use `watch -n 10 ./script` – Tom Fenech Aug 06 '14 at 12:43
-
This doesnt work with bash commands like ll etc. This one liner from http://unix.stackexchange.com/questions/10646/repeat-a-unix-command-every-x-seconds-forever is superior - while sleep 1; do echo "Hi"; done – MarcoZen Jun 12 '16 at 15:22
-
What is the error you receive when trying to use `watch` with `ll` on your system? – Tom Fenech Jun 12 '16 at 15:24
-
Just add quotes around the expression: `watch -n 10 "bash path/to/script.sh"` – while Jan 28 '19 at 08:54
-
@zabop I rolled back your edit because the rest of the answer already goes into detail about how to make an executable script with a shebang, so running it as `bash path/to/script.sh` is redundant. – Tom Fenech Oct 06 '22 at 12:15