5

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?

Community
  • 1
  • 1
Sasha
  • 3,405
  • 4
  • 19
  • 21

2 Answers2

10

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.

silvio
  • 2,174
  • 20
  • 36
Amey D.
  • 116
  • 1
  • 3
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Roman Marusyk Jan 09 '17 at 23:14
  • I'm not getting the desired result with journal+console. Does "console" mean the system console, i.e. the monitor attached to the machine, or does it mean the user session that ran systemd? In my case I'm sshed into a machine and am running systemd --user start on a unit that forke. I want to see the ExecStart output in realtime in my ssh session. – papercrane Mar 02 '18 at 23:39
  • From the link in the answer: "Journal+console, syslog+console and kmsg+console work in a similar way as the three options above but copy the output to the system console as well." See this answer for more info about system console: https://unix.stackexchange.com/questions/60641/linux-difference-between-dev-console-dev-tty-and-dev-tty0 – xloto Aug 27 '18 at 17:45
0

Get you tty:

ste@rockpi:~$ tty
/dev/tty1

Set this patch on service:

[Unit]
Description=you_description

[Service]
ExecStart=/home/user/you_script.sh
StandardOutput=tty
TTYPath=/dev/tty1

[Install]
WantedBy=multi-user.target

Example code:

                                                                             install_image_emmc.sh                                                                                            #!/bin/bash

IMAGE=`ls /home/ste/image_*`

if [ -f "$IMAGE" ]; then
    /bin/echo -e "Hello\n"
    /bin/sleep 5
    /bin/echo "Good luck!"
    reboot
else
    /bin/echo "Something went wrong please call the PROGRAMMER!"
fi