Florian Wesch

Running info-beamer in production

Posted Jul 07 2015 by Florian Wesch

<p>You have developed your visualization and are not sure how to run it in production? This post highlights a few recommendations.</p><p>While developing a visualization you usually run info-beamer manually from the command line. This obviously doesn't work when you run your code in production.</p><p>info-beamer doesn't enforce any particular way to run it. So there are a lot of options available. I've learned a few things while developing <a href="/product/info-beamer-hosted">info-beamer hosted</a> which might help you as well if you decide to use Raspbian (or any other Raspberry PI distribution) to run your visualization.</p><p>You can use this tutorial to autostart other programs reliably on the PI as well.</p><p>If you don't want to deal with all the complexity, you might also take a look at <a href="/product/info-beamer-hosted">info-beamer hosted</a>, which solved this for you.</p><h2 class="doc-header" id="processcontrolmonitoring">Process control/monitoring</h2> <p>When running your code in production you have to make sure that it runs reliable. This means that it is started after booting up and stays running. Any kind of process monitoring helps you with that. There are <a href="http://supervisord.org/" rel="external,noopener">multiple</a> <a href="http://www.freedesktop.org/wiki/Software/systemd/" rel="external,noopener">options</a> available. While those work, they are also a bit difficult set up. For that reason I personally prefer <a href="http://cr.yp.to/daemontools.html" rel="external,noopener">daemontools</a>, which is pretty simple once you understand how it's supposed to be used. It is also available out of the box for <a href="https://www.raspbian.org/" rel="external,noopener">Raspbian</a> which makes things even easier.</p><h3 class="doc-header" id="installingdaemontools">Installing daemontools</h3> <p>Installation is pretty simple. Just run</p><pre class='hl curl'>root@raspberry:~# apt-get install daemontools daemontools-run</pre><p>This will install the daemontools programs as well as make sure that everything will start automatically after booting. If you have a look at the processes running, you'll see this:</p><pre class='hl curl'>root@raspberry:~# ps fax PID TTY STAT TIME COMMAND [...] 2099 ? Ss 0:00 /bin/sh /usr/bin/svscanboot 2107 ? S 0:00 \_ svscan /etc/service 2108 ? S 0:00 \_ readproctitle service errors: ..................... [...] root@raspberry:~#</pre><p><tt>svscanboot</tt> is automatically started by <tt>init</tt> (have a look at <tt>/etc/inittab</tt> to see where that happens). <tt>svscanboot</tt> is a tiny program that just starts <tt>svscan</tt> and <tt>readproctitle</tt>. The first one monitors the directory <tt>/etc/service</tt> for programs to start (more on that in a minute). <tt>readproctitle</tt> is a very small program that logs errors by changing its process title. While this sounds weird it is also pretty useful, since you don't have to configure any logfiles when starting to use daemontools. To read the last few log lines, just run</p><pre class='hl curl'>root@raspberry:~# cat /proc/`pidof readproctitle`/cmdline</pre><p>You don't use this kind of logging a lot. It's almost only useful when you mess up while setting up daemontools. Programs you start won't use this logging mechanism, so don't worry.</p><h3 class="doc-header" id="settingupaservice">Setting up a service</h3> <p>It's time to setup a service. A service is a program that should run after the system started and should be running no matter what. Using daemontools for each service that you want to start requires you to create a directory that tells daemontools how to start your program. Let's see an example. First we'll create a new directory <tt>/service</tt> and a subdirectory <tt>info-beamer</tt> inside that directory:</p><pre class='hl curl'>root@raspberry:~# mkdir -p /service/info-beamer</pre><p>Inside this directory you need a new executable called <tt>run</tt>. We'll use a <a href="https://en.wikipedia.org/wiki/Shell_script" rel="external,noopener">shell script</a> here. So go ahead and create <tt>/service/info-beamer/run</tt> with the following content:</p><pre class='hl lua'>#!/bin/sh exec /path/to/info-beamer /path/to/your/node 2&gt;&amp;1</pre><p>Then make sure it is actually executable by setting the correct permissions:</p><pre class='hl curl'>root@raspberry:~# chmod 755 /service/info-beamer/run</pre><p>You can now run the <tt>run</tt> script manually to see if everything works as expected:</p><pre class='hl curl'>root@raspberry:~# cd /service/info-beamer root@raspberrypi:/service/info-beamer# ./run [ info-beamer output... ]</pre><p>While this is already a usable daemontools directory it doesn't do any logging right now. Let's set up logging using the system <a href="https://en.wikipedia.org/wiki/Syslog" rel="external,noopener">syslog</a>. Create a new subdirectory <tt>/service/info-beamer/log</tt>:</p><pre class='hl curl'>root@raspberry:~# mkdir -p /service/info-beamer/log</pre><p>The create the shell script <tt>/service/info-beamer/log/run</tt> with the following content:</p><pre class='hl lua'>#!/bin/sh export SERVICE=$(basename $(dirname $PWD)) exec logger -t $SERVICE</pre><p>Then make sure it is also executable by setting the correct permissions:</p><pre class='hl curl'>root@raspberry:~# chmod 755 /service/info-beamer/log/run</pre><p>When daemontools now runs your program, it will start the previous <tt>run</tt> and this <tt>log/run</tt> script and feed the output of the former to the input of the latter.</p><p>You can now test and check that logging works. Just do this. Once you started <tt>run</tt>, you can type text that will end up in the syslog:</p><pre class='hl curl'>root@raspberry:~# cd /service/info-beamer/log root@raspberrypi:/service/info-beamer/log# ./run hello logfile this is another log line ^D root@raspberrypi:/service/info-beamer/log#</pre><p>You can then check the log file:</p><pre class='hl curl'>root@raspberrypi:~# tail /var/log/syslog [... more log lines ...] Jul 1 16:07:32 raspberrypi info-beamer: hello logfile Jul 1 16:07:38 raspberrypi info-beamer: this is another log line</pre><h3 class="doc-header" id="startingaservice">Starting a service</h3> <p>It is highly recommended to run those final steps while being connected to your PI using SSH. Otherwise info-beamer will overlay its output over the console/X screen and you won't be able to interact with the system. So go ahead now and connect to your PI using SSH.</p><p>Now it's time to hand over the newly created directory to daemontools so it can take care of starting the program as well at its logger. To do that, just create a new symlink in <tt>/etc/service</tt> that points to your service directory:</p><pre class='hl curl'>root@raspberry:~# cd /etc/service root@raspberry:/etc/service# ln -s /service/info-beamer .</pre><p>That's all. <tt>svscan</tt> will detect that there is a new symlinked directory in <tt>/etc/service</tt> within 5 seconds and start the <tt>run</tt> and <tt>log/run</tt> scripts inside that directory.</p><p>In the example above it will start info-beamer.</p><p>If that doesn't work, have a look at the system log:</p><pre class='hl curl'>root@raspberry:~# tail -f /var/log/syslog</pre><p>Your should be able to see your problem there. If that doesn't help, have a look at the <tt>readproctitle</tt> log:</p><pre class='hl curl'>root@raspberry:~# cat /proc/`pidof readproctitle`/cmdline</pre><h3 class="doc-header" id="managingservices">Managing services</h3> <p>You can stop and start services using daemontools. To stop a service, just run <tt>svc</tt> like this:</p><pre class='hl curl'>root@raspberry:~# svc -d /service/info-beamer</pre><p>This will send a SIGTERM signal to the info-beamer process while will stop info-beamer.</p><p>To restart info-beamer again, do</p><pre class='hl curl'>root@raspberry:~# svc -u /service/info-beamer</pre><p>For more options on <tt>svc</tt>, have a look at <a href="http://cr.yp.to/daemontools/svc.html" rel="external,noopener">its documentation</a>.</p><h3 class="doc-header" id="addingmoreservices">Adding more services</h3> <p>You can repeat the above steps if you have more long-running programs that you want to autostart by default. Just add another directory in <tt>/service</tt>, add a <tt>run</tt> and <tt>log/run</tt> script and finally symlink that directory into <tt>/etc/service</tt>.</p><h2 class="doc-header" id="abetterrunscriptforinfobeamer">A better run script for info-beamer</h2> <p>The above <tt>run</tt> script wasn't too complicated. It just started info-beamer and provided a directory. When you run info-beamer in production you might want to use a few more options:</p><pre class='hl lua'>#!/bin/sh export INFOBEAMER_LOG_LEVEL=3 modprobe bcm2708_wdog export INFOBEAMER_WATCHDOG=15 exec nice -n -5 ionice -c 1 -n 0 /path/to/info-beamer /path/to/your/node 2&gt;&amp;1</pre><p>The first export statement makes info-beamer a bit more verbose, so you can debug problems easier. Log level <tt>3</tt> is normally enough for that. If you want to minimize log output, you can set that to the default value of <tt>1</tt>.</p><p>The next two lines load the hardware watchdog kernel module for the PI and tell info-beamer to use this feature. In this mode info-beamer has to provide a constant update to the Linux kernel at least every 15 seconds. If info-beamer fails to provide this update, the PI will reboot. This is useful if your PI hangs badly and info-beamer can't do its job anymore. Hopefully this never happens but if it happens at least you won't have a black screen since the PI will reboot.</p><p>Finally the modified exec line now includes a call to <tt>nice</tt> and <tt>ionice</tt>. <tt>nice</tt> will make sure that the info-beamer process gets a high priority when running. This make it less likely that another program causes info-beamer to stutter. <tt>ionice</tt> is similar but give info-beamer a higher priority for disk access. So loading images or videos won't have to wait for other processes.</p><h2 class="doc-header" id="conclusion">Conclusion</h2> <p>daemontools is pretty awesome once your start using it. It make it easy to have your PI start programs reliably after booting up. You should give it a try.</p><p>If you want to use a reliable Linux distribution where you don't have to do all those steps yourself, you might consider looking at <a href="/product/info-beamer-hosted">info-beamer hosted</a>. Not only is the operating running on the PI build for <a href="building-a-reliable-raspberry-pi-distribution">reliability</a>, but the complete <a href="info-beamer-hosted-insights">infrastructure around it</a> makes it incredibly easy to setup a visualization and run it with 100% uptime.</p><p>Let <a href="/doc/about">me know</a> if you have any questions or improvements.</p>

Read more...


info-beamer.com offers the most advanced digital signage platform for the Raspberry Pi. Fully hosted, programmable and easy to use. Learn more...


Get started for free!

Trying out the best digital signage solution for the Raspberry Pi is totally free: Use one device and 1GB of storage completely free of charge. No credit card required.


Follow @infobeamer on twitter to get notified of new blog posts and other related info-beamer news. It's very low traffic so just give it a try.

You can also subscribe to the RSS Feed RSS feed.


Share this post:

Share using Twitter


Questions or comments?
Get in contact!