Stupid Unix Tricks - Creating A Self-Updating Dashboard

11/27/2018 Update - What I wrote below works pretty well and is a fun exercise in Bash-style looping, but shortly after writing this article I discovered that there's already a canonical Unix/Linux tool that runs a command endlessly at given time periods called watch. Check that out first if you want to save a few keystrokes 😀

Have you ever wanted to automatically run the same command every 5, 10, or 60 seconds on a Unix/Linux machine until the output changed? For example, let's say that you're deploying a new application on top of a Tomcat app server. After starting Tomcat and deploying your application, it can take a couple of minutes before the application is really available. However, you want to know exactly when the new version of the application is usable (give or take 10 seconds).

A good way of testing whether the application is really "up" is to ping a URL. You can do this using a web browser, but this approach has a few disadvantages. First, it's a manual, tedious process. Second, web browsers can be a little flaky sometimes. For example, they may cache your last results, giving you incorrect results.

For me, I get much better results testing this sort of thing with curl. Here's an example of how you could test the availability of the Hello World application that comes with Tomcat 6.x:

$ curl http://localhost:8080/examples/servlets/servlet/HelloWorldExample
<html>
<head>
<title>Hello World!</title>
</head>
<body bgcolor="white">
<a href="../helloworld.html">
<img src="../images/code.gif" height=24 width=24 align=right border=0 alt="view code"></a>
<a href="../index.html">
<img src="../images/return.gif" height=24 width=24 align=right border=0 alt="return"></a>
<h1>Hello World!</h1>
</body>
</html>

This is fine, but it can also very labor-intensive and tedious to execute the same command over and over. Wouldn't it be nice if we could just create an ad-hoc "dashboard" for this command? Here's how you can do it. All we have to do is a little shell magic:

$ while true; do curl http://localhost:8080/examples/servlets/servlet/HelloWorldExample; sleep 10; done

Here's what I'm doing:

This loop will repeat itself forever until you kill it with a Ctrl-C. And here's my results (assuming that I just restarted the server and it took that servlet 20 seconds to respond):

curl: (7) couldn't connect to host # First execution
curl: (7) couldn't connect to host # second
<html>                             # Success!
<head>
<title>Hello World!</title>
</head>
<body bgcolor="white">
<a href="../helloworld.html">
<img src="../images/code.gif" height=24 width=24 align=right border=0 alt="view code"></a>
<a href="../index.html">
<img src="../images/return.gif" height=24 width=24 align=right border=0 alt="return"></a>
<h1>Hello World!</h1>
</body>
</html>

Ta-da! Easy, automatic command testing. Of course, you can use this trick with commands other than curl. It's very handy in any situation where you want to check a command for a change in output.

Last Updated .