nohup redirection command difference - linux

What is the difference between these 2 below lines?
nohup $CATALINA_HOME/bin/startup.sh $CATALINA_HOME 2> /dev/null &
nohup $CATALINA_HOME/bin/startup.sh $CATALINA_HOME > /dev/null &
I've these lines in 2 of my projects having Tomcat server. One of them is having 2> & other one is just with > symbol.
Appreciate youe help!
Note: The line with 2> if ran in CentOS runs fine but the other one gives warning: "nohup: redirecting stderr to stdout"
Thanks!

Both redirect to /dev/null the first one redirects stderr the second one redirects stdout.
More on that: http://www.tldp.org/LDP/abs/html/io-redirection.html, also a few examples always from tldp http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
Side note: if you want to redirect both stdout and stderr you could do:
nohup $CATALINA_HOME/bin/startup.sh $CATALINA_HOME &> /dev/null &

Related

Suppress output from & in Linux terminal

I've seen answers from multiple posts talking about sending output from a program to /dev/null and also suppressing error output by using 2>&1, but how can I use all of these AND suppress output from sending it to a background process by typing the & symbol?
Example:
feh "image.jpg" & >/dev/null 2>&1
still returns "[#] {PID}" (with the numbers filled in of course). If anyone can help me out that would be greatly appreciated.
I am using Manjaro Linux and my kernel version is 5.11.10-1-MANJARO
Once & is encountered, the first command (feh "image.jpg") is sent to the background, then separately the shell executes >/dev/null 2>&1, which executed alone like this does nothing. You need to set up the output pipes/redirection before sending the process to background:
feh "image.jpg" > /dev/null 2>&1 & echo "Second command"
Note that > /dev/null is the same as 1> /dev/null which means you can do >/dev/null 2>/dev/null or 1>/dev/null 2>/dev/null to get the same effect.
For the job termination output, you can wrap your command in parenthesis like this:
(feh "image.jpg" > /dev/null 2>&1 & echo "Second command" )
As mentioned in the linked post, be aware that this will make you lose control of the process that you send to back with &.

How can I retain stderr from nohup?

Nohup redirects stderr to stdout if it points to a terminal. But I want to retain stderr output to the terminal
Is there a way to accomplish that? Is there an alternative?
I don't know if I understood correctly or not.
you mean that you don't want to see the error in terminal?
if yes:
if you want to save the error in file:
nohup command 2> file.txt
if you don't need the errors:
nohup command 2> /dev/null
2 means the error output of command
2> file.txt means write the error output to the file.txt
Just redirect it somewhere else, so it's not the terminal:
nohup bash -c 'echo OUT ; echo ERR >& 2' 2> err
You can redirect the stderr back to stdout instead of to a file to keep the output in the terminal, but it doesn't make much sense: nohup is for situations where the terminal might get lost, in which case you'll lose the stderr.
nohup bash -c 'echo OUT ; echo ERR >& 2' 2> >(cat)

How redirect nohup stdout to stdin

Is there a way to redirect the nohup output to the stdin instead of nohup.out ?
I've tried:
nohup echo Hello > /dev/stdin 2>&1 &
But it does not the trick.
The nohup command purposefully detaches itself from the stdin, so there is nothing it expects to read in itself, and thus I think what you are really after in this question, is redirecting the output of nohup as the stdin for the next command. (Well somebody has to read the stdin, and it ain't nohup.)
Further, POSIX mandates that the output goes to the nohup.out file in the working directory, if the file can be successfully opened. So what you can do is to wire the stdin of the following commands from the nohup.out file. For instance:
$ nohup echo Hello 2>/dev/null; read VAR 0<nohup.out; echo "VAR=$VAR"
VAR=Hello

What is the difference between ">" and "&>" in bash?

In bash we have 3 stream types:
0 (STDIN)
1 (STDOUT)
2 (STDERR)
So, while executing some program i can use these streams (e.g. i can redirect them from console to a file or smth like /dev/null, etc):
# only errors from STDERR will be shown, STDOUT will be moved to /dev/null
command > /dev/null
# only STDOUT will be shown, STDERR will be moved to /dev/null
command 2> /dev/null
I saw that some people write
command &> /dev/null
What is the difference between > and &> in bash?
what is the difference between ">" and "&>" in bash?
It's a bashism that redirects both stdout and stderr. It can also be achieved with the more portable:
command > file 2>&1

How to redirect the output of an application in background to /dev/null

I would like to redirect the output generated from a background application in Linux to /dev/null.
I am using kate text editor and it prints all the debug messages on the terminal which I would like to redirect to /dev/null.
Any idea how to do it ?
Thanks
You use:
yourcommand > /dev/null 2>&1
If it should run in the Background add an &
yourcommand > /dev/null 2>&1 &
>/dev/null 2>&1 means redirect stdout to /dev/null AND stderr to the place where stdout points at that time
If you want stderr to occur on console and only stdout going to /dev/null you can use:
yourcommand 2>&1 > /dev/null
In this case stderr is redirected to stdout (e.g. your console) and afterwards the original stdout is redirected to /dev/null
If the program should not terminate you can use:
nohup yourcommand &
Without any parameter all output lands in nohup.out
These will also redirect both:
yourcommand &> /dev/null
yourcommand >& /dev/null
though the bash manual says the first is preferred.

Resources