Running scripts in the background or for cron jobs can product output that you would like to redirect. The standard way to redirect errors and output in Linux is to /dev/null.
Command line Bash Script Usage
- To run a script and redirect errors and output
$ ExampleScript.sh > /dev/null 2>&1
Cron Bash Script Usage
- Run a cron job every 30 minutes and redirect error and output from script
*/30 * * * * ~/bin/CheckServerStatus.sh > /dev/null 2>&1
What is the 2>&1 syntax?
- Stdin is 0
- Stdout is 1
- Stderr is 2
- If 2>1 was defined to redirect stderr to stdout, it would be interpreted as redirect stderr to a file named 1
- Using & defines what follows is a file descriptor and not a filename
- Leads to : 2>&1
What is /dev/null?
- Virtual file on Linux
- Data written to /dev/null is discarded
- Known sometimes as the "Black hole"
No comments:
Post a Comment