Terminal on a Raspberry Pi showing the MeinDienst.service unit file and systemctl status: service enabled and running

Raspberry Pi – Setting up Autostart for Scripts and Programs (Update 2026)

A script that runs as soon as you power up, a kiosk browser that opens full screen, a bot that carries on after a power cut: there are several ways to set up a Raspberry Pi autostart. Which one fits depends on whether your program needs a screen, whether it has to wait for the network and whether it should restart after a crash.

This guide covers every method for the current Raspberry Pi OS, whether you run a Raspberry Pi 4 or a Raspberry Pi 5*. Last checked in October 2026 on Raspberry Pi OS (64-bit). We reproduced rc.local, PM2 and Python’s output buffering hands-on; everything else is checked against the Raspberry Pi OS packages, source code and documentation.

Links marked with * are affiliate links, which makes them advertising. If you buy through one, I earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Learn more

In short

The most reliable way to start scripts and services is a systemd service; programs with a window start from the desktop’s autostart.

  • No screen needed: use a systemd service. If your script needs the network, have it wait for network-online.target.
  • Needs a window (kiosk browser, dashboard): use the labwc autostart file or a .desktop file, plus desktop auto-login.
  • Docker containers start through their restart policy; Node.js and Python apps can also use PM2.
  • rc.local still works but is no longer a good choice for new projects: newer systemd versions have dropped it.

Which autostart method should you use?

MethodStartsBest for
systemd service ⭐At boot, optionally once the network is upScripts and services without a screen, with automatic restarts
labwc autostart fileWith the desktop (Wayland)Kiosk browsers, dashboards, programs with a window
.desktop fileAfter desktop loginPrograms with a window, on Wayland and X11
Cron @rebootWhen the cron service startsSimple commands that run once
.bashrcWith every interactive shell (SSH, console, terminal window)Environment variables and login messages – not a boot autostart
PM2Through its own systemd service at bootNode.js and Python apps with monitoring
Docker restart policyWith the Docker serviceContainers such as Home Assistant or Pi-hole
rc.localAt the end of the boot processExisting setups; dropped in newer systemd versions

As a rule of thumb, anything that doesn’t need a screen belongs in a systemd service, and programs with a window start from the desktop’s autostart.

Usernames in the examples

In the examples, youruser stands for your username and /home/youruser for your home directory. Replace both with your own – run whoami if you’re not sure. Raspberry Pi OS hasn’t created a default pi user since 2022.

If you start your own script, its first line needs the right interpreter, such as #!/bin/bash, and the file must be executable: chmod +x my_script.sh. Our guide to file permissions with chmod and chown explains the details.

Autostart with systemd (recommended)

systemd starts every service on Raspberry Pi OS, and your own services work the same way. That gives you three advantages over the other methods: you can say what the service should wait for, such as the network; systemd restarts it after a crash; and all output ends up in the system log, the journal.

Create a unit file for your service in /etc/systemd/system/:

sudo nano /etc/systemd/system/MyService.service

For a Python script, it looks like this:

[Unit]
Description=My autostart service
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=youruser
WorkingDirectory=/home/youruser
ExecStart=/usr/bin/python3 -u /home/youruser/my_script.py
Restart=on-failure
RestartSec=10s

[Install]
WantedBy=multi-user.target

What each line does:

  • Wants= and After=network-online.target – the service waits until the network is configured. Wants= makes systemd wait for it at all, After= sets the order, so you need both lines. systemd doesn’t check whether the internet is reachable. If your script doesn’t need the network, leave both lines out.
  • Many older guides only use After=network.target. That isn’t enough: according to the systemd documentation, this target only means the network management stack has started. Whether a connection is up yet is undefined.
  • User= – the user the service runs as. Enter your own username. If the user doesn’t exist, the service won’t start and the journal shows status=217/USER.
  • WorkingDirectory= – the directory the script runs in. Relative paths in the script are resolved from here.
  • ExecStart= – the command, with absolute paths. The -u turns off Python’s output buffering. Without it, Python holds back its output whenever it isn’t writing to a terminal, and the journal stays empty for a long time – in our test, not a single line after five seconds.
  • Type=simple – the program keeps running in the foreground for as long as the service is active, which suits almost any script of your own.
  • For a shell script, the line is ExecStart=/home/youruser/my_script.sh – the script then needs a #!/bin/bash line and execute permission.
  • Restart=on-failure – restarts the service if it exits with an error, here after a ten-second pause (RestartSec=10s).
  • WantedBy=multi-user.target – the service starts on every normal boot, with or without a desktop.

