4

I was to make a launcher application, I haven't found a way to detach a sub-process entirely from the spawning python process.

When I launch a program with my desktop's (cinnamon's) launcher the process tree goes:

/sbin/init -> mdm -> mdm -> cinnamon-session -> cinnamon -> the-app-i-launched

Of the threads I read, this one was the most insightful/helpful: Launch a completely independent process. But gets muddied answers as the OP is looking to run python code, which can often be achieved in many usually-preferred ways than by spawning an independent process.

From other posts from stack overflow that do not answer how to launch a detatched python process:

  • Running daemonalized python code: Applicable to running python code/module as a daemon, (not another process/application) detached from the python instance.
  • subprocess.call: Process spawns as a child of the python process.
  • os.system: Process spawns as a child of the python process.
  • close_fds: (Apparently) Windows(R)-only solution, need portable solution (primary target is Debian linux). Attempting to use close_fds=True on linux, process spawns as a child of the python process.
  • creationflags: Windows(R)-only solution. On linux raises: ValueError: creationflags is only supported on Windows platforms.
  • prefix launched process with nohup: Process spawns as a child of the python process. As far as I know, nohup or equivalent is not available on all platforms, making it a linux-only solution.
  • os.fork: Same as "Running daemonalized python code".
  • multiprocessing: Same problem as "Running daemonalized python code": useful only for running python code/module.
  • os.spawnl* + os.P_NOWAIT: Deprecated functions are undesirable to use for new code. In my testing I was not able to see my process actually spawned at all.
  • os.spawnl* + os.P_DETACH: Windows(R)-only, seemingly removed in current python 2.X versions: AttributeError: 'module' object has no attribute 'P_DETACH'.
  • os.system + shell fork: I was able to actually see my process run detatched from the python process with this, however I am woried that it has the faults:
    • Relies on running commands in a shell, which is more vulnerable to maliciousness, intentional or otherwise?.
    • Relies on non-portable? POSIX/shell? syntaxies that may not be interpenetrate on non-Linux platforms. Which I haven't dug up any good reference for portability on Partial Ref.
  • subprocess.Popen Alt: I still only observed the sub-process running as a child of the python process.
1
  • i think the question i was trying to ask is "how to disown a process" and I'm not sure there is really a way to disown a process completely, at best it can maybe be owned by the spawning process's parent process. My objective was to run a process that could survive after the launching process closed, I think the only viable option is to host that process from a daemon meant to host other processes, i think most shells, including desktop shells, own this responsibility which is why "using bash" seemed like a viable option. albeit somewhat a confusing mess. Please let me know Commented Jun 16, 2021 at 4:00

2 Answers 2

1

A working solution can be found in JonMc's answer here. I use it to open documents using 'xdg-open'.

You can change the stderr argument to stderr=open('/dev/null', 'w'), if you do not want a logfile.

Sign up to request clarification or add additional context in comments.

Comments

-1

The only workable solution I have, and that may be non-portable Linux-only is to use shell evaluation with shell-detach-ampersand syntax.

#!/usr/bin/env python2 import os os.system('{cmd} &'.format('firefox')) 

This might go too-far up the process tree though, outside of the window manager session, potentially not-exiting with your desktop session.

/sbin/init -> firefox

1 Comment

I think this way the process is not detached.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.