I'm trying to find a simple way to repeatedly restart a command after a certain time (let's say 10 seconds for the purpose of this question). For simplicity let's say the command in question is htop --tree.

My attempt so far: To just run and then stop a command once I found the commmand timeout, so timeout 10 htop --tree runs the command and stops it again after 10 seconds. To repeat that I found the command watch which lets you rerun a command every given time interval. So my attempt was combining the two using

watch -n 10 `timeout 9 htop --tree`

but then it the output of htop does not show up. Can anyone see what I'm doing wrong or how to fix that, or suggest an alternative bash solution for what I'm trying to achive?


Yes, the watch utility affects the way some commands work. It would better to just use a loop. It also seems as if you are requesting to watch the output of the htop command, as a command, which is probably not what you actually want (just spotting those backticks, which is a command substitution, meaning that that part of the command will be replaced by the output of the command within).

while true; do
    timeout 10 htop --tree

Depending on the effect that you want to achieve, you may also want to investigate the update delay in htop itself. Here, I use a 10 second refresh rate:

htop -d 100

(the delay is measured in tenths of a second)

  • I completely forgot about the loops in bash, thank you very much! Thanks for the tip with htop, I only used it as an example comman but it is nice to know it also has this argument! – flawr Jun 5 '20 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.