9

I am not able to figure out what option to pass in StandardOutput= for a unit file (.service), where I want to show some messages on the connected terminal from where the service is started! (console/tty doesn't seem to be what I want)

Maybe /etc/systemd/system.conf's global default DefaultStandardOutput=journal can be changed, but I don't want to do that! I am only interested in showing some progress/startup messages from my unit file (like systemd-run -P)

I am on Ubuntu 18.04.2

# systemd --version
systemd 237
Ani
  • 59

3 Answers3

1

You can take a look at https://www.freedesktop.org/software/systemd/man/systemd.exec.html#StandardOutput=

If I understand the question correctly you want the service outputs status to tty / console? Remember that when a service is started systemd take care of default file descriptors.

You can use a file to store the status and query it there.

Securez
  • 11
1

Edit your script you want to run using systemd and add | logger to command you want output from to syslog:

#!/bin/bash
/usr/bin/ps aux | /usr/bin/grep gnome > /home/user/test/psoutput.txt
/usr/bin/date | logger
echo "This is terminal output" | logger
/usr/bin/date >> /home/user/test/psoutput.txt

Output of $ sudo journalctl -u pstofile -f is:

бер 09 13:33:42 lenovo systemd[1]: Started ps aux to file.
бер 09 13:33:42 lenovo root[8491]: понеділок, 9 березня 2020 13:33:42 +0200
бер 09 13:33:42 lenovo root[8493]: This is terminal output
бер 09 13:33:42 lenovo systemd[1]: pstofile.service: Succeeded.

My systemd service file contains the next:

[Unit]
Description=ps aux to file
After=systemd-user-sessions.service

[Service]
Type=simple
Restart=always
RestartSec=5
ExecStart=/home/user/test/pstofile.sh
StandardInput=journal+console
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target
Gryu
  • 8,002
  • 9
  • 37
  • 53
0

Its been quite long, I just want to update how I solved this. I created a POSIX MQ, on which a daemon listens on. This daemon is in charge of displaying to the current pty (mq_receive() + write()). All the daemons send a message on the queue when they start, and when they stop (with mq_send())

When I start the main service, I will also start this display service giving it, the pty info.

Ani
  • 59