Save with Ctrl+X, Y and Enter. Then reload the unit files, enable the service and start it right away:

sudo systemctl daemon-reload
sudo systemctl enable --now MyService.service

enable adds the service to the boot process, --now also starts it immediately. Whenever you change the unit file later, run daemon-reload again and restart the service – otherwise the old version keeps running:

sudo systemctl daemon-reload
sudo systemctl restart MyService.service

Check the status and output like this:

systemctl status MyService.service
journalctl -u MyService.service -n 50

The real test is a reboot with sudo reboot. Check the status afterwards: if it says active (running), your script is running.

To turn the autostart off again, stop and disable the service:

sudo systemctl disable --now MyService.service

You’ll find ready-made systemd service templates (Python script, network service) in the raspberry.tips GitHub repository.

Starting programs with a window on the desktop

A systemd service starts before there’s a desktop, so a program with a window has no screen to draw on. Start programs like that from the desktop’s autostart instead. There are two ways to do it: the labwc autostart file is the simplest, one command per line. Use .desktop files if your autostart also has to work under X11, or if you want each program in a file of its own.

Prerequisite: desktop auto-login

If the Pi runs without a keyboard, the desktop has to open without a login. In sudo raspi-config, first go to 1 System Options → S5 Boot and choose B2 Desktop. Then open 1 System Options → S6 Auto Login. The first question, “Would you like to automatically log in to the console?”, only affects the text console, so your answer makes no difference to the desktop. Unless you also want auto-login on the console, choose No. After a short confirmation, raspi-config asks “Would you like to automatically log in to the desktop?” – answer that with Yes. This second question only appears once booting to the desktop is set up. On the desktop, you’ll find the same setting in the Control Centre under System, “Desktop Auto Login”.

Which desktop are you running?

Since the October 2024 release, the Raspberry Pi OS desktop runs on Wayland with the labwc compositor on all models. Older installations that were never switched over still use Wayfire (Pi 4 and Pi 5) or X11. To find out which desktop you’re running, use this command – it works over SSH too:

pgrep -l 'labwc|wayfire|Xorg'

The output shows labwc, wayfire or Xorg (X11). With labwc, use the labwc autostart file or .desktop files; with X11, .desktop files; with Wayfire, see “Wayfire (older installations)” further down. You can switch between labwc and X11 in sudo raspi-config under 6 Advanced Options → A7 Wayland. Wayfire is no longer offered there.

Autostart on Wayland with the labwc autostart file

labwc has its own autostart: the file ~/.config/labwc/autostart. labwc runs it as a shell script once the desktop has started. Unlike in a systemd service, WAYLAND_DISPLAY and DISPLAY are already set, so graphical programs find their screen. Raspberry Pi OS starts labwc with the --merge-config option, so your file adds to the system-wide one rather than replacing it – the panel and desktop background stay put. If they do go missing, see the matching point under “When autostart doesn’t work”.

On a fresh install, the file doesn’t exist yet:

mkdir -p ~/.config/labwc
nano ~/.config/labwc/autostart

End every command with an & so it runs in the background. Without the ampersand, the autostart script waits for the program to exit, and every line after it is held up until then. The desktop itself still starts normally.

# Start your own script
/home/youruser/my_script.sh &

# Open Chromium as a full-screen kiosk browser
chromium --kiosk --noerrdialogs --disable-infobars --no-first-run https://raspberry.tips/en/ &

