DEV Community

Diego Carrasco Gubernatis
Diego Carrasco Gubernatis

Posted on • Originally published at diegocarrasco.com on

How to auto restart command/ process in Linux after specific run time or duration (quick-note)

Context

You want to auto-restart a command or process in Linux after specific run time, duration or time period.

For this you can use the timeout command in a while loop.

while true; do
timeout 10 htop --tree
true # you have to have true so that the while lop doesn't break. You might also do en echo.
done

Enter fullscreen mode Exit fullscreen mode

where:

  • timeout 10 means 10 seconds
  • htop --tree is the command you want to run

You may also use echo instead of true after the command

while true; do
timeout 10 htop --tree
echo "restarting process..."
done

Enter fullscreen mode Exit fullscreen mode

References

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay