22

I am looking for a way to store lock/unlock screen times.

A=$(date)
echo $A >> $HOME/time_xprofile

What did I try:

$HOME/.bashrc
$HOME/.bash_logout
$HOME/.bash_prompt
$HOME/.xprofile

Then I locked the screen and checked whether file appeared and it failes every time. How can I check the time than?

2
  • As I read this question, I thought you were not running an X server. The answer you've selected is purely working while running an X server. Maybe you could describe your situation better...
    – ExploWare
    Mar 18, 2014 at 23:45
  • FWIW, this question was also asked on Unix/Linux stackexchange. Nov 26, 2015 at 3:44

3 Answers 3

21

The following script will write lock/unlock time in a file time_xprofile in your home.

#!/bin/bash

dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | \
( while true
    do read X
    if echo $X | grep "boolean true" &> /dev/null; then
        echo "locking at $(date)" >> $HOME/time_xprofile
    elif echo $X | grep "boolean false" &> /dev/null; then
        echo "unlocking at $(date)" >> $HOME/time_xprofile
    fi
    done )

save the script. Give it execution permission.

chmod +x script.sh

How to run

./script.sh &

Note The script should run in back ground. Do not kill it. If you turn your screen lock/unlock while the script is running in background, your time of lock/unlock will be recorded in time_xprofile file at your home. One can use it to run some command or script at screen lock/unlock.

Mind that if you close the current terminal your script will be killed. You can use

nohup ./script.sh &

Then it will continue running even after closing the terminal.

How to kill the script

To kill the process, use in terminal

ps ax| grep "[s]cript.sh" | cut -d' ' -f2 | xargs kill

Above script is inspired by this answer

3
  • Does it still working in ubuntu 17.10 with XServer? I don't know why is not working for me.. Nov 7, 2017 at 15:25
  • More than that: it only works a few times... Nov 7, 2017 at 22:11
  • 1
    @eddieferetro I am still on 16.04 cannot test it for you. Why don't you ask a new question specifying your issues? Dbus event for screen lock may be changed in 17.10
    – sourav c.
    Nov 8, 2017 at 2:22
6

In ubuntu 14.04 the DBus event for screen lock unlock has changed and the new script for binding to screen lock and unlock events looks like the following

dbus-monitor --session "type='signal',interface='com.ubuntu.Upstart0_6'" | \
(
  while true; do
    read X
    if echo $X | grep "desktop-lock" &> /dev/null; then
      SCREEN_LOCKED;
    elif echo $X | grep "desktop-unlock" &> /dev/null; then
      SCREEN_UNLOCKED;
    fi
  done
)

Replace SCREEN_LOCKED and SCREEN_UNLOCKED with what you need to do.

0

I used the same answer as sourav c., but first I run only dbus-monitor --session "type='signal'" in a terminal, and locked an unlocked my screen. Then, I could figure out the correct name for my operating system:

signal time=1680812432.954794 sender=:1.27 -> destination=(null destination) serial=3080 path=/org/xfce/ScreenSaver; interface=org.xfce.ScreenSaver; member=ActiveChanged
   boolean true
signal time=1680812435.757417 sender=:1.27 -> destination=(null destination) serial=3082 path=/org/xfce/ScreenSaver; interface=org.xfce.ScreenSaver; member=ActiveChanged
   boolean false

So, my command starts with dbus-monitor --session "type='signal',interface='org.xfce.ScreenSaver'"' instead of dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .