How can i set up my pi zero, so that if I plug it in, it runs a python program, without having to login etc?

The project goal is a device that plays an audio file on the push of a hardware button. I would use another hardware button to safely shutdown. My question is: How do I configure the pi so that i can plug it back in, it boots up itself and runs the program without having to use ssh or a keyboard and monitor to login. Can i do that using raspbian or is there maybe a 'lighter' OS that is made for use cases like this one?

thanks in advance!

  • 1
    You can automatically run a script on boot up, but it will not do a long time because you unplug the RasPi without a graceful shutdown. This will corrupt the operating system after several interruptions.
    – Ingo
    Jan 5, 2021 at 9:57
  • I would use a hardware button to safely shutdown.
    – Anton
    Jan 5, 2021 at 10:46
  • Please add this information to your question.
    – Ingo
    Jan 5, 2021 at 10:57

2 Answers 2


check this link

this was helpful for me I could autostart qt application on a raspberry pi os lite so I think you can run your python program

you just need to create a .service file in the /etc/systemd/system

cd /etc/systemd/system
sudo nano application_one.service

Description=Qt application autostart



edit this file then enable this service

sudo systemctl enable application_one.service

reboot and you will have an autostart of your application

for that you have to remove the login prompt

sudo systemctl disable getty@tty1
sudo systemctl mask plymouth-start.service

My question is: How do I configure the pi so that i can plug it back in, it boots up itself and runs the program without having to use ssh or a keyboard and monitor to login.

You don't have to do anything to the RPi to have it start (boot) when power is connected - this is the way it works!

You can use cron to start a Python program upon startup - this is very simple. Use the terminal (via SSH if necessary) and Command Line Interface (CLI) as follows:

1. Take care of two (2) prerequisites

Once you are logged in as default user pi, you are presented with a terminal screen in the bash shell. You will see a prompt similar to the one shown below indicating where to begin your input.

pi@raspberrypi4b:~ $

  • You must know the location of your program - full path specification; e.g. /home/pi/MyPythonProgram.py

  • Your program must be marked as executable; you can ensure the program is executable with this command:

    pi@raspberrypi4b:~ $ chmod a+x /home/pi/MyPythonProgram.py

2. Open your crontabfor editing:

At the bash prompt (pi@raspberrypi4b:~ $ ), open your crontab for editing (choose nano as your default editor if asked):

$ crontab -e

Your default crontab will open in the nano editor.

3. "Schedule" your program to start each time the RPi boots:

Move the "insertion point" in nano just below any existing text in crontab, and type the following:

@reboot (sleep 20; /home/pi/MyPythonProgram.py) >> /home/pi/logmyprogram.txt 2>&1

Here's what this does:

  • @reboot simply instructs cron to execute the following commands each time the system boots.
  • sleep 20 waits 20 seconds after cron is started before it starts your program
  • your program (assumed to be /home/pi/MyPythonProgram.py) is started
  • any output or errors (stdout or stderr, 2>&1) from your program are re-directed (>>) to a file: /home/pi/logmyprogram.txt

sleep and the redirect are often useful in running cron jobs. sleep provides additional time for all system resources to get started (e.g. networking services). redirect (>>) is useful as your interactive shell won't be available to receive any output or error messages - instead, they will be written to the logfile you choose.

4. Test

Reboot your system (sudo reboot at the shell prompt). Review the logfile for any mesages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.