Run my Python script at login on linux

2019-08-19 15:57发布

问题:

I have a Python script, and I want it to be autostarted at every login. It's in a linux system. I followed a guide that explains that is enough to create a .desktop file in ~/.config/autostart/*.desktop and write:

[Desktop Entry]
Name=MyApp
Type=Application
Exec=python3 ~/.myapp/myapp
Terminal=false

I tried several times to reboot but the program doesn't execute, even if it seems to be active in the list of application of startup in my lxde environment.

回答1:

Often things like the ~ (tilde) aren't evaluated when placed in a config file. Try using the complete path instead (/home/user/… instead of ~/…) and see if this works. If this works, you can try to use $HOME instead ($HOME/…) to make this more portable and abstract.



回答2:

If you want to run your script on terminal login, place it to /etc/profile.d/

For KDE (at least, KDE 5) you could add applications to autorun in System Settings > Startup and Shutdown > Autostart (either *.desktop files or scripts), it adds links to ~/.config/autostart.



回答3:

You can achieve this by adding this line python /home/user/program.py in your .bashrc file. It will be invoked each time when you login to your system.