The Chromium flags come from the official Raspberry Pi kiosk tutorial, which says Chromium needs at least a Pi 3 with 1 GB of RAM.

labwc runs the file with sh, not bash. Bash features such as [[ … ]] don’t work there, and a #!/bin/bash line at the top is ignored. Put anything more involved into a separate script and just call that.

The @ prefix from old LXDE guides doesn’t work here. If a program should restart automatically, put /usr/bin/lwrespawn in front of it and keep the & at the end – that’s how Raspberry Pi OS starts its own panel and desktop background. Keep in mind that lwrespawn restarts the program whenever it exits, not just after a crash, so it’s no good for a script that runs once and finishes. For a kiosk browser that should come back after it’s closed, it’s just right:

/usr/bin/lwrespawn chromium --kiosk --noerrdialogs --disable-infobars --no-first-run https://raspberry.tips/en/ &

raspi-config gives a real-world example: if you turn on 2 Display Options → D2 Screen Blanking, it adds this line to your autostart file (the backslashes before the asterisks are part of it):

swayidle -w timeout 600 'wlopm --off \*' resume 'wlopm --on \*' &

If this line is in your file, the screen switches off after ten minutes without input. For a kiosk display that should never go dark, turn screen blanking off in raspi-config, which removes the line for you.

There’s also a system-wide file, /etc/xdg/labwc/autostart. Raspberry Pi OS uses it to start the panel, the desktop background, kanshi (which manages your monitor layout) and lxsession-xdg-autostart, which runs the .desktop files from the next section. The file belongs to the system and can be replaced by updates, so leave it alone and put your commands in the file in your home directory.

Changes to the autostart file take effect after you log out and back in, or after a reboot. labwc --reconfigure reloads settings such as rc.xml and environment, but it doesn’t run the autostart again. To check whether your program is running after a reboot, use pgrep -a with its name, for example pgrep -a chromium. To turn it off, delete the line or put a # in front of it.

Desktop autostart with .desktop files

.desktop files in your user’s autostart directory work under both labwc and X11. In both cases, lxsession-xdg-autostart launches them. Create the directory if it doesn’t exist yet, then a new file:

mkdir -p ~/.config/autostart
nano ~/.config/autostart/MyAutostart.desktop

A .desktop file looks like this:

[Desktop Entry]
Type=Application
Name=My autostart script
Comment=Starts after desktop login
Exec=/home/youruser/my_script.sh

The Exec= line doesn’t go through a shell, so ~, variables, redirects with >> and chains with && won’t work. Put commands like that in a script and enter its full path. No terminal window opens at autostart either, not even with Terminal=true: according to its source code, lxsession-xdg-autostart ignores that key. If you want to see the output, run the script in a terminal: Exec=lxterminal -e /home/youruser/my_script.sh.

The autostart only applies to the user whose home directory holds the file. To start something for every user, put the file in /etc/xdg/autostart/ with sudo. To switch an entry off, delete the file or add the line Hidden=true – lxsession-xdg-autostart then skips it.

Wayfire (older installations)

If you’re still on Wayfire, autostart commands go in the [autostart] section of ~/.config/wayfire.ini, each as name = command. Raspberry Pi OS stopped shipping Wayfire updates at the end of 2024, so it’s worth switching to labwc in raspi-config.

Cron job with @reboot

Cron has a special entry for commands that should run once after every boot. For a job under your own user, open your crontab:

crontab -e

The first time, crontab asks which editor to use – nano is the easiest. Add this line at the end:

@reboot /home/youruser/my_script.sh

System-wide jobs go in /etc/crontab, which you open with sudo nano /etc/crontab. There, @reboot is followed by the user the command should run as:

@reboot youruser /home/youruser/my_script.sh

@reboot runs as soon as the cron service starts and doesn’t wait for the network. That makes cron a good fit for simple tasks. If your script needs a network connection or should restart after a crash, use systemd. Our guide to setting up cron jobs on the Raspberry Pi covers cron in more depth, including scheduled jobs. To remove the job, delete the line again with crontab -e. The easiest way to see whether it ran is to redirect its output to a file (see “When autostart doesn’t work”).

