In bash, standard (1) and error (2) output can be re-routed and discarded with:
>/dev/null 2>&1
But the following example does something different:
nohup myscript.sh >myscript.log 2>&1 </dev/null &
What is the meaning/function of </dev/null
in the example above? In what sort of scripting scenario would it be useful?
(Example Source)
/dev/null is a null device–a special type of virtual device. It is present in every Linux system, and the purpose of this device is to discard anything sent to it and read the End of File (EOF). Most virtual devices are used to read data; however, /dev/null is unique since it is used to suppress any data written to it.
/dev/null is a special filesystem object that discards everything written into it. Redirecting a stream into it means hiding your program's output. The 2>&1 part means "redirect the error stream into the output stream", so when you redirect the output stream, error stream gets redirected as well.
tail -f /dev/null is usually added because the process (pid 1) in your docker container is not running in the foreground and if nothing is running in the foreground, docker automatically closes itself.
The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection. The /dev/null device is a special file, not a directory, so one cannot move a whole file or directory into it with the Unix mv command.
/dev/null is a null device–a special type of virtual device. It is present in every Linux system, and the purpose of this device is to discard anything sent to it and read the End of File (EOF). Most virtual devices are used to read data; however, /dev/null is unique since it is used to suppress any data written to it.
The Bash null command : is a shell builtin defined in the POSIX standard. It is there mostly for historical reason and compatibility with older shells but it can still serve a purpose in Bash today.
You're using /dev/null differently. cat /dev/null outputs the "contents" of /dev/null, which is to say its output is blank. > messages (or > wtmp) causes this blank output to be redirected to the file on the right side of the > operator.
Another reason to redirect to /dev/null is to prevent an unused file descriptor being created for stdin. This can minimize the total open file handles when you have many long running processes. Show activity on this post. </dev/null is used to avoid having the script wait for input.
Redirecting /dev/null
to stdin will give an immediate EOF
to any read call from that process. This is typically useful to detach a process from a tty (such a process is called a daemon). For example, when starting a background process remotely over ssh, you must redirect stdin to prevent the process waiting for local input.
Another reason to redirect to /dev/null is to prevent an unused file descriptor being created for stdin. This can minimize the total open file handles when you have many long running processes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With