I’m writing a simple desktop initiation script which waits for disk idle, and then launches next external program (like Firefox, Skype or conky) using &, like:
ps cax | grep conky > /dev/null if [ $? -eq 0 ]; then echo "Conky is already running." else wait-for-disk-idle sda conky & fi
That’s easy.
The problem is that some programs spew a lot of debug output to the terminal, which gets mixed with the messages produced by my initialization script.
The question:
Is there any way to asynchronously launch an external program so that its standard output is discarded?
What I already tried:
conky & >/dev/null 2>/dev/nullbash -c conky &
The correct answer:
bash -c "conky >/dev/null 2>/dev/null &"
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You probably want to discard any STDERR output as well. You can do both like so:
conky > /dev/null 2>&1 &
This statement essentially tells the shell to do the following:
conky > /dev/null– redirect all standard output to /dev/null2>&1– Redirect standard error to where standard output is currently pointing. Because of the previous redirect, you standard output is pointing to /dev/null, so standard error will follow.&– Run this in a sub-shell (background).- Thanks to @alexis for pointing out that my description for
&wasn’t quite precise:
(a) Every external command must be run after a (v)fork, not just background processes. (b) Backgrounded processes are not run in a sub-shell, but executed immediately after the fork. The real difference when a process is backgrounded is that the invoking shell doesn’t immediately wait(2) for it (but prints a prompt and awaits user input).
When redirecting output, Bash reads the redirects in order, from left to right. Bash also treats the ‘&’ as a command separator, which can be used anywhere ‘;’ would normally be used. What you were doing was telling bash
- Run conky in the background. More specifically, fork the process and run it in a sub-shell, asynchronously, and return control of the terminal to the user.
- Bash considers this a new command – this is the same as running
>/dev/nullon the prompt with nothing preceding the redirect. Nothing happens. - Redirect the standard error to /dev/null from the nonexistent command.
Method 2
Instead of
conky & >/dev/null 2>/dev/null
Use:
conky >/dev/null 2>/dev/null &
Explanation: Because ‘&’ serves as a statement separator, the former is really two commands. It works like this:
conky & >/dev/null 2>/dev/null
The first command runs conky in the background but does not redirect its output. The second redirects the output on a nonexistent command. To redirect conky’s output, you need to run:
conky >/dev/null 2>/dev/null
To redirect conky’s output and also run conky in the background, use:
conky >/dev/null 2>/dev/null &
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0