.bashrc: commands at login

~/.bashrc isn’t a boot autostart. It runs with every interactive shell: when you log in over SSH or at the console, but also every time you open a terminal window on the desktop. Without a login it never runs; with three terminal windows open, it has run three times.

That makes it the right place for things that belong to your session: environment variables, aliases or a status line when you log in. Add them at the end of the file:

nano ~/.bashrc
echo "Welcome to $(hostname), logged in as $USER"

Services or scripts that should keep running don’t belong here – they’d start again with every terminal window. To show a detailed system overview when you log in over SSH, see Raspberry Pi SSH login: show system info with a custom MOTD.

PM2 and Docker

Two common cases come with their own tools that handle the autostart for you: PM2 for Node.js and Python apps, and the restart policy for Docker containers.

PM2 (process manager for Node.js and Python)

PM2 starts your scripts, keeps an eye on them and restarts them after a crash. It’s worth it if you run several Node.js or Python apps and want their logs and restarts in one place; for a single script, a systemd service is enough. pm2 logs shows their output, pm2 monit their CPU and memory use in the terminal. PM2 runs on Node.js, which isn’t preinstalled on Raspberry Pi OS. Install both and start your script:

sudo apt install nodejs npm -y
sudo npm install -g pm2

pm2 start /home/youruser/my_script.py --name MyBot

For the autostart, PM2 needs a systemd service. pm2 startup doesn’t set it up itself, though – it only prints a command:

pm2 startup
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u youruser --hp /home/youruser

Copy the line that starts with sudo env PATH= and run it. Only that creates the pm2-youruser service; skip it and nothing starts after a reboot. Then save the list of running processes that PM2 restores at boot:

pm2 save

If you start more processes with PM2 later, run pm2 save again.

Docker restart policy

If your service runs in a Docker container, you need neither systemd nor cron. The restart policy in your docker-compose.yml takes care of the autostart:

services:
  my_service:
    image: my_image:latest
    restart: unless-stopped

Apply the change with docker compose up -d in the file’s folder; docker ps shows whether the container is back after a reboot. For a single container, pass the policy as docker run -d --restart unless-stopped …. According to the Docker documentation, unless-stopped brings the container back after the Pi reboots, unless you stopped it yourself beforehand. always brings it back even if you stopped it before the reboot. Our guide on how to install Docker on the Raspberry Pi walks you through the setup.

Autostart with rc.local (legacy)

/etc/rc.local is the oldest method: the commands in it run with root privileges at the end of the boot process. It still works on the current Raspberry Pi OS. But according to its release notes, systemd dropped rc.local support in version 260, and Debian’s testing release has already followed suit. For new projects, use a systemd service instead. An existing rc.local keeps working until you move to a future Raspberry Pi OS release with a newer systemd – systemctl --version tells you which one you have; below 260, rc.local works. Move it to a service before then.

Images from late 2024 onwards don’t include the file; older installations still have it. Check whether it’s there and create it if not:

sudo nano /etc/rc.local

Your commands go before the exit 0 line. Anything that keeps running needs an & at the end:

#!/bin/sh -e
#
# rc.local – runs with root privileges at the end of the boot process

/usr/bin/python3 /home/youruser/my_script.py &   # keeps running, hence the &
/usr/local/bin/run_once.sh                   # finishes quickly

exit 0
  • Without &, rc.local blocks. If a command never finishes, the lines after it never run. According to the unit files, the console login also waits for rc.local and never shows up. The desktop and SSH aren’t affected.
  • The -e in the first line stops the script at the first error. If one line fails, none of the following lines run.

For systemd to run the file, it has to be executable. Then start it once by hand and check its status:

sudo chmod +x /etc/rc.local
sudo systemctl start rc-local.service
systemctl status rc-local.service

