How to Run Your Python Script Automatically at Startup: a Beginner's Guide

by dhruvarora561 in Circuits > Computers

9 Views, 0 Favorites, 0 Comments

How to Run Your Python Script Automatically at Startup: a Beginner's Guide

DALL·E 2023-03-18 10.14.09 - linux penguin with a test tube in his hand pixel art.png

Running a Python script automatically at startup can be very useful, especially if you have a script that you need to run on a regular basis. This can save you time and effort, as you won't have to manually execute the script every time you want to run it. In this blog post, we will explore different methods for making your Python script execute automatically at startup on Linux. Whether you're a beginner or an experienced Python developer, this guide will provide you with all the information you need to get your script up and running at startup.

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

  1. 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.
  2. Start by creating a new service file in the `/etc/systemd/system` directory
sudo nano /etc/systemd/system/file.service
  1. Change `file.service` to something best describing your python script.
  2. 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.
  2. 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.