There are some things I want my little home server to do permanently in the background. The corresponding programs may or should run in the context of the regular, non-privileged user. They may be interactive / full-screen console applications that I want to access from different computers around my network.
A way to accomplish this is to set up screen with an autostart configuration. The screen command is part of all modern Linux distributions, although it may not be installed by default.
To start the jobs at boot time without manual intervention, screen must be hooked up with the boot process. For gentoo this happens through an executable file /etc/local.d/*.start. Fedora uses /etc/rc.d/rc.local, which must be created in contemporary releases of the distribution. In both cases, the files must be set executable.
The call to screen from this system boot hook needs to be prefixed with an appropriate sudo to run in the context of the non-privileged user:
sudo -u <user> /usr/bin/screen -wipe
sudo -u <user> -i /usr/bin/screen -dm
From the screen manpage:
-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.When the system was not shut down properly, screen session files may have been left behind which can prevent screen from autostarting on the next boot. Since this is in bootup and no other screen sessions are yet running, we can just use the -wipe option preventively to make sure no such leftovers remain.
There is an additional catch with Fedora as it sets Defaults requiretty in /etc/sudoers which prevents sudo from working here:
The solution is to add another line using visudo as follows:sudo: sorry, you must have a tty to run sudo
/etc/sudoers
Defaults:root !requirettyNow create a directory .screen and a file .screenrc in the user's home directory. The directory holds a number of scripts containing the commands to be run in various screen windows, while the .screenrc file assigns these scripts to windows.
.screenrc
defflow off
screen -t shell0 0 $HOME/.screen/s0
screen -t pinger 1 $HOME/.screen/s1
screen -t nload 2 $HOME/.screen/s2
screen -t shell0 0 $HOME/.screen/s0
screen -t pinger 1 $HOME/.screen/s1
screen -t nload 2 $HOME/.screen/s2
The defflow off directive turns off screen's flow control handling, which allows fullscreen console applications to receive and process the keystrokes Ctrl-S / Ctrl-Q. This can also be set per window (-f / -fn).
The -t option to the screen directive sets the window title; it is followed by the window number and the command to run.
.screen/s0
#!/bin/shfetchmail -d 3600
bash
This starts fetchmail in the background to retrieve mail each hour, and also gives me a shell in screen window 0.
.screen/s1
#!/bin/shpinger
Window 1 runs a tool that regularly pings a number of other computers and presents statistics about their availability.
.screen/s2
#!/bin/shnload -i 100000 -o 100000 -t 4000 -u K -U K eth0
This tool visualizes the utilization of the eth0 interface as an ascii-art graph.
See here for how to connect to such a screen session, and here for a general introduction to screen.