diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 000baae0dfe..3424819540a 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -2310,6 +2310,7 @@ static int on_signal(sd_event_source *s, const struct signalfd_siginfo *si, void static int setup_event(Context *c, int fd, sd_event **ret) { _cleanup_(sd_event_unrefp) sd_event *e = NULL; + struct stat st; int r; assert(arg_follow); @@ -2328,10 +2329,15 @@ static int setup_event(Context *c, int fd, sd_event **ret) { if (r < 0) return log_error_errno(r, "Failed to add io event source for journal: %m"); - /* Also keeps an eye on STDOUT, and exits as soon as we see a POLLHUP on that, i.e. when it is closed. */ - r = sd_event_add_io(e, NULL, STDOUT_FILENO, EPOLLHUP|EPOLLERR, NULL, INT_TO_PTR(-ECANCELED)); - if (r < 0) - return log_error_errno(r, "Failed to add io event source for stdout: %m"); + if (fstat(STDOUT_FILENO, &st) < 0) + return log_error_errno(r, "Failed to stat stdout: %m"); + + if (IN_SET(st.st_mode & S_IFMT, S_IFCHR, S_IFIFO, S_IFSOCK)) { + /* Also keeps an eye on STDOUT, and exits as soon as we see a POLLHUP on that, i.e. when it is closed. */ + r = sd_event_add_io(e, NULL, STDOUT_FILENO, EPOLLHUP|EPOLLERR, NULL, INT_TO_PTR(-ECANCELED)); + if (r < 0) + return log_error_errno(r, "Failed to add io event source for stdout: %m"); + } if (arg_lines != 0 || arg_since_set) { r = sd_event_add_defer(e, NULL, on_first_event, c);