I'm trying to launch a dbus client, programmed using a Python script, from another script launched using a udev rule (which runs as root), and I need this dbus client to remain running when the script ends.
The dbus client to launch is:
#!/usr/bin/python3
from gi.repository import Gtk
from gi.repository import Notify
import dbus
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
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')
Gtk.main()
I need to get the script to launch a dbus client for each existing user in the system, continue its execution until it finishes, and ends; without killing the clients you just launched.
I'm trying this way, using nohup
:
#Get online users list
user_list=$(who | cut -d " " -f 1)
#Set display
export DISPLAY=":0"
#For each user, launch dbus client
for user in $user_list
do
nohup su $user -c '/usr/bin/pendrive-reminder/client.py' &
done
But, by launching it like this, the dbus client leaves the script blocked, which, once its sequence of instructions is finished, remains running in the "defunct" state (as a zombie process).
And, in doing so, it also blocks udev, which stops responding to the following system events
how could I solve it?
UPDATE
I have changed the library that I use for the main loop of the dbus client, removing Gtk and using GLib
The code looks like this:
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()
Now, after this, I've noticed some changes:
-
If I launch the dbus client manually from the terminal, it ends when I do ctrl + c (the previous one was running and had to end it with the order
kill
) -
If I launch it from the script, with
nohup su $user -c '/usr/bin/pendrive-reminder/client.py' & disown
the script ends its instructions, and ends a few minutes later (before it never ended).
Now what I need is that the script, instead of finishing after a few minutes, ends immediately