Running a program on boot (Pi 4)

I have been trying to get a program to start automatically on boot. I have tried using systemd, init.d, rc.local, and cron to execute my bash script in order to invoke a program (e.g. open an instance of LXTerminal), and still can’t see it on boot.

systemd: I created a service in /etc/systemd/system/, following this template:

[Unit]
Description=My service
After=network.target

[Service]
ExecStart=/usr/bin/python3 -u main.py
WorkingDirectory=/home/pi/myscript
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

I then created my bash script in /etc/systemd/system and made it executable, before following the instructions in the official Raspberry Pi documentation, running the following commands:

sudo cp myscript.service /etc/systemd/system/myscript.service
sudo systemctl start myscript.service
sudo systemctl stop myscript.service
sudo systemctl enable myscript.service

I was informed that a symlink was indeed created, but the program did not run after starting the service.

init.d: Following this example, I added one init script under /etc/initd/, made the script executable, and registered the script to be run at startup, with no result.

rc.local: Following the official documenation found here, I added my bash script to the the rc.local file in /etc/, with no result.

cron: Following the official documentation found here, I ran crontab with the -e flag to edit the cron table:

crontab -e

Then, I added a scheduled task to crontab:

@reboot bash /home/pi/mybashscript.sh &

Unfortunately, there was still no result. I am hoping to use any method to run a program automatically on startup on Raspbian Buster. Please respond if you have had any experience with accomplishing this, or have ran into the same problems. Thanks for your help!

Answers 1

  • execute my bash script in order to invoke a program (e.g. open an instance of LXTerminal

    If your auto-started program needs anything graphical, then you're trying the wrong thing.
    Try putting it in the lxsession-autostart:

    sudo mousepad /etc/xdg/lxsession/LXDE-pi/autostart
    

    Add your command to the bottom of the file, then save and exit.


Related Questions