How to run a command or script at screen lock/unlock?

Benjamin

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?

sourav c.

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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Shell script: run screen, open several screens and run a command in each

분류에서Dev

Shell script to run Linux command on multiple servers command by command

분류에서Dev

I would like to run a command as a bash script?

분류에서Dev

BASH - Run command using sudo but within a detached screen

분류에서Dev

How to run command on connect to network

분류에서Dev

How to run command as different user

분류에서Dev

How to run a command in background always?

분류에서Dev

How to know the command run by a process?

분류에서Dev

How to run Python Script on powerBI

분류에서Dev

How to run a shell script in background?

분류에서Dev

How run cherrypy app without screen logging?

분류에서Dev

how to make the command line screen follow the curser?

분류에서Dev

How to disable screen lock timeout from a script?

분류에서Dev

How to execute a script before login screen?

분류에서Dev

Getting error ": command not found" when trying to run shell script

분류에서Dev

run command with sudo su in script and redirect stdin and stdout

분류에서Dev

Is there a way to run a specific script with every “halt” and “reboot” command on Linux?

분류에서Dev

how to run a powershell script and a batch script in a batch script?

분류에서Dev

How to run a VirtualBox VM from command line?

분류에서Dev

How to run batch file command with elevated permissions?

분류에서Dev

How to run this curl command using Net:HTTP

분류에서Dev

How to include a Java command in ANT to run the project

분류에서Dev

How to run ''top'' command 1 time and exit?

분류에서Dev

How to find the full path of sudo to run a command

분류에서Dev

How to run complicated batch command using "subprocess"

분류에서Dev

How to run bash command with arguments at scheduled time

분류에서Dev

Windows 7 - How to run a script at shutdown but not at logoff

분류에서Dev

how to run a shell script on git commit

분류에서Dev

How to run Bash shell script on Windows?

Related 관련 기사

  1. 1

    Shell script: run screen, open several screens and run a command in each

  2. 2

    Shell script to run Linux command on multiple servers command by command

  3. 3

    I would like to run a command as a bash script?

  4. 4

    BASH - Run command using sudo but within a detached screen

  5. 5

    How to run command on connect to network

  6. 6

    How to run command as different user

  7. 7

    How to run a command in background always?

  8. 8

    How to know the command run by a process?

  9. 9

    How to run Python Script on powerBI

  10. 10

    How to run a shell script in background?

  11. 11

    How run cherrypy app without screen logging?

  12. 12

    how to make the command line screen follow the curser?

  13. 13

    How to disable screen lock timeout from a script?

  14. 14

    How to execute a script before login screen?

  15. 15

    Getting error ": command not found" when trying to run shell script

  16. 16

    run command with sudo su in script and redirect stdin and stdout

  17. 17

    Is there a way to run a specific script with every “halt” and “reboot” command on Linux?

  18. 18

    how to run a powershell script and a batch script in a batch script?

  19. 19

    How to run a VirtualBox VM from command line?

  20. 20

    How to run batch file command with elevated permissions?

  21. 21

    How to run this curl command using Net:HTTP

  22. 22

    How to include a Java command in ANT to run the project

  23. 23

    How to run ''top'' command 1 time and exit?

  24. 24

    How to find the full path of sudo to run a command

  25. 25

    How to run complicated batch command using "subprocess"

  26. 26

    How to run bash command with arguments at scheduled time

  27. 27

    Windows 7 - How to run a script at shutdown but not at logoff

  28. 28

    how to run a shell script on git commit

  29. 29

    How to run Bash shell script on Windows?

뜨겁다태그

보관