24 May 2008
Put Your Computer’s Uptime On Your Blog
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.









Wes G Says:
That’s terrific! You did what would have taken me ages to figure out!
I’ve taken your script and modified it to output proper labels and text, and it should be running on my site once crontab cooperates.
The coolest part of blogging » wesg Says:
[…] of Applescripts and Automator actions. Commenter phalkunz stated that they liked the idea, and had developed an even more elegant solution. I was excited that someone else had taken the time to read my post, and also come up with another […]
phalkunz Says:
@Wes G: thanks for dropping by.
Wes G Says:
Hey I noticed you used the @reference.
I made a Wordpress plugin to add some zing to that.
http://www.wesg.ca/2008/04/wordpress-plugin-comment-connection/
phalkunz Says:
@wesg: i’ll check it out. thanks.
Automatically update your computer’s uptime on your website » wesg Says:
[…] originally I made it happen with Automator and an Appescript, helpful commenter phalkunz created an even more efficient method that I will build […]