DSwain- The New Day

Monday, May 08, 2006

Creating a Status Page via Bash & HTML

Realistically, this is a pretty cheap way of doing this. I've seen much more professional method of doing this, but my method happens to work (with proof, even). And so, here we go:

To break this down into a basic form, what happens is that this script that I wrote does a run through of a variety of different elements throughout my machine to list as status, and then redirects that output to an HTML file. The HTML file, in turn, is moved over into its correct location on the server to be updated. This process is computed once every minute via crond.

First, we begin with the bash script. I just added some things which I thought were important for reading the status of a server for a user. This consists of programs like uptime, df, free, and uname. This is the exact script code.

As you can see, many of the commands are common coreutil/system commands which aren't very uncommon. These are outputted to an HTML file along with the tags to make things bold, add spaces, and so on. I wrote the code to add spaces as functions so the code was more clear.

Next, on your machine you need to set up cron. Cron is a daemon system which puts a command on a schedule to run at certain times. You can have it do anything from running at every minute to running once a year with it. It's a rather flexible code, but it's a little confusing to understand by just reading a raw example. What you do with crond is write crontabs. Crontabs are basically small files which specify the user which the command is running for, the times which it should run, and what the actual thing to run is. My example looks like this:
* * * * * sudo /srv/www/htdocs/stats_script
This basically says that there are no specific times to run other than every minute, and to execute the command sudo /srv/www/htdocs/stats_script which handles the rest. Once you write this into a particular file, you run the command crontab filename which in turn adds the command to the appropriate crontab file. This file is read by crond on a minutely schedule and runs the command.

The result of all of this? Here it is.

0 Comments:

Post a Comment

<< Home