ardera / flutter-pi

A light-weight Flutter Engine Embedder for Linux Embedded that runs without X11 or Wayland.
MIT License
1.64k stars 162 forks source link

How to launch your flutterpi app at startup #461

Open vanlooverenkoen opened 1 week ago

vanlooverenkoen commented 1 week ago

What is the best way to launch your app at startup and have keyboard support & touch support (basicly everything)

This is what I followed. And it works. but this keeps a terminal open where the keyboard types

Launch script

Create a script here ~/projects/launch_my_app.sh

#!/bin/bash
flutter-pi --release projects/my_app

Boot service

Here's a comprehensive setup guide for creating a systemd service that will automatically launch your Flutter app on a Raspberry Pi using your custom startup script.

1. Create the Systemd Service File

Open a terminal on your Raspberry Pi and create a new service file for your app:

sudo nano /etc/systemd/system/my_app.service

2. Configure the Service File

In the editor, paste the following configuration. Adjust the paths and usernames as necessary.

[Unit]
Description=Launch Flutter App
After=network.target

[Service]
ExecStart=/home/pi/projects/launch_my_app.sh
Restart=on-failure
User=pi
Environment=DISPLAY=:0

[Install]
WantedBy=multi-user.target

3. Enable the Service to Start on Boot

After configuring the service file, save and close the editor (Ctrl+X, then Y, then Enter). Then, enable the service to run on boot:

sudo systemctl enable my_app.service

4. Test the Service

To test that everything is set up correctly, start the service manually:

sudo systemctl start my_app.service

You can check the status to verify that it’s running without errors:

systemctl status my_app.service

5. Reboot to Confirm Auto-Start

To ensure the service starts on boot, reboot your Raspberry Pi:

sudo reboot

Once the system reboots, you can confirm that your app is running by checking the service status again:

systemctl status my_app.service

This setup ensures your Flutter app will start automatically each time the Raspberry Pi reboots, using your launch_my_app.sh script for custom setup.

6. Stop the service

sudo systemctl stop my_app.service

The problem

This works, only the keyboard is still typing in the terminal that is still open behind the flutter app

effmuhammad commented 1 week ago

Currently I use cron job to run program on boot, all functionalities (also touch and keyboard) are working like a charm

sudo crontab -e

then in the bottom of the text editor (you can use vim, nano, etc) add this line:

@reboot /home/[your-username]/projects/launch_my_app.sh >> /home/[your-username]/projects/launch_my_app.log

*note: change [your-username] with your actual username

Then save the cron settings

vanlooverenkoen commented 1 week ago

@effmuhammad cool! I have a functionality "restart app" or "restart on crash" how would you do that with a cronjob? The service works great because you can say restart of fail so you get it out of the box.

effmuhammad commented 1 week ago

@vanlooverenkoen as I know the cronjob not support it by default, but i found a convincing solution in stackexchange. I will check it later