1

When I start some background process in the shell, for example:

geth --maxpeers 0 --rpc &

It returns something like:

[1] 1859

...without any out stream of this process. I do not understand what is it? And how can I get stdout of geth? There is information in documentation that stdout of background process is displayed in shell by default.

My shell is running in remote Ubuntu system.

3 Answers 3

2

The "&" directs the shell to run the command in the background. It uses the fork system call to create a sub-shell and run the job asynchronously.

The stdout and stderr should still be printed to the screen.

If you do not want to see any output on the screen, redirect both stdout and stderr to a file by:

geth --maxpeers 0 --rpc > logfile 2>&1 &
Sign up to request clarification or add additional context in comments.

3 Comments

++, but it's worth noting that this only silences (redirects) the command's output, but not the shell's job-control message.
Why stdout of a background process is not associated with terminal window by default? In my case it must be set directly: geth --rpc >&1 &
@Mergasov: You say you're running the shell remotely: what software are you using? Using ssh shouldn't be a problem. Is there anything special about how geth writes output? Do other commands that produce stdout output act the same? At first glance, >&1 looks like a no-op. Please add any clarifications directly to your question.
2

Regarding the first part of your question:

...without any out stream of this process. I do not understand what is it?

This is part of the command execution environment (part of the shell itself) and is not the result of your script. (it is how the shell handles backgrounding your script, and keeping track of the process to allow pause and resume of jobs).

If you look at man bash under JOB CONTROL, it explains what you are seeing in detail, e.g.

The shell associates a job with each pipeline. It keeps a table of currently executing jobs, which may be listed with the jobs command. When bash starts a job asynchronously (in the background), it prints a line that looks like:

    [1] 25647

Comments

1

I do not understand what is it? [1] 1859

It is the output from Bash's job feature, which enables managing background processes (jobs), and it contains information about the job just started, printed to stderr:

  • 1 is the job ID (which, prefixed with %, can be used with builtins such as kill and wait)

  • 25647 is the PID (process ID) of the background process.

Read more in the JOB CONTROL section of man bash.

how can I get stdout of geth? There is information in documentation that stdout of background process is displayed in shell by default.

Indeed, background jobs by default print their output to the current shell's stdout and stderr streams, but note that they do so asynchronously - that is, output from background jobs will appear as it is produced (potentially buffered), interleaved with output sent directly to the current shell, which can be disruptive.

You can apply redirections as usual to a background command in order to capture its output in file(s), as demonstrated in user3589054's helpful answer, but note that doing so will not silence the job-control message ([1] 1859 in the example above).

If you want to silence the job-control message on creation, use:

{ geth --maxpeers 0 --rpc & } 2>/dev/null

To silence the entire life cycle of a job, see this answer of mine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.