How do I open a program in the background and exit the script with no output text?

Question:

For example, if I run Popen(["firefox", "https://example.org/"]), I get a message along the lines of

Gtk-Message: 21:33:04.621: Failed to load module "appmenu-gtk-module"

Is there a way to run that same Popen (or any equivalent) and have no messages printed to the screen, and then kill the program (main.py)?

Eg:

command(["firefox", "https://example.org/"])
exit(0)

Opens firefox, then automatically exits the program, leaving the command() still active, and with no output from command().

Asked By: ecjwthx

||

Answers:

you just have to start a new detached session and null stderr and stdout

from subprocess import Popen
import subprocess
import os

process = Popen(["firefox", "https://example.org/"],
                stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
                start_new_session=True)
exit(0)
Answered By: Ahmed AEK
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.