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. 

What is a Cron Job and how to write to the Crontab

Updated 10th December 2023

The need to schedule tasks on a machine is common for any Software Developer, System Admin or IT professional, so it’s important to learn what a cron job is and how to write to the crontab. What is CRON? Cron is a daemon built-in to most Unix-like operating systems such as: Cron reads the crontab (cron tables)

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