14.04 - How can I run a local command (to run a script) on (just before) log out of a Unity session? - Ask Ubuntu


at risk of posting duplicate question:

how can run local script (as current user) when log out?

i read (and tried) among others:

  1. this one (marton's version), did nothing, used on simple script creating file, absolute paths script (being executable) , file.
  2. this one, must outdated; made me remove file command line able use system again.
  3. i looked this one, without succes.

i believe must doing wrong here, cannot find out blind spot.

context

looking elegant solution (workaround) this question, presumably result of bug, made script that, depending on argument, either takes snapshot of current desktop icon arrangement, or arranges icons, according last snapshot. script works fine, no candidate run background script.

the script therefore should run on logout get arrangement, run current user.

basic theory

as may or may not know , lot of unity actions implemented using dbus . big convenience dbus can perform lots of root-required actions calling appropriate methods in command-line without need of having admin account, instance shutting down system.

specifically in our case here interested in com.canonical.unity service, /com/canonical/unity/session path. can list information qdbus command. particularly we're interested in signals have requested in them.

$ qdbus com.canonical.unity /com/canonical/unity/session | grep 'signal.*requested.*'                              signal void com.canonical.unity.session.lockrequested() signal void com.canonical.unity.session.logoutrequested(bool have_inhibitors) signal void com.canonical.unity.session.rebootrequested(bool have_inhibitors) signal void com.canonical.unity.session.shutdownrequested(bool have_inhibitors) signal void com.canonical.unity.session.unlockrequested() 

these signals appear on bus when user clicks of shutdown, lock, logout, or reboot items in drop-down session menu on right side of unity's top bar. need this:

  • monitor bus signals appear.
  • once appear - perform action.

of course , big disclamer if user uses sudo shutdown -p now or gnome-session-quit , won't told dbus , whatever action wanted won't done.

monitoring dbus

conveniently there command that, dbus-monitor , can filter output using set of options, , perform action on using while read … done structure

bellow example of simple monitoring script.

dbus-monitor --profile "interface='com.canonical.unity.session',type=signal" | \ while read -r line;   echo "$line"    sleep 0.25 done 

what merely monitor bus signals , , echo them stdout if appear; of course echo command. run , , try clicking on each item in session menu should generate signals, , should see this:

$ ./logout_monitor.sh                                                                                              sig 1458319246  172587  2   /org/freedesktop/dbus   org.freedesktop.dbus    nameacquired sig 1458319251  213766  5496    /com/canonical/unity/session    com.canonical.unity.session rebootrequested sig 1458319265  62555   5525    /com/canonical/unity/session    com.canonical.unity.session logoutrequested sig 1458319271  856770  5555    /com/canonical/unity/session    com.canonical.unity.session lockrequested sig 1458319273  223940  5564    /com/canonical/unity/session    com.canonical.unity.session locked sig 1458319276  991413  5604    /com/canonical/unity/session    com.canonical.unity.session unlockrequested sig 1458319278  3443    5606    /com/canonical/unity/session    com.canonical.unity.session unlocked 

so have monitoring function can perform action once interrupt occurs. , address specific question trying solve , script taking snapshots of icons on desktop. there's no harm done if take snapshot each time signal occurs - doesn't have reboot or shutdown specifically. if monitor specific unity interface signals , take snapshots upon each signal's arrival, can simplify scripting.

interrupt-driven script

bellow can see interrupt-driven script monitor bus signals (ignoring first 1 ) , if have signal coming in , call interrupt function (which in case icon snapshot script you've written).

#!/bin/bash  main() {   arg="cow"   dbus-monitor --profile "interface='com.canonical.unity.session',type=signal" |    while read -r line;       grep -q '.*nameacquired.*' <<< "$line"  && continue  #  ignore first line     if [ -n "$line"  ];then        interrupt $arg # call python snapshot script here     fi   done }  interrupt() {   echo 'old mcdonald had ' $arg ' e-i-e-i-o ' }  main 

note: dbus relies on having gui session , since (as far know) starts unity/gnome login. older environments such blackbox , openbox, tty environments do not start dbus session, hence script puke if try launch in ~/.profile (i had 1 user tried , got trouble). start such script using unity's or gnome's startup applications program.

going beyond script

  • dbus has lot of api's including python
  • i prefer using qdbus because it's simple, 1 use dbus-send . examples
  • the interrupt function can anything, other dbus methods. there's lot into.

trivia: i've used same method preventing shutdown if apt running


Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User