Last Updated: March 03, 2016
· jessethegame

Run a script on startup in a detached screen on a Raspberry Pi

This document explains how to run a script every time you startup your Raspberry Pi and how to get access to the screen running it. This applies to any UNIX system.

The script can contain anything. For this example, we'll use a script /home/pi/startup. It could look something like this


# Start my node project
cd ~/node/myproject && npm start

Make sure it's executable

chmod +x ~/startup

Configure the startup script

Install screen

sudo apt-get install screen

Edit the root startup script file

sudo nano /etc/rc.local

And the following line before exit 0

# Run a command as `pi` from the home folder 
# in a screen named `pistartup`
su - pi -c "screen -dm -S pistartup ~/startup"

We make use of two very convenient flags. As the man page explains:

-d -m Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.

Reboot for effect

sudo reboot

Yay, it runs the script on startup! What now?

The true niceness happens when you ssh into your Raspberry Pi

ssh pi@raspberry.local

and get to look at the script running!

screen -DR

Google for instructions on how to use screen, or check this quick reference I found when I did myself.

BONUS: How do we make this even more tasty?

Screen allows us to open multiple tabs. Say I were to have two long running socket server scripts that I wanted to run separately. I can open 2 tabs, give them a name and start the processes like so:

# /home/pi/startup
# screen -t <title> <tab-index> <command>

screen -t socket-local  0 forever /home/pi/socket-local/server.js
screen -t socket-remote 1 forever /home/pi/socket-remote/server.js

4 Responses
Add your response

This is super useful. This is how I run minecraft server.

over 1 year ago ·

This is a nice setup, however it will not run for me because the su command is asking for a password. Any way around this?

over 1 year ago ·

i have tried this but will not run my script on boot or when running the rc.local from prompt.
It shows the ip as in it but not my screen command it looks like this
execute it set
root@raspberrypi:~# ls -lasi /etc/rc.local
523710 4 -rwxr-xr-x 1 root root 538 Jun 21 08:04 /etc/rc.local

rc.local looks like this

!/bin/sh -e


This script is executed at the end of each multiuser runlevel.

Make sure that the script will "exit 0" on success or any other

value on error.

In order to enable or disable this script just change the execution


By default this script does nothing.

Print the IP address

IP=$(hostname -I) || true
if [ "$
IP" ]; then
printf "My IP address is %s\n" "$_IP"

screen -d -m -S cgminer /home/pi/chminer/cgminer -c /home/pi/cgminer.conf

exit 0

what do i have wrong the screen works by hand but need it to start on boot


over 1 year ago ·

This did not work for me because su asked for password. However, I was able to solve that by changing the command to

sudo su - pi -c "screen -dm -S pistartup ~/startup"

over 1 year ago ·