23

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.

Jens
  • 65,924
  • 14
  • 115
  • 171
obohovyk
  • 412
  • 2
  • 4
  • 12

2 Answers2

33

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
  • 65,924
  • 14
  • 115
  • 171
  • 2
    My 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
  • 1
    I 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
31

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
  • 69,051
  • 12
  • 96
  • 131
  • 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