如何在Linux下通过.config/upstart/启动可见终端中的Python脚本
Got it, let's get your myscript.py running in a visible terminal window on boot instead of hiding in the background. Here's how to adjust your Upstart configuration:
Why It's Running in the Background Now
Your current start_python.conf executes the Python script directly via Upstart, which treats it as a background service—no terminal attached, hence only visible via ps aux | grep python. To get a visible window, we need to launch a terminal application first, then run your script inside it.
Step 1: Update Your User-Level Upstart Config
Since you placed the config in ~/.config/upstart/ (user-level), we'll leverage desktop session events to ensure the terminal has access to your X server (required for GUI windows).
Choose the Right Terminal Command
Pick the command matching your desktop environment's terminal:
For GNOME Terminal (most common for Ubuntu, Fedora, etc.)
Replace the contents of start_python.conf with this:
description "Launch myscript.py in a visible GNOME Terminal" start on desktop-session-start stop on desktop-shutdown # Replace /path/to/your/script with the actual folder containing myscript.py exec gnome-terminal -- bash -c "cd /path/to/your/script; python myscript.py; exec bash"
For XTerm (lightweight terminal)
description "Launch myscript.py in a visible XTerm" start on desktop-session-start stop on desktop-shutdown exec xterm -e "cd /path/to/your/script; python myscript.py; exec bash"
For Konsole (KDE desktop)
description "Launch myscript.py in a visible Konsole" start on desktop-session-start stop on desktop-shutdown exec konsole -e "cd /path/to/your/script; python myscript.py; exec bash"
Key Details to Note:
start on desktop-session-start: Ensures the terminal only launches after your desktop environment is fully loaded (so it can access theDISPLAYvariable required for GUI windows).cd /path/to/your/script: Replace this with the absolute path to the folder wheremyscript.pylives. If you skip this, the script might fail to find any local files it depends on.exec bashat the end: Prevents the terminal from closing immediately if your script exits unexpectedly. This lets you read error messages or restart the script manually.
Step 2: Test the Config Without Rebooting
You don't need to restart your machine to test. Run these commands in an existing terminal:
# Reload user-level Upstart configurations initctl --user reload-configuration # Start the service manually to verify initctl --user start start_python
A terminal window should pop up running your script. If it doesn't, check for typos in the path or terminal command.
If You Were Using a System-Level Config
If your original config was in /etc/init/ (system-level), move it to ~/.config/upstart/ first. System-level services run without access to your user's desktop environment, so they can't launch GUI terminals reliably.
内容的提问来源于stack exchange,提问作者beardswitcher

