0

I want to run a program at startup, so I create a .sh with /.myprogram. I added to rc.local and program run in every boot, but I want to see program on console, so I need to attach a screen.

If I do in terminal screen -d -m -S screen_name /home/pi/start.sh everything runs ok like I want: Program starts in screen_name and runs ok.

But if I add this command to rc.local, new screen doesn't start. And I tried to put this screen command to start.sh but didn't work too.

What am I doing wrong?

  • rc.local is run as the root user, so the screen "console" will be visible under the root user. – onion Mar 27 '18 at 10:33
  • You are using Raspbian? Do you use a graphical system? Where do you want to get the output? On the virtual terminal tty1 where you also get the boot messages? For this is an example in /etc/rc.local to output the ip address. Or output in a window with terminal emulation? When? Before login or after? – Ingo Mar 27 '18 at 10:44
  • Yes, im using Raspbian and cli. I want run the program at boot and output on terminal with boot msgs, or in another screen doesnt matter. So the point is when I connect through shh, can see the program. Should I use crontab to run .sh on boot? THX – Ne0x Mar 27 '18 at 11:02
1

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 tty1, tty2 and so on. You can switch to it with <alt>F1 (tty1), <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
#!/bin/bash
while true; do
    echo "hello world"       # outputs to normal stdout
    echo "error output" >&2  # outputs to stderr
    sleep 3
done
EOF
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 <ctrl>C.

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)
rpi3 ~$

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.

  • thanks for all help. But I cant understand what are Im doing wrong. I have a .sh with this code #!/bin/bash cd /home/pi screen -d -m -S screenname ./app If I run this .sh on terminal all works ok, but if I add /home/pi/start.sh to rc.local or @reboot on crontab doesnt work. Why? – Ne0x Mar 28 '18 at 6:05
  • I have updated my answer – Ingo Mar 28 '18 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.