4

I'm porting some Qt Windows/VC++ code to Linux/GCC. The application can add it's own shortcut to the Windows Autostart folder so the application starts after login.

I want to do the same in Linux. I'm using Kubuntu 15.10 but the solution should work for virtually all (or at least most) Linux variants out there. And it should work without super user rights (or it should request the rights automatically).

I searched the web and found two solutions:

  1. Add a desktop entry file to $HOME/.config/autostart
  2. Add a symbolic link to /etc/init.d/

Will they both work in all Linux distributions? What are the differences? Which is to be preferred?

Also I would like to know if I should do that by programmatically running a shell command or if there is some native API I could use in C/C++ (including easy error detection).

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
  • 1
    Are you aware how those two things you mention differ? – Ulrich Eckhardt Dec 14 '15 at 07:09
  • "the solution should work for virtually all (or at least most) Linux variants out there" - that's almost impossible. – el.pescado - нет войне Dec 14 '15 at 07:41
  • @Ulrich. Not exactly. I assume that the home based solution is user specific while the other one is system wide, is this correct? Maybe there are also differences in necessary user rights and/or when the application is started? – Silicomancer Dec 14 '15 at 07:53
  • Correct, anything under `/etc` is system-wide, under `/home` it is user-specific. That also means that if no user is logged in, nothing is running and if two users are logged in, the same program could be started twice. It seems that you want at most one program running in the system, since it always runs as root. However, that would have to run without an X session. If you need a UI, you could also install the application with SUID-bit set and then use a per-user autostart. Lastly, you could also start a server on demand, e.g. via xinetd if it operates as a networked server. – Ulrich Eckhardt Dec 16 '15 at 07:20

2 Answers2

3

I have put project in GitHub for managing auto-start feature in different OS. It's written in Qt. Please check it and let me know if you have any problem using it: https://github.com/b00f/qautostart

Mostafa
  • 670
  • 1
  • 10
  • 20
1

You can add application in various ways.

  1. Via linux init system. For newest linux OS systemd is a standard. In this case your need to create systemd unit for your application
  2. Via desktop manager, such as gnome, kde and possible others. In this case you need also create specification for autostarting your app.
  3. Via bash files

I think, prefered way via systemd unit, because now this is standard way for starting process at boot time and for special user, if need.

Community
  • 1
  • 1
synacker
  • 1,722
  • 13
  • 32
  • If I understand correctly I would create some new link or file by executing a shell command for each of these solutions. So there is no such thing like a C++ API for that task? – Silicomancer Dec 14 '15 at 08:02
  • That GUI applications needs user interaction to do anything useful. This is why I planned to start it *after* login. Could it generate any problem when starting it *before* login (on boot) using systemd? – Silicomancer Dec 14 '15 at 10:55
  • 1
    @Silicomancer your can specify systemd unit for user or via desktop manager file specialization in this case. – synacker Dec 14 '15 at 14:05
  • systemd seems to work well. I tried to use D-Bus for systemd control but it looks like user session D-Bus of systemd is buggy in some way. So I am using comand line tools instead. – Silicomancer Dec 21 '15 at 20:51
  • @Silicomancer your mean that you create systemd unit via command line or what? – synacker Dec 22 '15 at 07:49
  • 1
    No, I am creating the .service file using normal file operations. But I tried to do the necessary enabling/disabling of the unit using D-Bus. However as I mentioned, it doesn't work for user units due to D-Bus problems. For system units it works. – Silicomancer Dec 22 '15 at 14:15