-1

I want to make my qt project run while booting raspberry pi3. When I searched on the web there is a solution for python. Eventhough I put some code on rc.local, this did not solve my problem.

The application has a GUI and the OS is Raspbian Stretch.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
aysenur
  • 1
  • 1
  • 1
  • 1
    This will be dependent on the OS you are running on your Pi. You should add this info to your question. – Glen Yates May 04 '18 at 13:25
  • What happens when you add it to the rc.local file? Have you been able to log the output, and does that reveal anything? – Aurora0001 May 04 '18 at 13:43
  • Since you are using QT, you might want to make it explicit whether this is a GUI application you expect to open in the foreground, or something that can run in the background (and if so whether it is finite event or a persistent daemon). – goldilocks May 04 '18 at 13:54
  • Application has GUI and the OS is Raspbian Stretch. – aysenur May 04 '18 at 14:25
  • Thanks. In future edit information into your question and leave a comment @goldilocks to let me know. – goldilocks May 04 '18 at 14:34
  • Is this something that is supposed to just happen for a bit while the system boots, or is it an end purpose (i.e., there will be user interaction, or the application will run indefinitely)? – goldilocks May 04 '18 at 14:39
  • There is a program for 3d printing. I just want user to see my application not the Desktop. @goldilocks – aysenur May 07 '18 at 06:52
  • "In future edit information into your question..." I should have added "and not into comments". Anyway, if you had made this all clear in the beginning, I or someone could have pointed you to a duplicate question that uses the methodology you eventually found yourself. The problem is by saying less rather than more you leave some ambiguities even if (or, because) you are unaware of them: 1) Technically what you are doing does not take place "while booting", it takes place once booting is mostly complete. 2) You are talking about a long running task, not a short one. – goldilocks May 07 '18 at 11:36

2 Answers2

3

I guess you are using Raspbian Stretch. This comes with systemd so you should start your qt project as service. First create a new service:

rpi3 ~$ sudo systemctl edit --force --full my_qt_project.service

Insert this statements with your settings, save them and quit the editor:

[Unit]
Description=My Qt Project Service

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi
ExecStart=/home/pi/my_qt.project

[Install]
RequiredBy=graphical.target
# Look at the comments, maybe you can use
#WantedBy=graphical.target

Check the new service:

rpi3 ~$ systemctl status my_qt_project.service
● my_qt_project.service - My Qt Project Service
   Loaded: loaded (/etc/systemd/system/my_qt_project.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Now you can enable and test your service:

rpi3 ~$ sudo systemctl enable my_qt_project.service
rpi3 ~$ sudo systemctl start my_qt_project.service

Here are some commands that may help. After editing you must restart the service to take effect.

rpi3 ~$ systemctl status my_qt_project.service
rpi3 ~$ systemctl cat my_qt_project.service
rpi3 ~$ sudo systemctl edit --full my_qt_project.service
rpi3 ~$ sudo systemctl restart my_qt_project.service


references
[1] man systemd.unit
[2] Running a script after an internet connection is established
[3] No access to USB port, when running python script on boot

Ingo
  • 42,107
  • 20
  • 85
  • 197
  • That this is a QT app implies it is has a GUI which will require the X server, etc. Are you sure this approach will work as is for that? I'm guessing it doesn't... – goldilocks May 04 '18 at 13:52
  • To suggest a solution to the problem goldilocks pointed out, you could guarantee that the DE is ready by replacing the line beginning with WantedBy by Requires=graphical.target. See this graph to see the rough order of units being loaded; WantedBy=multi-user.target is almost certainly too early. – Aurora0001 May 04 '18 at 14:01
  • I did the same as you said. Service started without error but does not start my application. In Line ExecStart=/home/pi/my_qt.project, I wrote the path of my executable file not the pro file. Is this true? Later I changed the WantedBy.. line as told in the comment. I wrote Requires=graphical.taget instead of this. But now gives error while checking status. The error is; Active:failed(Result: signal) Main process exited, code=killed, status=6/ABRT – aysenur May 04 '18 at 14:22
  • Looking around a bit I think you will also have to specific, e.g., Environment="DISPLAY=:0". – goldilocks May 04 '18 at 14:37
  • @Aurora0001 Yes, for a GUI we need the graphical.target but in the [Install] section the parameter is called RequiredBy=. But in this unclear situation for a first step I would prefer WantedBy because quote: "Often, it is a better choice to use Wants= instead of Requires= in order to achieve a system that is more robust when dealing with failing services." (man systemd.unit) – Ingo May 04 '18 at 15:11
  • 1
    @Ingo I had thought that WantedBy and RequiredBy means that your unit must run before the specified one, whereas Wants and Requires run after the specified (which is probably what you want, as you need the GUI to be ready before running). You're right that Wants is more fault tolerant, but in this case I might suggest that it'd be better for the unit to fail than to try and run after graphical.target failed, which would happen if Wants was used. Still, it's an edge case and probably won't matter. – Aurora0001 May 04 '18 at 15:16
  • 1
    @aysenur as you see at the comments your information is to small. There are too many possibilities we have to guess. Maybe there are other services that have to start before or after your application. You have to examine environment/rights for your application. Please correct the [Install] section. Does it run from a command line? – Ingo May 04 '18 at 15:19
0

Thank you for your answers. Adding the execution path of the application with sudo command ( @sudo /path/to/QtPro ) to the following file solved the problem. ~/.config/lxsession/LXDE-pi/autostart

aysenur
  • 1
  • 1
  • 1
  • The sudo is likely superfluous there unless the application itself requires privileges (i.e., the pi user must use sudo to start it normally). Also note this will only work at boot if autologin for the pi user is enabled. It is probably a better choice than using systemd for this kind of thing. – goldilocks May 07 '18 at 11:29