The Linux Rain Linux General/Gaming News, Reviews and Tutorials

Scripting a log for a single application

By Bob Mesibov, published 04/03/2014 in Tutorials


Here's a BASH shell script that automatically logs the date and time I start my Iceweasel web browser, and the length of time I use it. I launch the script (and Iceweasel) with a keyboard shortcut, and close the script by closing Iceweasel.

Click for full size

The log file has entries in the form [date]tab[Iceweasel start time]tab[Iceweasel duration], as in:

The launch time for the script comes from the date command with the %T formatting option. This option is shorthand for %H:%M:%S on a 24-hour clock (see the date man page for more options).

The output of the BASH built-in time command is here set by TIMEFORMAT. I've used %R, which outputs time in seconds, but I've added a zero before the R (%0R) to get whole seconds only. (See the BASH man page for TIMEFORMAT options.)

Notice that the start time logged is actually the start time of the script, not of Iceweasel. The difference is less than a second on my computer. The first version of this script got the start time of Iceweasel from the command

ps -eo cmd,start

executed after time had started in a subshell. The result was occasional weird entries in the log file. Sometimes ps returned multiple start times for the same process. It also printed to the log the kinds of error messages from the process that we desktop users normally never see, like GTK panic signals and hidden browser crashes at close.

I wrote the script above to keep track of my browser use, but keeping track of other applications could be handy. For example, you might like to play solitaire on your Linux computer at various times during your workday. You open the game, play for a while and close the application. How long do you spend doing that? You can find out by substituting the game command (possibly /usr/games/sol on your computer) for 'iceweasel' in the time command, and then launching the script when you want to play, instead of the game itself.

The script above quietly sends reports to the log, but it can also post an instant report as a notification. Put a '&&' after the echo command and follow with the lines:

When you close the games window you'll see a notification like:

Of course, if you feel a little guilty about playing games, especially at work, you might not want to do the log thing — and definitely not the notification...

*Edit*

I'm still getting occasional, strange 'empty times' in my Iceweasel log, as in this entry:

4 March 2014 19:12:51 0:0:0 0:0:0 0:0:0 0:0:0 1:12:35

To eliminate those 'empty times', I've changed the log-building command to this one:

The sed command deletes any [tab]0:0:0 strings. Please add a comment below if you can suggest why those 'empty times' sometimes appear.



About the Author

Bob Mesibov is Tasmanian, retired and a keen Linux tinkerer.

Tags: tutorial log application-logging scripting
blog comments powered by Disqus