Wednesday, May 1, 2024
21
rated 0 times [  21] [ 0]  / answers: 1 / hits: 13143  / 2 Years ago, tue, february 22, 2022, 5:18:17

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?


More From » command-line

 Answers
5

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


[#26675] Wednesday, February 23, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
umplegitimat

Total Points: 137
Total Questions: 126
Total Answers: 118

Location: Saint Pierre and Miquelon
Member since Sat, Aug 21, 2021
3 Years ago
;