how to get a process launched from a script not to die when the script ends?

7

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

    
asked by AlmuHS 26.03.2018 в 19:18
source

2 answers

0

After doing several tests, I've already got the script to launch the dbus client and end it, leaving the dbus client running.

Launching the script from terminal I see that the script ends instantaneously, and that the dbus client is still running.

The script code is this:

#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      
   su $user -c '/usr/bin/pendrive-reminder/client.py' &
done

The client's code has stayed 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()
    
answered by 31.03.2018 / 22:26
source
2

Besides nohup, you have to add & disown at the end of the process to disconnect it from the active process list of the shell that launches it.

Example:

proceso & disown
    
answered by 26.03.2018 в 21:56