Run script on screen lock/unlock
Clash Royale CLAN TAG#URR8PPP
up vote
46
down vote
favorite
I'd like to run a screen if the Gnome session is locked and unlocked. Is there a way that I can intercept this and perform certain actions when the desktop is locked or unlocked?
ubuntu gnome
add a comment |Â
up vote
46
down vote
favorite
I'd like to run a screen if the Gnome session is locked and unlocked. Is there a way that I can intercept this and perform certain actions when the desktop is locked or unlocked?
ubuntu gnome
add a comment |Â
up vote
46
down vote
favorite
up vote
46
down vote
favorite
I'd like to run a screen if the Gnome session is locked and unlocked. Is there a way that I can intercept this and perform certain actions when the desktop is locked or unlocked?
ubuntu gnome
I'd like to run a screen if the Gnome session is locked and unlocked. Is there a way that I can intercept this and perform certain actions when the desktop is locked or unlocked?
ubuntu gnome
ubuntu gnome
asked Jan 3 '12 at 2:14
Naftuli Kay
11.5k53153246
11.5k53153246
add a comment |Â
add a comment |Â
8 Answers
8
active
oldest
votes
up vote
40
down vote
accepted
Gnome-screensaver emits some signals on dbus when something happens.
Here the documentation (with some examples).
You could write a scripts that runs:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'"
and that does what you need anytime dbus-monitor
prints a line about the screen being locked/unlocked.
Here a bash command to do what you need:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
Just replace echo SCREEN_LOCKED
and echo SCREEN_UNLOCKED
with what you need.
Hi @peoro, That makes me think you can unlock or lock gnome screen/session from a scripted program, worth doing some ssh tricks about it ;-)
â Nikhil Mulley
Jan 3 '12 at 5:13
1
@Nikhil: to do that you don't need to play around with dbus:gnome-screensaver-command
is already there. Passing-a
tognome-screensaver-command
you'll lock the screen, while you'll unlock it with-d
. Anyway most gnome apps use dbus extensively, so you'll be able to do many amazing things with it.
â peoro
Jan 3 '12 at 5:18
1
@peoro Thanks great, very helpful! Can I also run this as some sort of daemon? When I enter this in the terminal now, it has to stay open to monitor the dbus for that case. I would like to execute this command at login and then it can be active during the entire session.
â Sander
Jan 4 '12 at 16:13
1
I think things may have changed now in 2014? as the output doesnt change if the screen was only locked, it only shows something when it gets blanked and is very different from here :(, I created this question askubuntu.com/questions/505681/â¦, do you believe there is still some way to do that? thx!
â Aquarius Power
Aug 2 '14 at 2:18
How to run that script that it catches the lock event? Kinda like a watcher.
â Starx
Nov 1 '16 at 13:03
 |Â
show 1 more comment
up vote
18
down vote
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
)
ideas on how to make this work on fedora 23?
â Ray Foss
Apr 30 '16 at 21:49
2
Works fine on 16.04 as well
â Jacob Vlijm
Nov 29 '16 at 6:43
@JacobVlijm Thanks for testing this and green-lighting it for me to use tonight :)
â WinEunuuchs2Unix
Dec 8 '16 at 0:44
add a comment |Â
up vote
3
down vote
Ubuntu 16.04: ozmaâÂÂs solution did not work for me, however this one did:
dbus-monitor --session "type=signal,interface=com.canonical.Unity.Session,member=Unlocked" |
while read MSG; do
LOCK_STAT=`echo $MSG | awk 'print $NF'`
if [[ "$LOCK_STAT" == "member=Unlocked" ]]; then
echo "was unlocked"
fi
done
It might work on Unity but question was in regards to Gnome.
â cprn
Jun 1 '17 at 10:03
add a comment |Â
up vote
3
down vote
Expanding on already given answer.
If you try to run a script from inside a screen
or tmux
session, you'll need to find the correct $DBUS_SESSION_BUS_ADDRESS
first and pass it as an argument for dbus-monitor
instead of --session
. Also if you're running it as a daemon you should make sure only one instance is running at a time (e.g. with a lock file) and that the script cleans up after itself with trap
. The following example will work as a daemon in most current Gnome environments (tested on Ubuntu GNOME 16.04):
#!/bin/bash
set -o nounset # good practice, exit if unset variable used
pidfile=/tmp/lastauth.pid # lock file path
logfile=/tmp/lastauth.log # log file path
cleanup() # when cleaning up:
rm -f $pidfile # * remove the lock file
trap - INT TERM EXIT # * reset kernel signal catching
exit # * stop the daemon
log() # simple logging format example
echo $(date +%Y-%m-%d %X) -- $USER -- "$@" >> $logfile
if [ -e "$pidfile" ]; then # if lock file exists, exit
log $0 already running...
exit
fi
trap cleanup INT TERM EXIT # call cleanup() if e.g. killed
log daemon started...
echo $$ > $pidfile # create lock file with own PID inside
# usually `dbus-daemon` address can be guessed (`-s` returns 1st PID found)
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pidof -s dbus-daemon)/environ)
expr='type=signal,interface=org.gnome.ScreenSaver' # DBus watch expression here
dbus-monitor --address $DBUS_SESSION_BUS_ADDRESS "$expr" |
while read line; do
case "$line" in
*"boolean true"*) log session locked;;
*"boolean false"*) log session unlocked;;
esac
done
cleanup # let's not leave orphaned lock file when the loop ends (e.g. dbus dies)
If this doesn't work for you, it's probably because:
- you don't use Gnome - check other answers for better DBus watch expression
- you run multiple DBus lines - check details on how to make PID finding deterministic
1
This actually answers a different question I had for dynamically discovering the DBus session information given a user account, which I have solved here. Thanks for your contribution here!
â Naftuli Kay
Jun 2 '17 at 5:10
Thanks. I'll link your solution in the answer for further reading.
â cprn
Jun 2 '17 at 9:53
add a comment |Â
up vote
2
down vote
If you're on Kubuntu or using KDE / Plasma as your Desktop Environment, you have to listen for the interface org.freedesktop.ScreenSaver
, so the script for listening to that event would look like this:
dbus-monitor --session "type='signal',interface='org.freedesktop.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
add a comment |Â
up vote
2
down vote
Nowadays I think it's better to listen to the LockedHint
rather than screensaver messages. That way you're not tied to a screensaver implementation.
Here's a simple script to do that:
gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint
Gives this:
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <true>, @as )
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <false>, @as )
add a comment |Â
up vote
0
down vote
Where to write this code and how it will run:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
add a comment |Â
up vote
-1
down vote
this is what worked for me in ubuntu 16.04
dbus-monitor --session "type=signal,interface=org.gnome.ScreenSaver" |
while read MSG; do
LOCK_STAT=`echo $MSG | grep boolean | awk 'print $2'`
if [[ "$LOCK_STAT" == "true" ]]; then
echo "was locked"
else
echo "was un-locked"
fi
done
This didn't work for me. As it finished executing and does not listen to the state changed.
â Starx
Nov 1 '16 at 13:12
which screen saver are you using? gnome or xscreensaver? which flavor ubuntu, xubuntu, kubuntu etc. which version (it was tested on 16.04)
â ozma
Nov 1 '16 at 14:06
ubuntu gnome 16.04
â Starx
Nov 2 '16 at 9:25
add a comment |Â
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
40
down vote
accepted
Gnome-screensaver emits some signals on dbus when something happens.
Here the documentation (with some examples).
You could write a scripts that runs:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'"
and that does what you need anytime dbus-monitor
prints a line about the screen being locked/unlocked.
Here a bash command to do what you need:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
Just replace echo SCREEN_LOCKED
and echo SCREEN_UNLOCKED
with what you need.
Hi @peoro, That makes me think you can unlock or lock gnome screen/session from a scripted program, worth doing some ssh tricks about it ;-)
â Nikhil Mulley
Jan 3 '12 at 5:13
1
@Nikhil: to do that you don't need to play around with dbus:gnome-screensaver-command
is already there. Passing-a
tognome-screensaver-command
you'll lock the screen, while you'll unlock it with-d
. Anyway most gnome apps use dbus extensively, so you'll be able to do many amazing things with it.
â peoro
Jan 3 '12 at 5:18
1
@peoro Thanks great, very helpful! Can I also run this as some sort of daemon? When I enter this in the terminal now, it has to stay open to monitor the dbus for that case. I would like to execute this command at login and then it can be active during the entire session.
â Sander
Jan 4 '12 at 16:13
1
I think things may have changed now in 2014? as the output doesnt change if the screen was only locked, it only shows something when it gets blanked and is very different from here :(, I created this question askubuntu.com/questions/505681/â¦, do you believe there is still some way to do that? thx!
â Aquarius Power
Aug 2 '14 at 2:18
How to run that script that it catches the lock event? Kinda like a watcher.
â Starx
Nov 1 '16 at 13:03
 |Â
show 1 more comment
up vote
40
down vote
accepted
Gnome-screensaver emits some signals on dbus when something happens.
Here the documentation (with some examples).
You could write a scripts that runs:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'"
and that does what you need anytime dbus-monitor
prints a line about the screen being locked/unlocked.
Here a bash command to do what you need:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
Just replace echo SCREEN_LOCKED
and echo SCREEN_UNLOCKED
with what you need.
Hi @peoro, That makes me think you can unlock or lock gnome screen/session from a scripted program, worth doing some ssh tricks about it ;-)
â Nikhil Mulley
Jan 3 '12 at 5:13
1
@Nikhil: to do that you don't need to play around with dbus:gnome-screensaver-command
is already there. Passing-a
tognome-screensaver-command
you'll lock the screen, while you'll unlock it with-d
. Anyway most gnome apps use dbus extensively, so you'll be able to do many amazing things with it.
â peoro
Jan 3 '12 at 5:18
1
@peoro Thanks great, very helpful! Can I also run this as some sort of daemon? When I enter this in the terminal now, it has to stay open to monitor the dbus for that case. I would like to execute this command at login and then it can be active during the entire session.
â Sander
Jan 4 '12 at 16:13
1
I think things may have changed now in 2014? as the output doesnt change if the screen was only locked, it only shows something when it gets blanked and is very different from here :(, I created this question askubuntu.com/questions/505681/â¦, do you believe there is still some way to do that? thx!
â Aquarius Power
Aug 2 '14 at 2:18
How to run that script that it catches the lock event? Kinda like a watcher.
â Starx
Nov 1 '16 at 13:03
 |Â
show 1 more comment
up vote
40
down vote
accepted
up vote
40
down vote
accepted
Gnome-screensaver emits some signals on dbus when something happens.
Here the documentation (with some examples).
You could write a scripts that runs:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'"
and that does what you need anytime dbus-monitor
prints a line about the screen being locked/unlocked.
Here a bash command to do what you need:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
Just replace echo SCREEN_LOCKED
and echo SCREEN_UNLOCKED
with what you need.
Gnome-screensaver emits some signals on dbus when something happens.
Here the documentation (with some examples).
You could write a scripts that runs:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'"
and that does what you need anytime dbus-monitor
prints a line about the screen being locked/unlocked.
Here a bash command to do what you need:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
Just replace echo SCREEN_LOCKED
and echo SCREEN_UNLOCKED
with what you need.
edited Oct 20 '16 at 1:16
Riot
1935
1935
answered Jan 3 '12 at 2:57
peoro
1,47922222
1,47922222
Hi @peoro, That makes me think you can unlock or lock gnome screen/session from a scripted program, worth doing some ssh tricks about it ;-)
â Nikhil Mulley
Jan 3 '12 at 5:13
1
@Nikhil: to do that you don't need to play around with dbus:gnome-screensaver-command
is already there. Passing-a
tognome-screensaver-command
you'll lock the screen, while you'll unlock it with-d
. Anyway most gnome apps use dbus extensively, so you'll be able to do many amazing things with it.
â peoro
Jan 3 '12 at 5:18
1
@peoro Thanks great, very helpful! Can I also run this as some sort of daemon? When I enter this in the terminal now, it has to stay open to monitor the dbus for that case. I would like to execute this command at login and then it can be active during the entire session.
â Sander
Jan 4 '12 at 16:13
1
I think things may have changed now in 2014? as the output doesnt change if the screen was only locked, it only shows something when it gets blanked and is very different from here :(, I created this question askubuntu.com/questions/505681/â¦, do you believe there is still some way to do that? thx!
â Aquarius Power
Aug 2 '14 at 2:18
How to run that script that it catches the lock event? Kinda like a watcher.
â Starx
Nov 1 '16 at 13:03
 |Â
show 1 more comment
Hi @peoro, That makes me think you can unlock or lock gnome screen/session from a scripted program, worth doing some ssh tricks about it ;-)
â Nikhil Mulley
Jan 3 '12 at 5:13
1
@Nikhil: to do that you don't need to play around with dbus:gnome-screensaver-command
is already there. Passing-a
tognome-screensaver-command
you'll lock the screen, while you'll unlock it with-d
. Anyway most gnome apps use dbus extensively, so you'll be able to do many amazing things with it.
â peoro
Jan 3 '12 at 5:18
1
@peoro Thanks great, very helpful! Can I also run this as some sort of daemon? When I enter this in the terminal now, it has to stay open to monitor the dbus for that case. I would like to execute this command at login and then it can be active during the entire session.
â Sander
Jan 4 '12 at 16:13
1
I think things may have changed now in 2014? as the output doesnt change if the screen was only locked, it only shows something when it gets blanked and is very different from here :(, I created this question askubuntu.com/questions/505681/â¦, do you believe there is still some way to do that? thx!
â Aquarius Power
Aug 2 '14 at 2:18
How to run that script that it catches the lock event? Kinda like a watcher.
â Starx
Nov 1 '16 at 13:03
Hi @peoro, That makes me think you can unlock or lock gnome screen/session from a scripted program, worth doing some ssh tricks about it ;-)
â Nikhil Mulley
Jan 3 '12 at 5:13
Hi @peoro, That makes me think you can unlock or lock gnome screen/session from a scripted program, worth doing some ssh tricks about it ;-)
â Nikhil Mulley
Jan 3 '12 at 5:13
1
1
@Nikhil: to do that you don't need to play around with dbus:
gnome-screensaver-command
is already there. Passing -a
to gnome-screensaver-command
you'll lock the screen, while you'll unlock it with -d
. Anyway most gnome apps use dbus extensively, so you'll be able to do many amazing things with it.â peoro
Jan 3 '12 at 5:18
@Nikhil: to do that you don't need to play around with dbus:
gnome-screensaver-command
is already there. Passing -a
to gnome-screensaver-command
you'll lock the screen, while you'll unlock it with -d
. Anyway most gnome apps use dbus extensively, so you'll be able to do many amazing things with it.â peoro
Jan 3 '12 at 5:18
1
1
@peoro Thanks great, very helpful! Can I also run this as some sort of daemon? When I enter this in the terminal now, it has to stay open to monitor the dbus for that case. I would like to execute this command at login and then it can be active during the entire session.
â Sander
Jan 4 '12 at 16:13
@peoro Thanks great, very helpful! Can I also run this as some sort of daemon? When I enter this in the terminal now, it has to stay open to monitor the dbus for that case. I would like to execute this command at login and then it can be active during the entire session.
â Sander
Jan 4 '12 at 16:13
1
1
I think things may have changed now in 2014? as the output doesnt change if the screen was only locked, it only shows something when it gets blanked and is very different from here :(, I created this question askubuntu.com/questions/505681/â¦, do you believe there is still some way to do that? thx!
â Aquarius Power
Aug 2 '14 at 2:18
I think things may have changed now in 2014? as the output doesnt change if the screen was only locked, it only shows something when it gets blanked and is very different from here :(, I created this question askubuntu.com/questions/505681/â¦, do you believe there is still some way to do that? thx!
â Aquarius Power
Aug 2 '14 at 2:18
How to run that script that it catches the lock event? Kinda like a watcher.
â Starx
Nov 1 '16 at 13:03
How to run that script that it catches the lock event? Kinda like a watcher.
â Starx
Nov 1 '16 at 13:03
 |Â
show 1 more comment
up vote
18
down vote
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
)
ideas on how to make this work on fedora 23?
â Ray Foss
Apr 30 '16 at 21:49
2
Works fine on 16.04 as well
â Jacob Vlijm
Nov 29 '16 at 6:43
@JacobVlijm Thanks for testing this and green-lighting it for me to use tonight :)
â WinEunuuchs2Unix
Dec 8 '16 at 0:44
add a comment |Â
up vote
18
down vote
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
)
ideas on how to make this work on fedora 23?
â Ray Foss
Apr 30 '16 at 21:49
2
Works fine on 16.04 as well
â Jacob Vlijm
Nov 29 '16 at 6:43
@JacobVlijm Thanks for testing this and green-lighting it for me to use tonight :)
â WinEunuuchs2Unix
Dec 8 '16 at 0:44
add a comment |Â
up vote
18
down vote
up vote
18
down vote
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
)
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
)
answered Jun 24 '15 at 14:38
Luv Agarwal
19115
19115
ideas on how to make this work on fedora 23?
â Ray Foss
Apr 30 '16 at 21:49
2
Works fine on 16.04 as well
â Jacob Vlijm
Nov 29 '16 at 6:43
@JacobVlijm Thanks for testing this and green-lighting it for me to use tonight :)
â WinEunuuchs2Unix
Dec 8 '16 at 0:44
add a comment |Â
ideas on how to make this work on fedora 23?
â Ray Foss
Apr 30 '16 at 21:49
2
Works fine on 16.04 as well
â Jacob Vlijm
Nov 29 '16 at 6:43
@JacobVlijm Thanks for testing this and green-lighting it for me to use tonight :)
â WinEunuuchs2Unix
Dec 8 '16 at 0:44
ideas on how to make this work on fedora 23?
â Ray Foss
Apr 30 '16 at 21:49
ideas on how to make this work on fedora 23?
â Ray Foss
Apr 30 '16 at 21:49
2
2
Works fine on 16.04 as well
â Jacob Vlijm
Nov 29 '16 at 6:43
Works fine on 16.04 as well
â Jacob Vlijm
Nov 29 '16 at 6:43
@JacobVlijm Thanks for testing this and green-lighting it for me to use tonight :)
â WinEunuuchs2Unix
Dec 8 '16 at 0:44
@JacobVlijm Thanks for testing this and green-lighting it for me to use tonight :)
â WinEunuuchs2Unix
Dec 8 '16 at 0:44
add a comment |Â
up vote
3
down vote
Ubuntu 16.04: ozmaâÂÂs solution did not work for me, however this one did:
dbus-monitor --session "type=signal,interface=com.canonical.Unity.Session,member=Unlocked" |
while read MSG; do
LOCK_STAT=`echo $MSG | awk 'print $NF'`
if [[ "$LOCK_STAT" == "member=Unlocked" ]]; then
echo "was unlocked"
fi
done
It might work on Unity but question was in regards to Gnome.
â cprn
Jun 1 '17 at 10:03
add a comment |Â
up vote
3
down vote
Ubuntu 16.04: ozmaâÂÂs solution did not work for me, however this one did:
dbus-monitor --session "type=signal,interface=com.canonical.Unity.Session,member=Unlocked" |
while read MSG; do
LOCK_STAT=`echo $MSG | awk 'print $NF'`
if [[ "$LOCK_STAT" == "member=Unlocked" ]]; then
echo "was unlocked"
fi
done
It might work on Unity but question was in regards to Gnome.
â cprn
Jun 1 '17 at 10:03
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Ubuntu 16.04: ozmaâÂÂs solution did not work for me, however this one did:
dbus-monitor --session "type=signal,interface=com.canonical.Unity.Session,member=Unlocked" |
while read MSG; do
LOCK_STAT=`echo $MSG | awk 'print $NF'`
if [[ "$LOCK_STAT" == "member=Unlocked" ]]; then
echo "was unlocked"
fi
done
Ubuntu 16.04: ozmaâÂÂs solution did not work for me, however this one did:
dbus-monitor --session "type=signal,interface=com.canonical.Unity.Session,member=Unlocked" |
while read MSG; do
LOCK_STAT=`echo $MSG | awk 'print $NF'`
if [[ "$LOCK_STAT" == "member=Unlocked" ]]; then
echo "was unlocked"
fi
done
answered Oct 9 '16 at 12:09
Artemy Tregubenko
1312
1312
It might work on Unity but question was in regards to Gnome.
â cprn
Jun 1 '17 at 10:03
add a comment |Â
It might work on Unity but question was in regards to Gnome.
â cprn
Jun 1 '17 at 10:03
It might work on Unity but question was in regards to Gnome.
â cprn
Jun 1 '17 at 10:03
It might work on Unity but question was in regards to Gnome.
â cprn
Jun 1 '17 at 10:03
add a comment |Â
up vote
3
down vote
Expanding on already given answer.
If you try to run a script from inside a screen
or tmux
session, you'll need to find the correct $DBUS_SESSION_BUS_ADDRESS
first and pass it as an argument for dbus-monitor
instead of --session
. Also if you're running it as a daemon you should make sure only one instance is running at a time (e.g. with a lock file) and that the script cleans up after itself with trap
. The following example will work as a daemon in most current Gnome environments (tested on Ubuntu GNOME 16.04):
#!/bin/bash
set -o nounset # good practice, exit if unset variable used
pidfile=/tmp/lastauth.pid # lock file path
logfile=/tmp/lastauth.log # log file path
cleanup() # when cleaning up:
rm -f $pidfile # * remove the lock file
trap - INT TERM EXIT # * reset kernel signal catching
exit # * stop the daemon
log() # simple logging format example
echo $(date +%Y-%m-%d %X) -- $USER -- "$@" >> $logfile
if [ -e "$pidfile" ]; then # if lock file exists, exit
log $0 already running...
exit
fi
trap cleanup INT TERM EXIT # call cleanup() if e.g. killed
log daemon started...
echo $$ > $pidfile # create lock file with own PID inside
# usually `dbus-daemon` address can be guessed (`-s` returns 1st PID found)
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pidof -s dbus-daemon)/environ)
expr='type=signal,interface=org.gnome.ScreenSaver' # DBus watch expression here
dbus-monitor --address $DBUS_SESSION_BUS_ADDRESS "$expr" |
while read line; do
case "$line" in
*"boolean true"*) log session locked;;
*"boolean false"*) log session unlocked;;
esac
done
cleanup # let's not leave orphaned lock file when the loop ends (e.g. dbus dies)
If this doesn't work for you, it's probably because:
- you don't use Gnome - check other answers for better DBus watch expression
- you run multiple DBus lines - check details on how to make PID finding deterministic
1
This actually answers a different question I had for dynamically discovering the DBus session information given a user account, which I have solved here. Thanks for your contribution here!
â Naftuli Kay
Jun 2 '17 at 5:10
Thanks. I'll link your solution in the answer for further reading.
â cprn
Jun 2 '17 at 9:53
add a comment |Â
up vote
3
down vote
Expanding on already given answer.
If you try to run a script from inside a screen
or tmux
session, you'll need to find the correct $DBUS_SESSION_BUS_ADDRESS
first and pass it as an argument for dbus-monitor
instead of --session
. Also if you're running it as a daemon you should make sure only one instance is running at a time (e.g. with a lock file) and that the script cleans up after itself with trap
. The following example will work as a daemon in most current Gnome environments (tested on Ubuntu GNOME 16.04):
#!/bin/bash
set -o nounset # good practice, exit if unset variable used
pidfile=/tmp/lastauth.pid # lock file path
logfile=/tmp/lastauth.log # log file path
cleanup() # when cleaning up:
rm -f $pidfile # * remove the lock file
trap - INT TERM EXIT # * reset kernel signal catching
exit # * stop the daemon
log() # simple logging format example
echo $(date +%Y-%m-%d %X) -- $USER -- "$@" >> $logfile
if [ -e "$pidfile" ]; then # if lock file exists, exit
log $0 already running...
exit
fi
trap cleanup INT TERM EXIT # call cleanup() if e.g. killed
log daemon started...
echo $$ > $pidfile # create lock file with own PID inside
# usually `dbus-daemon` address can be guessed (`-s` returns 1st PID found)
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pidof -s dbus-daemon)/environ)
expr='type=signal,interface=org.gnome.ScreenSaver' # DBus watch expression here
dbus-monitor --address $DBUS_SESSION_BUS_ADDRESS "$expr" |
while read line; do
case "$line" in
*"boolean true"*) log session locked;;
*"boolean false"*) log session unlocked;;
esac
done
cleanup # let's not leave orphaned lock file when the loop ends (e.g. dbus dies)
If this doesn't work for you, it's probably because:
- you don't use Gnome - check other answers for better DBus watch expression
- you run multiple DBus lines - check details on how to make PID finding deterministic
1
This actually answers a different question I had for dynamically discovering the DBus session information given a user account, which I have solved here. Thanks for your contribution here!
â Naftuli Kay
Jun 2 '17 at 5:10
Thanks. I'll link your solution in the answer for further reading.
â cprn
Jun 2 '17 at 9:53
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Expanding on already given answer.
If you try to run a script from inside a screen
or tmux
session, you'll need to find the correct $DBUS_SESSION_BUS_ADDRESS
first and pass it as an argument for dbus-monitor
instead of --session
. Also if you're running it as a daemon you should make sure only one instance is running at a time (e.g. with a lock file) and that the script cleans up after itself with trap
. The following example will work as a daemon in most current Gnome environments (tested on Ubuntu GNOME 16.04):
#!/bin/bash
set -o nounset # good practice, exit if unset variable used
pidfile=/tmp/lastauth.pid # lock file path
logfile=/tmp/lastauth.log # log file path
cleanup() # when cleaning up:
rm -f $pidfile # * remove the lock file
trap - INT TERM EXIT # * reset kernel signal catching
exit # * stop the daemon
log() # simple logging format example
echo $(date +%Y-%m-%d %X) -- $USER -- "$@" >> $logfile
if [ -e "$pidfile" ]; then # if lock file exists, exit
log $0 already running...
exit
fi
trap cleanup INT TERM EXIT # call cleanup() if e.g. killed
log daemon started...
echo $$ > $pidfile # create lock file with own PID inside
# usually `dbus-daemon` address can be guessed (`-s` returns 1st PID found)
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pidof -s dbus-daemon)/environ)
expr='type=signal,interface=org.gnome.ScreenSaver' # DBus watch expression here
dbus-monitor --address $DBUS_SESSION_BUS_ADDRESS "$expr" |
while read line; do
case "$line" in
*"boolean true"*) log session locked;;
*"boolean false"*) log session unlocked;;
esac
done
cleanup # let's not leave orphaned lock file when the loop ends (e.g. dbus dies)
If this doesn't work for you, it's probably because:
- you don't use Gnome - check other answers for better DBus watch expression
- you run multiple DBus lines - check details on how to make PID finding deterministic
Expanding on already given answer.
If you try to run a script from inside a screen
or tmux
session, you'll need to find the correct $DBUS_SESSION_BUS_ADDRESS
first and pass it as an argument for dbus-monitor
instead of --session
. Also if you're running it as a daemon you should make sure only one instance is running at a time (e.g. with a lock file) and that the script cleans up after itself with trap
. The following example will work as a daemon in most current Gnome environments (tested on Ubuntu GNOME 16.04):
#!/bin/bash
set -o nounset # good practice, exit if unset variable used
pidfile=/tmp/lastauth.pid # lock file path
logfile=/tmp/lastauth.log # log file path
cleanup() # when cleaning up:
rm -f $pidfile # * remove the lock file
trap - INT TERM EXIT # * reset kernel signal catching
exit # * stop the daemon
log() # simple logging format example
echo $(date +%Y-%m-%d %X) -- $USER -- "$@" >> $logfile
if [ -e "$pidfile" ]; then # if lock file exists, exit
log $0 already running...
exit
fi
trap cleanup INT TERM EXIT # call cleanup() if e.g. killed
log daemon started...
echo $$ > $pidfile # create lock file with own PID inside
# usually `dbus-daemon` address can be guessed (`-s` returns 1st PID found)
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pidof -s dbus-daemon)/environ)
expr='type=signal,interface=org.gnome.ScreenSaver' # DBus watch expression here
dbus-monitor --address $DBUS_SESSION_BUS_ADDRESS "$expr" |
while read line; do
case "$line" in
*"boolean true"*) log session locked;;
*"boolean false"*) log session unlocked;;
esac
done
cleanup # let's not leave orphaned lock file when the loop ends (e.g. dbus dies)
If this doesn't work for you, it's probably because:
- you don't use Gnome - check other answers for better DBus watch expression
- you run multiple DBus lines - check details on how to make PID finding deterministic
edited Nov 14 '17 at 15:46
answered Jun 1 '17 at 18:25
cprn
3441414
3441414
1
This actually answers a different question I had for dynamically discovering the DBus session information given a user account, which I have solved here. Thanks for your contribution here!
â Naftuli Kay
Jun 2 '17 at 5:10
Thanks. I'll link your solution in the answer for further reading.
â cprn
Jun 2 '17 at 9:53
add a comment |Â
1
This actually answers a different question I had for dynamically discovering the DBus session information given a user account, which I have solved here. Thanks for your contribution here!
â Naftuli Kay
Jun 2 '17 at 5:10
Thanks. I'll link your solution in the answer for further reading.
â cprn
Jun 2 '17 at 9:53
1
1
This actually answers a different question I had for dynamically discovering the DBus session information given a user account, which I have solved here. Thanks for your contribution here!
â Naftuli Kay
Jun 2 '17 at 5:10
This actually answers a different question I had for dynamically discovering the DBus session information given a user account, which I have solved here. Thanks for your contribution here!
â Naftuli Kay
Jun 2 '17 at 5:10
Thanks. I'll link your solution in the answer for further reading.
â cprn
Jun 2 '17 at 9:53
Thanks. I'll link your solution in the answer for further reading.
â cprn
Jun 2 '17 at 9:53
add a comment |Â
up vote
2
down vote
If you're on Kubuntu or using KDE / Plasma as your Desktop Environment, you have to listen for the interface org.freedesktop.ScreenSaver
, so the script for listening to that event would look like this:
dbus-monitor --session "type='signal',interface='org.freedesktop.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
add a comment |Â
up vote
2
down vote
If you're on Kubuntu or using KDE / Plasma as your Desktop Environment, you have to listen for the interface org.freedesktop.ScreenSaver
, so the script for listening to that event would look like this:
dbus-monitor --session "type='signal',interface='org.freedesktop.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If you're on Kubuntu or using KDE / Plasma as your Desktop Environment, you have to listen for the interface org.freedesktop.ScreenSaver
, so the script for listening to that event would look like this:
dbus-monitor --session "type='signal',interface='org.freedesktop.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
If you're on Kubuntu or using KDE / Plasma as your Desktop Environment, you have to listen for the interface org.freedesktop.ScreenSaver
, so the script for listening to that event would look like this:
dbus-monitor --session "type='signal',interface='org.freedesktop.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
answered Apr 27 '17 at 7:55
Bob
1859
1859
add a comment |Â
add a comment |Â
up vote
2
down vote
Nowadays I think it's better to listen to the LockedHint
rather than screensaver messages. That way you're not tied to a screensaver implementation.
Here's a simple script to do that:
gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint
Gives this:
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <true>, @as )
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <false>, @as )
add a comment |Â
up vote
2
down vote
Nowadays I think it's better to listen to the LockedHint
rather than screensaver messages. That way you're not tied to a screensaver implementation.
Here's a simple script to do that:
gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint
Gives this:
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <true>, @as )
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <false>, @as )
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Nowadays I think it's better to listen to the LockedHint
rather than screensaver messages. That way you're not tied to a screensaver implementation.
Here's a simple script to do that:
gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint
Gives this:
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <true>, @as )
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <false>, @as )
Nowadays I think it's better to listen to the LockedHint
rather than screensaver messages. That way you're not tied to a screensaver implementation.
Here's a simple script to do that:
gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint
Gives this:
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <true>, @as )
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', 'LockedHint': <false>, @as )
answered Jun 10 at 23:33
Matthew
1212
1212
add a comment |Â
add a comment |Â
up vote
0
down vote
Where to write this code and how it will run:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
add a comment |Â
up vote
0
down vote
Where to write this code and how it will run:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Where to write this code and how it will run:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
Where to write this code and how it will run:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
while read x; do
case "$x" in
*"boolean true"*) echo SCREEN_LOCKED;;
*"boolean false"*) echo SCREEN_UNLOCKED;;
esac
done
edited Aug 17 at 7:46
Kevdog777
2,062113257
2,062113257
answered Aug 17 at 7:21
Shrawan Lal
1
1
add a comment |Â
add a comment |Â
up vote
-1
down vote
this is what worked for me in ubuntu 16.04
dbus-monitor --session "type=signal,interface=org.gnome.ScreenSaver" |
while read MSG; do
LOCK_STAT=`echo $MSG | grep boolean | awk 'print $2'`
if [[ "$LOCK_STAT" == "true" ]]; then
echo "was locked"
else
echo "was un-locked"
fi
done
This didn't work for me. As it finished executing and does not listen to the state changed.
â Starx
Nov 1 '16 at 13:12
which screen saver are you using? gnome or xscreensaver? which flavor ubuntu, xubuntu, kubuntu etc. which version (it was tested on 16.04)
â ozma
Nov 1 '16 at 14:06
ubuntu gnome 16.04
â Starx
Nov 2 '16 at 9:25
add a comment |Â
up vote
-1
down vote
this is what worked for me in ubuntu 16.04
dbus-monitor --session "type=signal,interface=org.gnome.ScreenSaver" |
while read MSG; do
LOCK_STAT=`echo $MSG | grep boolean | awk 'print $2'`
if [[ "$LOCK_STAT" == "true" ]]; then
echo "was locked"
else
echo "was un-locked"
fi
done
This didn't work for me. As it finished executing and does not listen to the state changed.
â Starx
Nov 1 '16 at 13:12
which screen saver are you using? gnome or xscreensaver? which flavor ubuntu, xubuntu, kubuntu etc. which version (it was tested on 16.04)
â ozma
Nov 1 '16 at 14:06
ubuntu gnome 16.04
â Starx
Nov 2 '16 at 9:25
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
this is what worked for me in ubuntu 16.04
dbus-monitor --session "type=signal,interface=org.gnome.ScreenSaver" |
while read MSG; do
LOCK_STAT=`echo $MSG | grep boolean | awk 'print $2'`
if [[ "$LOCK_STAT" == "true" ]]; then
echo "was locked"
else
echo "was un-locked"
fi
done
this is what worked for me in ubuntu 16.04
dbus-monitor --session "type=signal,interface=org.gnome.ScreenSaver" |
while read MSG; do
LOCK_STAT=`echo $MSG | grep boolean | awk 'print $2'`
if [[ "$LOCK_STAT" == "true" ]]; then
echo "was locked"
else
echo "was un-locked"
fi
done
answered Jul 26 '16 at 17:02
ozma
991
991
This didn't work for me. As it finished executing and does not listen to the state changed.
â Starx
Nov 1 '16 at 13:12
which screen saver are you using? gnome or xscreensaver? which flavor ubuntu, xubuntu, kubuntu etc. which version (it was tested on 16.04)
â ozma
Nov 1 '16 at 14:06
ubuntu gnome 16.04
â Starx
Nov 2 '16 at 9:25
add a comment |Â
This didn't work for me. As it finished executing and does not listen to the state changed.
â Starx
Nov 1 '16 at 13:12
which screen saver are you using? gnome or xscreensaver? which flavor ubuntu, xubuntu, kubuntu etc. which version (it was tested on 16.04)
â ozma
Nov 1 '16 at 14:06
ubuntu gnome 16.04
â Starx
Nov 2 '16 at 9:25
This didn't work for me. As it finished executing and does not listen to the state changed.
â Starx
Nov 1 '16 at 13:12
This didn't work for me. As it finished executing and does not listen to the state changed.
â Starx
Nov 1 '16 at 13:12
which screen saver are you using? gnome or xscreensaver? which flavor ubuntu, xubuntu, kubuntu etc. which version (it was tested on 16.04)
â ozma
Nov 1 '16 at 14:06
which screen saver are you using? gnome or xscreensaver? which flavor ubuntu, xubuntu, kubuntu etc. which version (it was tested on 16.04)
â ozma
Nov 1 '16 at 14:06
ubuntu gnome 16.04
â Starx
Nov 2 '16 at 9:25
ubuntu gnome 16.04
â Starx
Nov 2 '16 at 9:25
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f28181%2frun-script-on-screen-lock-unlock%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password