Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send stdout/stderr to console for a systemd service

Tags:

linux

systemd

I have an application that we're converting from using initd to systemd. The initd scripts used to run "myscript.bash start", but the user could also run "myscript.bash start". Now when the user runs "myscript.bash start", it runs systemctl (which itself launches "myscript.bash startup") to start the service. (This is obtuse, I know - the idea is to keep the version history of the contents of myscript.bash, but also to allow users to start the system the way they are used to, the switch to systemctl should be invisible).

Previously, if the user ran myscript.bash, they got a bunch of updates to the console on how the startup was going. Now that information is not going to the console. I've tried a couple of things, the most promising seemed to be setting StandardOutput & StandardError to tty:

StandardOutput=tty
StandardError=tty
ExecStart=/bin/bash -c './myscript.bash startup &'
ExecStop=/bin/bash -c './myscript.bash shutdown'

But I get this error:

systemd[20694]: Failed at step STDOUT spawning /bin/bash: Inappropriate ioctl for device

I've looked at this: How to Pipe Output to a File When Running as a Systemd Service? (which gave me the idea to try StandardOutput=), but the goal there is to write to a file and I'm trying to get output to the user's console.

Is this because we have a script running systemctl, instead of it being launched directly by the user? Is there a way to do this?

like image 238
Sasha Avatar asked Nov 25 '16 21:11

Sasha


People also ask

Where do systemd Service logs go?

With in-memory journaling, systemd creates its journal files under the /run/log/journal directory. The directory is created if it doesn't exist. With persistent storage, the journal is created under /var/log/journal directory; again, the directory is created by systemd if needed.

Does stderr go to syslog?

To view the stdout and stderr of a systemd unit use the journalctl command. By default stdout and stderr of a systemd unit are sent to syslog.


1 Answers

I wanted to do something similar, and setting StandardOutput=journal+console worked for me. FWIW this is the relevant systemd documentation https://www.freedesktop.org/software/systemd/man/systemd.exec.html.

In your case you want want to look at the TTYPath option, specifically the default value of systemd vs the default on your system. HTH.

like image 120
Amey D. Avatar answered Sep 22 '22 06:09

Amey D.