Brainy Pi

Available to select audience (currently beta)

Are you a programmer looking to ship your Python script as a product on Brainy PiRaspberry pi alternative ? If yes, you might have realized that adding your script to the boot process is essential to ensure it runs automatically after the device starts up. However, the process of configuring a script to run on boot can be challenging, especially if you are not familiar with the Brainy Pi’s operating system. That’s where this blog comes in handy. In this article, we will guide you through the step-by-step process of Launch Python Script on Startup on a Brainy Pi, so your script runs seamlessly after booting. So, let’s get started!

Supplies:

  1. Brainy Pi
  2. Keyboard
  3. Mouse
  4. Monitor
  5. A Python Script

The Easier Method!

  1. Start by opening the terminal or an ssh session and then open the rc.local file.
sudo nano /etc/rc.local  
  1. Add the following line to the end of the file before exit 0 line
python /path/to/your/script/yourscript.py &  
  1. Change the /path/to/your/script/yourscript.py to the path to your script and the name of the script.
  2. Exit out of the the file by pressing ctrl+x then y then press enter to save.
  3. Now, is the time to restart the system and see if it worked.
  4. Check if the script runs correctly when executed manually: Before setting the script to run automatically at startup, make sure that it runs correctly when executed manually from the command line. This can help you identify any syntax errors or other issues that may be preventing the script from running correctly.

The Complex Method

Another method to execute your Python script at system startup is by creating a systemd service. This approach is more complex than the previous method, but it offers greater control and flexibility over the startup process.
  1. Start by creating a new service file in the /etc/systemd/system directory
sudo nano /etc/systemd/system/file.service  
  • Change file.service to something best describing your python script.
  1. Add the following content to the file
Copy code[Unit] Description=My Python Script After=multi-user.target [Service] Type=simple ExecStart=/usr/bin/python3 /path/to/file.py Restart=on-failure [Install] WantedBy=multi-user.target  
  1. Replace /path/to/file/file.py with the path to your python script.
  2. Save and exit out of the file by pressing ctrl+x then y then Enter.
  3. Reload the systemd daemon by running the command
sudo systemctl daemon-reload  
  1. Start the service by running the following command
sudo systemctl start file  
  1. Replace the file with the name you gave to the file earlier.
  2. Enable run on boot by the following command
sudo systemctl enable file  
  1. Replace the file with the name you gave to the file earlier.
The python file should run at startup now. Make sure that the python code is bug free as bugs could result in failed system startups.

Endnotes

So, we have seen how to Launch Python Script on Startup on a Brainy Pi using simple as well as a complex way, Stay tuned for more such blogs !

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*