>/dev/null 2>&1 detailed explain

# crontab -l

5 4 * * * /home/script.sh >/dev/null 2>&1

//explain

2 - standard error

1 - standard output

0 - standard input

/dev/null - standard file that discards all you write to it, but reports that the write operation is successes. 

>/dev/null 2>&1 here the standard error is redirect to standard output

>>/dev/null 2>&1 here all the standard error are collecting to standard output

Examples

//below is the example of exit code , 0 - stand for successful execution, rest other number have failed/other reason




//stdout example

[root@centos boobalan]# hostname > out.txt
[root@centos boobalan]# cat out.txt
centos.boobi.com

//stderr example

[root@centos boobalan]# hos > out.txt
bash: hos: command not found...
[root@centos boobalan]# cat out.txt
//here out.txt have not caputured anything, so we use below to get the error

[root@centos boobalan]# hos 2> out.txt
[root@centos boobalan]# cat out.txt
bash: hos: command not found...
//now out.txt having the standard error

//purpose of this standard error if you get lot of error when execute the command then you can't be check the exact output you looking for. this is helps to ignore the error and give you the exact output precisely 


[boobalan@centos ~]$ grep -r boobal / 2> /dev/null
Binary file /proc/3804/task/3804/environ matches
Binary file /proc/3804/environ matches
Binary file /proc/3815/task/3815/environ matches

//likewise if you want to view only the error not success one then use 1 

[boobalan@centos ~]$ grep -r boobi /home 1> /dev/null
grep: /home/Nandhini: Permission denied
[boobalan@centos ~]$

//if you want both stderr and stderr need to ignore then

[boobalan@centos ~]$ grep -r boobi /home &> /dev/null
[boobalan@centos ~]$

Post a Comment

0 Comments