A Raspberry Pi used as part of a home monitoring system will need to be robust to reboots. If the power flickers or if the Pi needs to be rebooted for some other reason, the monitoring software ought to restart without human intervention. This might involve starting multiple programs. Additionally, especially when developing new code, one might want to log in after boot and see the messages being printed by the software.
This can be accomplished with a combination of the Pi's bootup files and screen. The basic steps are:
- Configure the Pi to run a script when booting
- Make that script start multiple programs using screen and detach
Configuring Pi to Run a Script at Boot Time
First, we specify a file that should be executed upon startup. This can be accomplished by changing the Pi's /etc/rc.local script. You'll need to do this as root. I add the following to end of the /etc/rc.local script (but before the "exit 0" line if it exists. This will cause the script to sleep for four seconds, which I found to be necessary, I believe to wait for network resources to be available. Then it switches to the pi account (you can use another account name as well) and runs the script called "startup" in pi's home directory.
sleep 4
su - pi -c ~/startup
Starting multiple programs using screen and detaching
The next step is to create a script called startup in the home directory of the appropriate account (in this case, "pi"). We need to be able to start a sequence of commands detached from the console so the script can complete, but in a manner that lets you get to them later. The screen command is perfect for this. A full description of screen is beyond this post but is well documented all over the internet.The only general screen advice I'll give is that I highly recommend creating a .screenrc file that gives you a status line so you can see what's running. I use the following, which I've pieced together from different web sites. Among other things, it puts a useful status line at the bottom telling me about what windows I have open. It also changes the escape character from "a" to "o" which is almost necessary if you use emacs within screen and other applications that use ^a to move to the beginning of a line.
shell bash
startup_message off
defscrollback = 5000
shelltitle '$ |bash'
msgwait 1
nethack on
backtick 0 0 0 whoami
escape ^Oo
hardstatus alwayslastline "%{=b}%{G}Screen @ %0`@%H: %{C}%w %=%{G}%D, %m/%d"
The contents of the startup file should create a new screen session and then populate that session with windows, each of which is running a different program. The following simple example creates a session called "mytest" which will contain a bash shell. The next two lines start two different instances of top, one of which is in a window called "first" and the other is in a window called "second." You can follow this pattern, adding more lines for more commands. The bash command to run for a window is put in the double quotes.
screen -dm -S mytest
screen -S mytest -X screen -t first bash -c "top"
screen -S mytest -X screen -t second bash -c "top"
With this configuration in place, you can reboot your pi. After the boot is complete, you can run "screen -r" to resume the session that was created during boot. The code above will create a screen that looks like this:
Comments
Post a Comment