When processes get executed in the background, for example by using systemd to start the processes at startup, why doesn't the stdout and stderr still appear on the terminal when we access the terminal via SSH or serial? Where does it go exactly?

I understand that if we directly run a process from the terminal, that that process will become a child process of the terminal instance and hence, stdout and stderr will write to a file descriptor understood by the terminal.

However it's not understood where the stdout and stderr is going when a process is executed in the background not from the terminal. Thanks in advance .

  • 4
    Does this answer your question? View stdout/stderr of systemd service
    – muru
    Apr 29, 2021 at 8:00
  • 1
    stdout and stderr are file descriptors 1 and 2, respectively. A process inherits the file descriptors from its parent. Whatever the parent uses as stdout and stderr, the child uses as well. Of course, the child has full control - it can close stdout and stderr and open other files. For example, the child may detect it's running in the background and decide to write its messages to /var/log/myownlogfile. In short, there is no general answer to your question; it depends on parent and child. Apr 29, 2021 at 8:14
  • 2
    “that process will become a child process of the terminal instance” — no, it’s a child of the shell, and uses whatever file descriptors the shell gives it. Apr 29, 2021 at 8:27
  • @berndbausch “Whatever the parent uses as stdout and stderr, the child uses as well” not necessarily, the child uses whatever the parent gives it by default (e.g. with redirections, the shell doesn’t pass its own descriptors to the child, it passes the result of the redirection). Apr 29, 2021 at 8:32
  • 1
    @StephenKitt Let me see if I understand it correctly. The shell forks, creating a child identical to itself. That child gets the parent's FD's, which it might then redirect using a combination of open, close and dup system calls. So, in my understanding it's the child that inherits the parent's files and redirects them. Of course, at this point in time, the child is still a shell, and only after redirection will it exec the program. Perhaps we are both right. Apr 29, 2021 at 10:09


You must log in to answer this question.

Browse other questions tagged .