Estoy intentando usar una regla udev para lanzar un cliente dbus desde un script.
La regla udev saltará cuando se conecte un pendrive, y ejecuta el script usbdevinserted.sh
, el cual copiara el identificador del pendrive a un fichero y lanzará el cliente dbus
La regla udev es esta:
KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/usr/bin/pendrive-reminder/usbdevinserted.sh"
El código de usbdevinserted.sh
este:
set 2>&1 | grep DEVPATH | cut -d "=" -f 2 >> /tmp/usbdevinfo
#Get list of users with graphic session started, and their active display
userdisplay=$(who | gawk '/\(:[[:digit:]](\.[[:digit:]])?\)/ { print $1 ";" substr($NF, 2, length($NF)-2) }' | uniq)
#for each user, show notification and (only in polkit >= 106) launch dbus client
for element in $userdisplay
do
#get username
user=$(echo $element | cut -d ";" -f 1)
#get display active of this user
export DISPLAY=$(echo $element | cut -d ";" -f 2)
#Send notification to user
su $user -c 'notify-send "Pendrive Reminder" "Shutdown lock enabled. The shutdown will be unlocked when pendrive is disconnected"'
#if polkit version >=106, also launch dbus client
if test $polkit_version -ge 106
then
su $user -c 'python /usr/bin/pendrive-reminder/client.py' &
fi
done
Y el cliente dbus (client.py
) es este:
#!/usr/bin/python3
from gi.repository import GLib
from gi.repository import Notify
import dbus
from dbus.mainloop.glib import DBusGMainLoop
dbus_loop = DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus(mainloop=dbus_loop)
loop = GLib.MainLoop()
def msg_handler(*args,**keywords):
try:
#show notification to desktop
Notify.init('Pendrive Reminder')
notify = Notify.Notification.new('Pendrive Reminder', 'Shutdown lock enabled. Disconnect pendrive to enable shutdown')
notify.show()
except:
pass
bus.add_signal_receiver(handler_function=msg_handler, dbus_interface='org.preminder', path_keyword='path')
loop.run()
La idea es que el script (usbdevinserted.sh
) termine, dejando al cliente dbus en ejecución.
He probado lanzándolo manualmente desde la terminal, y funciona bien: el script finaliza y deja al cliente dbus en ejecución.
Pero, cuando lanzo el script desde la regla udev, el script, aún habiendo ejecutado su secuencia de instrucciones, se queda zombie y no muere hasta pasados unos minutos, tiempo en el cual deja udev bloqueado.
Y, si lanzo el script desde udev, quitando la parte en que se lanza el cliente dbus (client.py
), el script finaliza correctamente sin quedarse zombie.
¿sabéis donde puede estar el problema?
ACTUALIZACIÓN
Tal como me han indicado en los comentarios, he probado con:
su $user -c '/usr/bin/pendrive-reminder/client.py &>/dev/null' & | at now
Pero ahora el cliente dbus no se llega a lanzar (o muere junto al script) También he probado con:
su $user -c '/usr/bin/pendrive-reminder/client.py &>/dev/null' & - batch
Pero el script no llega a finalizar (ni tan siquiera como zombie)
Y con:
su $user -c '/usr/bin/pendrive-reminder/client.py &>/dev/null' & - at
el script se vuelve a quedar zombie hasta unos minutos despues