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 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

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

How to create Alias command in Linux

Updated 16th August 2022

Alias commands in Linux are a great way to speed up your workflow. In this post, I will show you how to create Alias command in Linux using Bash on WSL (Windows Subsystem for Linux). What is an Alias command? The alias command in Linux allows you to create custom shortcuts for frequently used commands.