3

I have the following service:

[Unit]
Description="Hoping to get rich"

[Service]
ExecStart='/home/me/Dev/hellogoogle.sh'
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=default.target

As you can see, it's a very simple service that executes a shell script. The shell script is as follows:

#!/usr/bin/sh

echo "Here we go"
ping google.com | grep '.'
echo "And we're rich"

When I start the service with the above script, the only output I get is Here we go. However, if I remove the | grep '.' i.e. I don't pipe the output, the service show what the echo commands display.

This is how I monitor output (using journald):

journalctl --follow --user-unit=hello-google.service

Can someone help me understand what is going on here? Why is systemd not showing any output from the shell script just because the script pipes some of its output to another command?

Note, this is running as a user service, not system.

3
  • I don't understand why you do grep '.' in the first place - in regex, a dot . matches any character. Also, qutiong on the last echo is wrong.
    – Panki
    Jan 5, 2021 at 9:55
  • @Panki that's exactly the point. It should print the same thing as the output of ping, but because of the | it is not doing that. Also good catch with the quotes
    – smac89
    Jan 5, 2021 at 9:56
  • Try appending 2>&1? It might show an error you otherwise don't see
    – Panki
    Jan 5, 2021 at 10:06

1 Answer 1

0

Alright so I wrestled with it a bit more and it looks like the problem was simply that grep buffers its output, and then doesn't show it even after the command ends...still not sure why this is the case.

The solution?

ping google.com | grep --line-buffered '.'

OR

ping google.com | stdbuf --output=L grep '.'

According to this answer on SO, the output may not show up due to the fact that the output buffer is not yet full. So that explains why even after the program exits, no output is shown.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .