Run Python Script on boot

By Dillon Smart · · · 0 Comments

Writing your own Shell scripts is a great way to automate your processes. In this post, I will show you how to Run Python Script on boot.

This example will be for Raspberry Pi Computers and will run a python script when the device is booted and the user has authenticated.

The Python Script we will run on boot will make a new file in the user’s documents directory.

Edit the LXDE autostart file

First, change your directory to the LXDE-pi directory using:

cd /etc/xdg/lxsession/LXDE-pi

Next, open the autostart file in a text editor like Vim or Nano.

nano autostart

Add the following configuration to the file to run a python script on boot:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@/usr/bin/python3 /home/pi/Documents/scripts/example.py

Write the file out by using Ctrl+O.

Create the Python Script to run on boot

Now, open a new file in the Documents/scripts directory called example.py

nano /home/pi/Documents/scripts/example.py

Add the following Python code to the new file:

with open('new-file.txt','w') as f:

f.write('Created when the device was booted!')

Then write the file out using Ctrl+O.

Finally, restart your machine.

After the device has booted to the desktop screen, open the Documents/scripts directory to see the new file called new-file.txt.

Learn more about LXDE sessions.

LinuxPythonRaspberry Pi

0 Comment

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How to set up SSH on Raspberry Pi

Updated 16th August 2022

In this post, I am going to show you how to set up SSH on a Raspberry Pi. Step 1: Raspberry Pi config menu First, open a new terminal and execute the command below to open the Raspberry Pi config menu. sudo rasps-config  Select “Interfacing Options” and press ENTER. Then select “SSH” and press ENTER. 

IPtables – How to block and unblock IP Addresses in Linux

Updated 14th December 2023

Not long ago, I noticed some strange activity on a server I manage. The server, running Ubuntu 22.04, was receiving high volumes of traffic from a single IP Address. After some investigation, I decided the activity resembled that of web scrapping, so I decided to block the IP Address. Iptables or ufw I hear you

[UPDATED 2022] Change PHP version on Ubuntu, Linux

Updated 23rd August 2022

In this post, I will show you how to switch PHP version on Ubuntu, Linux. If you are developing on Windows, follow this guide to set up Windows Sub System for Linux. What will be covered: Change PHP version for Apache2 Change PHP version for Nginx Change PHP CLI version In this example, we will