Automatically update your computer’s uptime on your website

You might notice in the footer of my blog that there is a small item that says My MacBook has been on for 4 days, 20:18 hours. I thought that this might be an interesting thing to share with people.

If this is something you’re interested in putting on your site, here is how I’ve automated the entire process. I’m going to assume that you’re running OS X, and that you have a standard text editor to write basic code.

While originally I made it happen with Automator and an Appescript, helpful commenter phalkunz created an even more efficient method that I will build on.

Process

  1. Bash script to generate the uptime data, format it nicely, and upload to website.
  2. Mark script as executable.
  3. Use PHP on your website to load the file.
  4. Make a cron task to have it updated regularly.

Step 1: Bash script

You can copy the following script entirely, just be sure to change the text in red to your corresponding value.


#!/bin/sh
#mark as executable

# get the uptime data
days=$(uptime | awk '{print $3}' | sed 's/,//g')
hours=$(uptime | awk '{print $5}' | sed 's/,//g')
label=$(uptime | awk '{print $4}')

if [ "$days" = 1 ]; then
day_label='day'
else
day_label='days'
fi

#format labels
if [ $hours = 1 ]; then
hour_label='hour'
else
hour_label='hours'
fi

#format output
if [ "$label" = 'mins,' ]; then
echo 'My MacBook has been on for '$days minutes'' > uptime.txt
elif [[ "$label" = 'day,' || "$label" = 'days,' ]]; then
echo 'My MacBook has been on for '$days $day_label, $hours $hour_label'' > uptime.txt
elif [ "$label" = '2' ]; then
echo 'My MacBook has been on for '$days hours'' > uptime.txt
fi

#upload to the website
hostname="FTP address"
username="FTP username"
password="FTP password"

ftp -n $hostname </path/to/wordpress/uploads
put uptime.txt
EOF

#move the uptime file back to its original place
mv uptime.txt /path/to/file

echo "task completed"

The FTP user details are fairly self explanatory. Put in your user name and password, as well as the directory you wish to upload the file to. I find it is best to upload your WordPress storage folder (typically /wp-content/uploads).

The last thing you need to change is the folder you wish to store the uptime text file. Due to a bug that I couldn’t fully resolve, the script writes to your user home folder, so this is just an easy way to clean up that directory.

Once you have all the important details down, choose a safe name and place for the script and save it with the extension .sh.

Step 2: Mark script as executable

This step caused me some frustration, before I realized what an easy fix it could be. Since the original script is just a text file, we need to get the system to mark the file as executable, or have it be used as a program.

To do that, we’ll need to open a Terminal window (found in /Applications/Utilities), and also have the script you’ve just written visible in a Finder window.

Inside the Terminal window, type chmod a+x then drag the file to the code window. The path information will automatically be added to the end, then press enter. If you’ve done everything correctly, there won’t be any output.

Example:

chmod a+x ~/Documents/uptime.sh

You can now test the script. Either write the path of the file in the Terminal window, or simply drag the script again, and press enter. You should see the FTP connect, transfer and disconnect, then see task completed at the end of the script.

Step 3: Use PHP to output the text file

This is the step that makes the data visible, but it has an extra tricky step if you’re using WP-cache or another caching plugin.

Open a new text document, and copy this code into it. Again, you’ll need to change the red bits.

/path/to/upload/folder/uptime.txt";
$fh = file_get_contents($ourFileName);
$modified = filemtime($ourFileName);
echo"
";
if ((time() - $modified) < 4800) echo($fh); else echo "Macbook uptime is out of date"; ?>

The only bit you need to change is the path to folder that you’ve uploaded the text file. It should be the same as the path used in the FTP upload process. Save the file with the extension .php and upload it to your web directory. To make things easiest, I suggest putting it in the home folder. The if/else statement there checks the file modified time of the text file and only outputs the data if it is less than 80 minutes old. You can change the value to anything you want (in seconds).

Next, you’ll need to edit a page of your WordPress template system. I’ve used the footer page so that it is visible from each page. Once you’ve opened the file you want to edit, and have found the spot you want to insert the text, copy this code.


mclude uptime.php

/mclude

Be sure to wrap mclude uptime.php and /mclude in HTML comments — <!-- mclude uptime.php -->.

If you’ve named the PHP file uptime.php and have uploaded it to the root directory, you won’t need to change anything. Otherwise, you’ll need to add the path and file name to both mclude uptime.php and '/uptime.php'. The mclude wrapper is important if you’re using WP-cache on your blog. This tells the plugin to treat the php page as dynamic with every load. The text you’ve entered should now be updated regardless of the page’s cached status.

Step 4: Schedule the script to run regularly


Mac OS X has a terrific system program called cron that checks every minute to see if a program is set to run. The beauty of this system is that users can add any type of program they want to run consistently. While it is possible to do the job from the command line, I have found it much easier to use an application called Cronnix that handles the coding for me.

The best way to fill out the sheet is to use the help file, but the basic idea in the new task window is that * means run every and a real number means that specific date. I’ve set my window to have * in every blank, and a 0 in the minute so that it runs hourly.

I should also mention that if you edit the Cronnix task, you need to follow step 2 again and mark the script as executable. Without doing that again, the task won’t run.

Phew, that’s a lot! If this seems just a little too nerdy for you, feel free to just watch the number in the footer about my MacBook. Tip o’ the hat to phalkunz for his code to gather the uptime properly.

[tags]programming, Unix, uptime, computers, blogs[/tags]

Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*