![如何使用 Python 脚本将程序添加到启动应用程序?](https://linux22.com/image/897576/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20Python%20%E8%84%9A%E6%9C%AC%E5%B0%86%E7%A8%8B%E5%BA%8F%E6%B7%BB%E5%8A%A0%E5%88%B0%E5%90%AF%E5%8A%A8%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%EF%BC%9F.png)
我想使用另一个 Python 脚本将 Python 脚本添加到启动中。有办法吗?
答案1
按照本规范,放置 .desktop 文件应该~/.config/autostart
可以工作。所以基本上你的 python 脚本的任务是
- 将 Python 脚本放在某处
- 将 .desktop 文件放在 下
~/.config/autostart
。
以下是此类脚本的示例
import os
autostart_path = os.path.expanduser('~/.config/autostart/')
nameofmyscript = 'myscript.py'
nameofmydesktopfile = 'myscript.desktop'
mypythonscript = """#!/usr/bin/python
print("hello")"""
desktopfile = """[Desktop Entry]
Type=Application
# The version of the desktop entry specification to which this file complies
Version=1.0
# The name of the application
Name=Script
# A comment which can/will be used as a tooltip
Comment=My cool python script
# The path to the folder in which the executable is run
Path=%s
# The executable of the application, possibly with arguments.
Exec=%s
# Describes the categories in which this entry should be shown
Categories=Education;Languages;Python;
""" % (autostart_path, nameofmyscript)
# write the desktop file
with open(autostart_path + nameofmydesktopfile, 'w+') as script:
script.write(desktopfile)
# write the python script; you can place it anywhere actually, just be sure to correct the desktop
# file accordingly
with open(autostart_path + nameofmyscript, 'w+') as script:
script.write(mypythonscript)
os.system('chmod +x ' + autostart_path + nameofmyscript)