Put Your Computer’s Uptime On Your Blog

The period that your machine has been in operation

I stumbled on an interesting blog post yesterday. The author wrote about various information which was included in his blog footer. There were 4 pieces of information and I found one of those really interesting - where he uses an Applescript to upload the uptime of his computer (Macbook) and it is updated periodically.

Upon reading that post, it made me think of implementing the idea immediately. However, only after a while have I decided to do it. Instead of using Applescript, I chose bash script because bash script is lower level than Applescript and furthermore, you don’t have to interact with other high level applications which consumes less system resources. Below is the bash script that does the work on local machine.


# get the uptime
days=$(uptime | awk '{print $3}')
hours=$(uptime | awk '{print $5}' | sed 's/,//g')
# start the uptime in a text file
echo $days $time_lable > temp.txt
# use ftp to upload the file to a designed directory
ftp ftp://username:password@host <<EOF
cd "path-to/a-directory/on-web-server"
put temp.txt
bye
EOF
# delete the text file on the local machine
rm temp.txt

Save the script into a directory, for instant, a home directory. After that, you need to update it periodically. How? Cron (scheduling service on unix-like operating system) does this work perfectly. Create a new text file with the following code.


# this runs once in one hour
0 * * * * /path-to-the-bash-script

And then, you need to tell cron to start. Type the following in Terminal:


> crontab /patht-to-the-cron-job-file

There’s one thing left. We need to write a server script (PHP, Perl, or ASP) to display the text file upload by the bash script. The following is a PHP script to display the text file.


<?php
$file = 'path-to-temp.txt';
$fh = fopen($file, 'r');
$content = fread($fh, filesize($file));
// do some decoration work here
// ......
echo $content;
?>

It’s done.

Kudos to wesg.ca.

6 Responses to “Put Your Computer’s Uptime On Your Blog”

Leave a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word