1

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.

John-Philip
  • 3,392
  • 2
  • 23
  • 52
Allexj
  • 1,375
  • 6
  • 14
  • 29
  • 1
    At boot or at login time? login of a specific user or of any user? – Alfe Jul 19 '17 at 11:25
  • to be more specific, at login time. the login is of a specific or any, doesn't matter. But at the moment I'm trying on a specific user, not root – Allexj Jul 19 '17 at 11:27
  • Did you try a complete path like `/home/allexj/.myapp/myapp`? Does this script have x-permissions set? – Alfe Jul 19 '17 at 11:30
  • I wrote "chmod +x myapp" in terminal and I also tried as you suggested to write "/home/user/.myapp/myapp" instead of "~/.myapp/myapp" but it doesn't work anyway – Allexj Jul 19 '17 at 11:42
  • Did you see anything enlightening in the X log file (`~/.xsession-errors` or similar)? Did you try a complete path for the `python3` as well (e. g. `/usr/bin/python3`, look at `type python3` to figure out the path)? – Alfe Jul 19 '17 at 11:45
  • Is your application GUI based? Your comment on bakatrouble's answer suggests it isn't. Then how about putting its start into your shell's login scripts? That could be either `~/.profile` or `~/.login` or `~/.bash_profile`. That would mean every login (also from remote via ssh or similar) might trigger this. – Alfe Jul 19 '17 at 11:53
  • 1
    First I was wrong, it worked when I put in the .desktop file "/home/user/.myapp/myapp" instead of "~/.myapp/myapp". Thanks – Allexj Jul 19 '17 at 11:56
  • is there a way to identify in that user home is the script executing?Because I need to run this script in other computers – Allexj Jul 19 '17 at 11:59
  • See my answer about using `$HOME` instead of `~`. Maybe this works, depending on the program reading and interpreting the file you write this in (your window manager or similar). – Alfe Jul 19 '17 at 12:00

3 Answers3

1

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.

bakatrouble
  • 1,746
  • 13
  • 19
  • It's probably a windows-based thingy because of the `Terminal=false`. You wouldn't want to execute this on each ssh login or similar. – Alfe Jul 19 '17 at 11:31
  • I need to to work just on home directory. Because /etc/ /root/ or something else is with root permissions and it's not what I need – Allexj Jul 19 '17 at 11:34
  • thanks @bakatrouble but I need a method that I can implement to the code and can be executed directly from the script, and your gui method wouldn't be managed from my script – Allexj Jul 19 '17 at 11:37
0

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.

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • thanks @Alfe it worked. But the try to use $HOME instead of ~ didnt. is there a way to identify in what user home is the script executing? – Allexj Jul 19 '17 at 12:09
  • You could call a bash to do the evaluation for you: `Exec=bash -c $HOME/.myapp/myapp`. Insert `#!/usr/bin/env python3` as first line in your Python script and make sure it is executable. – Alfe Jul 19 '17 at 12:22
  • And if you type `$HOME/.myapp/myapp` in a bash shell directly? does this properly run your app? – Alfe Jul 19 '17 at 12:40
  • I also tried this before. It tells "no such file or directory", even if the auto completion with Tab key works, so the terminal sees the file – Allexj Jul 19 '17 at 12:42
  • I found out how to get the home path here: https://stackoverflow.com/questions/10170407/find-home-directory-in-python – Allexj Jul 19 '17 at 12:46
0

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.

user7345878
  • 492
  • 2
  • 7
  • 20