-2

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!

  • What happens when you run your crontab command from the CLI? – Seamus May 31 at 0:09
  • I then created my bash script in /etc/systemd/system why? what does the bash script do? how is it at all related to the service you created? – Jaromanda X May 31 at 0:10
  • This Question is lacking the vital piece of evidence - viz. the script! – Milliways May 31 at 0:11
  • @Seamus, crontab hangs with no result, and the only choice seems to be to use a Control+C keyboard interrupt to exit out. – Riaementi May 31 at 6:58
  • @JaromandaX, the bash script was to run my program with several parameters, but I got it working by adding "lxterminal -e bash /home/pi/myScript.sh" to my lxsession-autostart, as per Botspot's suggestion. My service's only purpose was to automatically start the script on boot. – Riaementi May 31 at 7:02
3

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.

| improve this answer | |
  • This works, thank you! I was able to get it to autostart by adding "lxterminal -e bash /home/pi/myScript.sh" to the lxsession autostart file, in order to have my script run in the LXTerminal terminal emulator. – Riaementi May 31 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.