0

I have recently migrated my upstart scripts to systemd, unlike upstart, I don't see any output on the tty for services being started/stopped. To get that visual feedback, I added something like

      echo "Starting $UNIT_NAME" > $MYTTY

Where the MYTTY is an environment variable I am setting from output of tty command. I have 20 odd services but somehow not all messages were appearing on my tty. So I changed the line (just to check) to:

      echo "Starting $UNIT_NAME" | write myuser $MYTTY

And with this, I see all the messages being displayed! (of course, with the additional Message from <user>@<hostname> on <term> at <time> ... EOF

Just to check if write is doing something special, I checked the code in bsdutils and I didnt find anything special, its just writing character by character (with some handling for special chars and CR, LF)

What is wrong with :

      echo "Starting $UNIT_NAME" > $MYTTY

? I also tried:

      echo -e "Starting $UNIT_NAME\r\b" > $MYTTY

etc. But still I don't see messages from all the services on the screen! Is it because systemd starts up all services in parallel that some writes to the tty vanish !?

--

--EDIT--

the following round about way seems to work! but I want to know if this is safe or is there something better/simpler

 mkfifo /tmp/ttyfifo
 (cat > $MYTTY < /tmp/ttyfifo &) && echo -e 'Starting $UNIT_NAME\r\n'  > /tmp/ttyfifo
Ani
  • 32
  • 2
  • 13
  • Why are you not using `journald` for this? – Mircea Vutcovici Jun 04 '20 at 17:05
  • `journald` does not write to current logged in PTS (say, `/dev/pts/1`). There is a `TTYPath` config, but that is useful only when logged on `console`. For regular user, who SSHs to the host and lands on a `pts` and manages the services, how can they see the status on the terminal ? – Ani Jun 05 '20 at 07:16
  • A daemon shouldn't interact with terminals. The end users should use `systemctl status myservice` and `journalctl -u myservice -b` – Mircea Vutcovici Jun 05 '20 at 13:16
  • What is the real problem that you are trying to solve? – Mircea Vutcovici Jun 05 '20 at 13:18
  • @MirceaVutcovici I am trying to show the status to the user. And, I want to know if there is a way (or a better way) than what I am trying here. Agree that daemon should not interact with terminals, but in this case a user tries to start/stop a daemon - getting a status output (like `Service X starting...` on the terminal is not wrong! and I would want that feedback) – Ani Jun 05 '20 at 14:33
  • You can create an alias to start the service and tail a log. – Mircea Vutcovici Jun 05 '20 at 14:59

1 Answers1

2

in theory yes , as requests seem to be queued and the descriptor is not locked , you are better off using something like TTYBUS Multiplexer , as concurrent writes to a file/fifo might mess up line breaks etc.

Bash Stack
  • 430
  • 2
  • 6
  • 1
    Ok, I'll read more about this, it seems quite complicated to set up! BTW, do you see anything can go wrong with piping through a temp fifo (the last edit in the question) ? Or is that reliable? – Ani Jun 07 '20 at 15:13
  • in the end you should try `&>` instead of `>` eg. `dooMyFoo &> /tmp/ttyfifo` , because you also want to redirect STDERR , this way you can also mute cronjobs when they are too loud and send mails – Bash Stack Jun 08 '20 at 22:20