I need a script for when I press Ctrl + Alt + L (Lock screen) I blocked or shut down all usb's and shut down all my network interfaces like eth0.

When I Unlock, everything should be back to normal. I tried

echo suspend> / sys / bus / usb / devices / usb1 / power / level 

.. but I could not ..

And also

dbus-monitor --session "type = 'signal', interface = 'org.gnome.ScreenSaver' 

to interact with the screen but could not too.

Is there any better method? Or something I can study to create this script?

  • better approach would be to bind a script to the ctrl+alt+L shortcut , which will do whatbyou want and only then lock the screen. I would do that but lately i am short on time. Perhaps someone else can do this. If not, i might come back and write one – Sergiy Kolodyazhnyy Nov 29 '16 at 0:53
  • I just want a script for when I lock the screen, disable all USB ports and my interfaces like eth0. I tried to use something like, dbus-monitor --session "type = 'signal', interface = 'org.gnome.ScreenSaver'" | While read x; of Case "$ x" in * "Boolean true" * echo suspend> / sys / bus / usb / devices / usb1 / power / level ...... But it does not work – nullXOR Nov 29 '16 at 2:12
  • @nullXOR posted my answer. Please mention if all is clear. – Jacob Vlijm Nov 29 '16 at 9:20

How to run scripts or commands on changes in lock state

According to this answer on U&L, you tried an outdated dbus command. For 14.04 and up, you can use:

dbus-monitor --session "type='signal',interface='com.ubuntu.Upstart0_6'" | \
  while read X; do
    if [[ "$X" =~ desktop-lock ]]; then
    elif [[ "$X" =~ desktop-unlock ]]; then

which works fine on my system (16.04).
(Thanks for improvements by @Serg and @muru)

Of course you need to replace SCREEN_LOCKED and SCREEN_UNLOCKED by the commands or scripts you want to run on changes in lock state, but tested it, and it works fine.

|improve this answer|||||
  • If you're using bash, you can simplify it to if [[ $X =~ desktop-lock ]]; then .. (and similarly for the other one). And maybe use while read X instead of while true (does the read ever fail other than when the dbus-monitor process exits?) – muru Nov 30 '16 at 6:20
  • 1
    doesn't seem to work on ubuntu 17.04. The ouput of dbus-monitor --session "type='signal',interface='com.ubuntu.Upstart0_6'" is signal time=1497336035.520628 sender=org.freedesktop.DBus -> destination=:1.140 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired string ":1.140" signal time=1497336035.520706 sender=org.freedesktop.DBus -> destination=:1.140 serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost string ":1.140", then nothing when I lock or unlock – maxbellec Jun 13 '17 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.