I have Ubuntu Server 22.04.
Tasks:
- To run console app on it, which will display some text information on the screen.
- Need to see app's output (messages) it on connected via HDMI monitor or TV and also see it remotely via SSH.
- OS and app started automatically after turning on power of the RPI. So no need to login, press anything etc.
- App started automatically and if it hangs or exit, it restarted automatically.
Additional tasks:
5. To save all output and error messages of app to separate log file in app folder if possible, but not to systemd’s journal.
6. Limit log file size.
7. Need an opportunity to login locally with connected keyboard and via SSH to run updates and do some maintenance etc.
That's what I managed to achieve:
I guess the best way is to use method with Systemd Service.
- App is here /home/user/myapp
- I created myapp.service:
[Unit]
Description=My App
After=network.target
[Service]
Type=simple
ExecStart=/home/user/myapp
StandardOutput=tty
StandardError=tty
TTYPath=/dev/tty1
User=user
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
App running automatically and displays info at connected monitor without login. I can switch to new tty with Ctrl + Alt +F2 with attached keyboard and can login to OS for maintenance. Some of the tasks have been completed.
Is it correct method? May be there is some better way?
When I connect via SSH I see pts0, so don’t see output of app :(
I tried to use screen command but it works only after login if I start app manually (myapp.service stoped).
- Creating new session and run app: screen -S tty1 /home/user/myapp. See output on local screen.
- Connecting with SSH (manuals recommend use -t parameter. It works without it for me).
- Attaching to session: screen -x. See output via SSH and on local screen. Can detach and run command for maintenance. Is there some way to run screen -x on server side automatically after connecting via SSH?
It works, but it not when I try to run it with myapp.service:
[Unit]
Description=My App
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/screen -S tty1 /home/user/myapp
ExecStop=/usr/bin/screen -X quit
StandardOutput=tty
StandardError=tty
TTYPath=/dev/tty1
User=user
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
Don’t works. Have error message on local screen «Must be connected to a terminal»