The Haiku/BeOS Tip Server
Tips and tricks for Haiku/BeOS users

Keep an uptime log

Contributed by: Scot Hacker
This tip is valid for: Both BeOS and Haiku

If you’d like to keep a text log listing the lengths of time your system has stayed running between boots, you can do it easily with the uptime binary and a simple script. There are two possible ways to do this (actually, there are probably lots of ways to do this): The first method involves using UserShutdownScript to write a line to your log file when you shut down. This method only requires that uptime be available on your system, and so is easier to use. Unfortunately, if your system crashes or locks up, UserShutdownScript will not be run, so you could lose an entry for three weeks of uptime, and that would suck. The second method requires a little more configuration, but guards against this possibility.

Method 1: Invoking from UserShutdownScript

First, download, install, and test uptime. A version that compiles cleanly for all BeOS platforms is available here.

Now you need to write a shell script that will gather the current date, time, and uptime results, compile them into a single line of text, and append that line to your log file. Download the prefab if you don’t feel like writing your own.

Test the script, then have it invoked as the last step in the BeOS shutdown process by calling it from /boot/home/config/boot/UserShutdownScript.

So here’s what happens: You begin the BeOS shutdown process normally, which causes UserShutdownScript to be run automatically, if it exists. UserShutdownScript invokes your script, which in turn invokes the uptime binary and writes a new line of text to your uplog log file. A sample line of this log file might look like:

01/23/99, 10:30:38 PM : 4 days, 12 hours, 6 minutes, 49 seconds

A couple of months from now, you can scan this file to find your longest uptime.

Method 2: Using cron to keep a continuous uplog

If you want to make sure your log always gets written to, even if you crash, you’ll want to use cron to write your uptime once a minute to a temporary file. Then, every time you boot your system, you’ll take the last line of that temp file and append it to your uplog. This method requires that you have both uptime (see above) and cron (see BeWare) installed, and that you learn how to use and configure cron.

Build a crontab file that runs a command once per minute to run uptime and redirect the results to the end of an ongoing log file. Here’s mine:

* * * * * ~/config/bin/uptime >> /boot/tmp/uplog.tmp

The important thing here is that you keep your uplog in /boot/tmp and not just /tmp. This is because /tmp is part of your virtual file system, not your real one. Therefore anything written into /tmp will vanish after a reboot, and you won’t be able to extract your uptime from the temp file.

Now you just need to add the following to your UserBootScript (adjust any paths to suit your needs):

tail -1 /boot/tmp/uplog.tmp >> ~/data/uplog

rm /boot/tmp/uplog.tmp
touch /boot/tmp/uplog.tmp

crontab ~/cronjobs/jobs

Your permanent log file will be ~/data/uplog, and will keep a chronicle of uptimes, accurate to within one minute.

Posted in Miscellaneous

(comments are closed).