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.

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)

List of programming languages used for Web Development

Updated 16th August 2022

There are a wide variety of programming languages that can be used for web development. Which one you choose depends on your preferences and needs. Some languages are better suited for certain tasks than others. In this post, we’ll take a look at some of the most popular languages used for web development. JavaScript PHP

How to Upload and Download files to and from a Linux server over SSH

Updated 16th August 2022

Its common place to need to upload and download files to and from a remote Linux server. To upload and download files from a remote Linux server you can use the scp (secure copy) command. To download a file you can use: To upload a file you can use: scp /path/to/file/your/uploading username@ip-or-server-name:/path/to/location/to/upload/to Note: It is