2

I have a different user for each of my own work areas on my system. This is to keep bookmarks, files and other defaults separate.

Several "users" are generally logged in at once and I just switch between them when I change which project I'm working on in that moment.

I am using Ubuntu 18.04 with Gnome Desktop Environment

I'd like to keep track of when I've been working on different projects and I figured tracking these sessions would be an easy way to do this, however I don't know if there is a log available that knows which user is using the GUI at any one time, or how to create such a tracker.

The two details I think are needed for the tracker are: 1.) Events where a user logs in, including the times when the user is unlocking a session that is already open 2.) When screen locks due to inactivity

Does anyone know if these are already/can be logged?

Thanks! Woody

Solution: ( With help from meuh below and this question: How to run a script on screen lock/unlock? )

This following script needs to be run from ~/.profile for each user that I wish to keep track of, their login/logout times all get logged in the OUTPUTFILE

The initial echo is because the script only gets run after the user has logged in and would otherwise be missed. All subsequent logins are caught from dbus-monitor

#!/bin/bash
echo $(date), $USER, SCREEN_UNLOCKED >> OUTPUTFILENAME
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
  while read x; do
    case "$x" in 
      *"boolean true"*) echo $(date), $USER, SCREEN_LOCKED >> OUTPUTFILENAME;;
      *"boolean false"*) echo $(date), $USER, SCREEN_UNLOCKED >> OUTPUTFILENAME;;
    esac
  done
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Woody
  • 23

2 Answers2

2

Probably, the way to achieve this is using dbus. This is a mechanism of communication amongst disparate tasks that is highly generic and so quite difficult to use. You should be looking for events, called signals, sent over the bus by things like the systemd logind daemon and the gnome screensaver.

There are at least 3 different C libraries to access the bus, and some more manageable Python libraries on top of these. And there are some simple command-line interfaces that should be sufficient to write a shell script to extract the data. For example,

dbus-monitor --system

(you may need sudo) will run continously showing calls and events. Look for stanzas beginning signal that have a path=/org/freedesktop/login1.... These will contain, in some complex way, information when someone logs in or out, or when the user on seat0 (the principal console) changes, or when the display locks due to idleness.

systemd has created its own alternative command:

sudo busctl monitor

which provides the information in a different format. There is also gdbus monitor ... which needs more args to say what to listen for.

Alternatively, you could try using in each login some time-tracking for worksheets tool such as track (about which I know nothing), then merging the information.

meuh
  • 51,383
0

loginctl will give you a list of session-id's and users. From there you can use loginctl show-session <session-id> and you'll see Type=x11 or Type=wayland for those sessions.

Here's an example:

$ loginctl
SESSION  UID USER SEAT  TTY  
     19 1000 stew       pts/2
      2 1000 stew seat0 tty2 
     20 1000 stew seat0 tty6

3 sessions listed. $ loginctl show-session 19 -p Type Type=tty $ loginctl show-session 2 -p Type Type=x11 $ loginctl show-session 20 -p Type Type=tty

In this case I am logged into to this machine via ssh, and also on two TTYs. One of the TTYs is running X11.

It's not a log, but if you periodically poll this, you can create a log with it.

Stewart
  • 13,677