Automatically Launch Applications on tty

1. General Idea
2. Create A New Service
3. Start Your New Service On Boot
3.1. Enablind The Service
3.2. Preventing The Default Behaviour

tropf

ABSTRACT

Rundown on how to start an interactive console application automatically on boot using systemd. Useful for unattended Raspberry Pis.

1. General Idea

By default the service getty is launched. That will do the magic that opens a login prompt on any tty.

To automatically launch an application instead of that login prompt, one has to simply overwrite that behaviour.

2. Create A New Service

Create a file named /etc/systemd/system/mytui@.service.

[Unit]
Description=Custom user interface on %I
Documentation=https://unix.stackexchange.com/a/318336/5132
Conflicts=getty@%I.service
Before=getty.target

[Service]
ExecStart=/usr/local/bin/mytui
StandardInput=tty
StandardOutput=tty
Restart=always
RestartSec=1
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
TTYVTDisallocate=yes
SendSIGHUP=yes

[Install]
WantedBy=multi-user.target

Obviuosly adjust the filename and the ExecStart parameter.

3. Start Your New Service On Boot

3.1. Enablind The Service

Simply enable your new service:

systemctl enable mytui@tty1.service

tty1 works for debian. It also has to be tty1 as this is the one that will be selected by default.

Launching on tty2 would require switching to that tty after boot, and that would be the opposite of "unattended".

3.2. Preventing The Default Behaviour

Prevent systemd from starting the getty at the same time on the same tty:

systemctl mask getty@tty1.service

04 December 2020
Home