From now on, systemd picks the file up on every boot by itself. You don’t need the systemctl enable rc-local.service found in older guides: systemd just reports that the unit has no installation config and changes nothing. If the file isn’t executable, it’s silently skipped at boot. We reproduced both with a reboot.

Init scripts in /etc/init.d

SysV init scripts in /etc/init.d/ with update-rc.d are in the same boat as rc.local: on the current Raspberry Pi OS, systemd still turns them into services, but from systemd 260 onwards it no longer does. Move scripts like these to a unit file as shown above.

When autostart doesn’t work

The most common reasons a script doesn’t start at boot:

  • Relative paths – at boot there’s no working directory you can rely on. Always use absolute paths for scripts and files (/home/youruser/…) and set WorkingDirectory= in systemd.
  • Missing execute permission – you forgot chmod +x, or the #!/bin/bash line is missing. For rc.local, this applies to the file itself.
  • Network not ready yet – After=network.target on its own doesn’t wait for a connection. Use After=network-online.target together with Wants=network-online.target.
  • Wrong user – if User= names a user that doesn’t exist (such as pi), systemctl status reports status=217/USER.
  • Graphical program in a systemd service – there’s no desktop yet at boot, so the service has no WAYLAND_DISPLAY or DISPLAY. Anything with a window belongs in the labwc autostart file or a .desktop file.
  • Panel and background are gone since you added your own labwc autostart file – the desktop is then running the plain “labwc” session instead of the Raspberry Pi OS one, for example because it was once picked on the login screen. Select labwc again in raspi-config under 6 Advanced Options → A7 Wayland, which makes the Raspberry Pi OS session (rpd-labwc) the default again.
  • No output – with systemd, it’s in the journal (journalctl -u MyService.service); start Python scripts there with -u. With cron and in the labwc autostart file, redirect it to a file yourself: /home/youruser/my_script.sh >> /home/youruser/script.log 2>&1 &. This doesn’t work in the Exec= line of a .desktop file – put the redirect inside the script instead.

You’ll also find the systemctl and journalctl commands in our Raspberry Pi commands cheat sheet.

Need hardware for your projects? On Amazon you’ll find SD cards*, the official 27 W power supply for the Raspberry Pi 5* and the official Raspberry Pi 4 power supply*.

Frequently asked questions

What is the difference between systemd and rc.local?

A systemd service can wait for the network, restarts after a crash and logs its output to the journal. rc.local just runs commands once, with root privileges, at the end of the boot process. It still works on the current Raspberry Pi OS as long as the file is executable, but systemd dropped support for it in version 260.

How do I run a Python script automatically at boot?

Create a service file at /etc/systemd/system/MyService.service with ExecStart=/usr/bin/python3 -u /home/youruser/my_script.py and your username in User=. Enable and start it with sudo systemctl daemon-reload and sudo systemctl enable --now MyService.service.

How do I delay the autostart until the network is available?

Add Wants=network-online.target and After=network-online.target to the [Unit] section of your service file. The service then waits until the network is configured. After=network.target on its own isn’t enough.

How can I tell whether my autostart service is running?

systemctl status MyService.service shows the current status, and journalctl -u MyService.service shows the service’s output.

Does @reboot in crontab still work on Raspberry Pi OS?

Yes. @reboot runs as soon as the cron service starts and works well for simple commands. It doesn’t wait for the network and won’t restart anything after a crash – systemd is the better choice for that.

How do I set up autostart on Wayland with labwc?

Add the command to ~/.config/labwc/autostart and end it with an ampersand so it runs in the background (create the folder with mkdir -p ~/.config/labwc if needed). Raspberry Pi OS runs this file in addition to the system-wide one, so the panel and background stay in place. .desktop files in ~/.config/autostart work under labwc as well.

Why doesn’t my PM2 process start after a reboot?

pm2 startup doesn’t set up the autostart itself – it prints a line starting with sudo env PATH=. Run that line, then save your running processes with pm2 save.

Similar Posts