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
#!/usr/bin/bash # Start my node project cd ~/node/myproject && npm start
Make sure it's executable
chmod +x ~/startup
Configure the startup script
sudo apt-get install screen
Edit the root startup script file
sudo nano /etc/rc.local
And the following line before
# 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
Yay, it runs the script on startup! What now?
The true niceness happens when you ssh into your Raspberry Pi
and get to look at the script running!
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