Using job control and output redirect
We have a default linux without graphics that boots to text console just as
Raspbian Light do. There are several virtual consoles named
tty2 and so on. You can switch to it with
<alt>F2 (tty2) and so on.
I have made a small test script that will output "hello world" on standard output
stdout and "error output" on standard error
stderr every 3 seconds in an endless loop.
pi@rpi3 ~$ cat >test.sh <<EOF
while true; do
echo "hello world" # outputs to normal stdout
echo "error output" >&2 # outputs to stderr
pi@rpi3 ~$ chmod u+x test.sh
Test it with
pi@rpi3 ~$ sudo bash -c './test.sh 1>/dev/tty2 2>/dev/console'
You will see "hello world" on
tty2 (<alt>F2) and "error output" on your login console usually
tty1 (<alt>F1) every 3 seconds. Terminate the script with
Now simply add this to your
/etc/rc.local just before the
exit 0 line.
/home/pi/test.sh 1>/dev/tty2 2>/dev/console &
NEVER forget the ampersand
& at the line end! Otherwise your program will not go to the background and
rc.local never ends and you cannot login. Of course you can send the output on what terminal you want.
Why not using screen?
man screen says:
Screen is a full-screen window manager that multiplexes a physical
terminal between several processes (typically interactive shells).
That means it is not intended to be used in a non interactive environment. We not even have a login at boot. Only services with root rights are running. It may be possible to force
screen to run as a service at boot time but for me it isn't worth its effort because it isn't for this and may be error prone. As you see it is not easy. We have the native job control from the operating system and we can send jobs to the background and still running there, e.g. with
& and that is easy.
Specific to your example. If you execute:
rpi3 ~$ screen -d -m -S screenname ./app
rpi3 ~$ screen -ls
There are screens on:
1266.screenname (2018-03-28 11:02:21) (Detached)
As you can see screen is started
Detached so you do not see any output. This is from options
-d -m. To see some output from the running program you have to:
rpi3 ~$ screen -r
This works in a shell you are logged in. In a start job you have to do it in a script. Don't know if it works and where the output goes to. There is no login shell.