Score:0

Running a program at start up with output to screen and SSH session

sg flag

I have Ubuntu Server 22.04.

Tasks:

  1. To run console app on it, which will display some text information on the screen.
  2. Need to see app's output (messages) it on connected via HDMI monitor or TV and also see it remotely via SSH.
  3. OS and app started automatically after turning on power of the RPI. So no need to login, press anything etc.
  4. 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.

  1. App is here /home/user/myapp
  2. 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).

  1. Creating new session and run app: screen -S tty1 /home/user/myapp. See output on local screen.
  2. Connecting with SSH (manuals recommend use -t parameter. It works without it for me).
  3. 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»

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.