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 generate an SSH key and add to authorized_keys

Updated 16th August 2022

In this article we explain how to generate a SSH key for your local machine and add your public key to the authorized_keys on a remote server. Generate an SSH key for Windows, MAC and Linux Note: If you are using Windows, we recommend you use a SSH client like PuTTY. For MAC/Linux run: ssh-keygen

The ULTIMATE guide to setup Windows Subsystem for Linux (WSL) with Ubuntu, Apache2, MySQL and PHP

Updated 16th August 2022

Windows is now good for development! Yes! All thanks to Windows Subsystem for Linux (WSL). No more WAMP or Xampp, WSL can do everything you need. In this ultimate guide to setup Windows Subsystem for Linux with Ubuntu, Apache2, MySQL, and PHP we will cover: What is Windows Subsystem for Linux? Setting up Windows Subsystem

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