Thursday, August 23, 2012

Log BASH command error and write the output to a file along with the STDOUT

BASH shell error (STDERR) is displayed on the screen by default.  For the following find command, all the output, including error is displayed on screen

find /var/lib/. -name “*.py”

bash$ find /var/lib/. -name "*.py*"
find: /var/lib/./zfs: Permission denied
find: /var/lib/./dav: Permission denied
find: /var/lib/./mlocate: Permission denied
find: /var/lib/./php/session: Permission denied
find: /var/lib/./ldap: Permission denied
find: /var/lib/./aide: Permission denied
find: /var/lib/./dovecot: Permission denied
find: /var/lib/./nvidia: Permission denied
find: /var/lib/./nfs/statd: Permission denied
find: /var/lib/./pgsql: Permission denied
find: /var/lib/./sss/pipes/private: Permission denied
find: /var/lib/./sss/db: Permission denied
find: /var/lib/./iptraf: Permission denied
/var/lib/./zope/bin/zopeservice.py
/var/lib/./zope/bin/zopeservice.pyo
/var/lib/./zope/bin/zopeservice.pyc
find: /var/lib/./imap: Permission denied
find: /var/lib/./mailman/archives/private: Permission denied
find: /var/lib/./dhcpv6: Permission denied


If the following command is used, it will only write out the ‘find’ result to the log file, not any error.

find /var/lib/. -name “*.py” > find_py.log

How can you log the error along with the output to a file so that you can read it later or from a remote session?


Follow the commands and output below


find /var/lib/. -name “*.py” > find_py.log 


[Only STDOUT written to a log file; (STDERR output to screen only) ]

bash$ find /var/lib/. -name "*.py*" > find_py.log
find: /var/lib/./zfs: Permission denied
find: /var/lib/./dav: Permission denied
find: /var/lib/./mlocate: Permission denied
find: /var/lib/./php/session: Permission denied
find: /var/lib/./ldap: Permission denied
find: /var/lib/./aide: Permission denied
find: /var/lib/./dovecot: Permission denied
find: /var/lib/./nvidia: Permission denied
find: /var/lib/./nfs/statd: Permission denied
find: /var/lib/./pgsql: Permission denied
find: /var/lib/./sss/pipes/private: Permission denied
find: /var/lib/./sss/db: Permission denied
find: /var/lib/./iptraf: Permission denied
find: /var/lib/./imap: Permission denied
find: /var/lib/./mailman/archives/private: Permission denied
find: /var/lib/./dhcpv6: Permission denied

bash$ cat find_py.log
/var/lib/./zope/bin/zopeservice.py
/var/lib/./zope/bin/zopeservice.pyo
/var/lib/./zope/bin/zopeservice.pyc



find /var/lib/. -name “*.py” 2> find_py.log


[Only STDERR written to a log file; (No STDERR output to screen; Only STDOUT output on screen)]

bash$ find /var/lib/. -name "*.py*" 2> find_py.log
/var/lib/./zope/bin/zopeservice.py
/var/lib/./zope/bin/zopeservice.pyo
/var/lib/./zope/bin/zopeservice.pyc

bash$ cat find_py.log
find: /var/lib/./zfs: Permission denied
find: /var/lib/./dav: Permission denied
find: /var/lib/./mlocate: Permission denied
find: /var/lib/./php/session: Permission denied
find: /var/lib/./ldap: Permission denied
find: /var/lib/./aide: Permission denied
find: /var/lib/./dovecot: Permission denied
find: /var/lib/./nvidia: Permission denied
find: /var/lib/./nfs/statd: Permission denied
find: /var/lib/./pgsql: Permission denied
find: /var/lib/./sss/pipes/private: Permission denied
find: /var/lib/./sss/db: Permission denied
find: /var/lib/./iptraf: Permission denied
find: /var/lib/./imap: Permission denied
find: /var/lib/./mailman/archives/private: Permission denied
find: /var/lib/./dhcpv6: Permission denied

find /var/lib/. -name “*.py” 2> find_py.log 1>&2 


STDOUT and STDERR written to a log file; (Nothing output to screen)

bash$ find /var/lib/. -name "*.py*" 2> find_py.log 1>&2

bash$ cat find_py.log
find: /var/lib/./zfs: Permission denied
find: /var/lib/./dav: Permission denied
find: /var/lib/./mlocate: Permission denied
find: /var/lib/./php/session: Permission denied
find: /var/lib/./ldap: Permission denied
find: /var/lib/./aide: Permission denied
find: /var/lib/./dovecot: Permission denied
find: /var/lib/./nvidia: Permission denied
find: /var/lib/./nfs/statd: Permission denied
find: /var/lib/./pgsql: Permission denied
find: /var/lib/./sss/pipes/private: Permission denied
find: /var/lib/./sss/db: Permission denied
find: /var/lib/./iptraf: Permission denied
/var/lib/./zope/bin/zopeservice.py
/var/lib/./zope/bin/zopeservice.pyo
/var/lib/./zope/bin/zopeservice.pyc
find: /var/lib/./imap: Permission denied
find: /var/lib/./mailman/archives/private: Permission denied
find: /var/lib/./dhcpv6: Permission denied



find /var/lib/. -name “*.py” 2>&1 | tee find_py_tee.log


STDOUT and STDERR written to a log file; (Also displayed on screen live while writing the log)

bash$ find /var/lib/. -name "*.py*" 2>&1 | tee find_py_tee.log
find: /var/lib/./zfs: Permission denied
find: /var/lib/./dav: Permission denied
find: /var/lib/./mlocate: Permission denied
find: /var/lib/./php/session: Permission denied
find: /var/lib/./ldap: Permission denied
find: /var/lib/./aide: Permission denied
find: /var/lib/./dovecot: Permission denied
find: /var/lib/./nvidia: Permission denied
find: /var/lib/./nfs/statd: Permission denied
find: /var/lib/./pgsql: Permission denied
find: /var/lib/./sss/pipes/private: Permission denied
find: /var/lib/./sss/db: Permission denied
find: /var/lib/./iptraf: Permission denied
/var/lib/./zope/bin/zopeservice.py
/var/lib/./zope/bin/zopeservice.pyo
/var/lib/./zope/bin/zopeservice.pyc
find: /var/lib/./imap: Permission denied
find: /var/lib/./mailman/archives/private: Permission denied
find: /var/lib/./dhcpv6: Permission denied

bash$ cat find_py_tee.log
find: /var/lib/./zfs: Permission denied
find: /var/lib/./dav: Permission denied
find: /var/lib/./mlocate: Permission denied
find: /var/lib/./php/session: Permission denied
find: /var/lib/./ldap: Permission denied
find: /var/lib/./aide: Permission denied
find: /var/lib/./dovecot: Permission denied
find: /var/lib/./nvidia: Permission denied
find: /var/lib/./nfs/statd: Permission denied
find: /var/lib/./pgsql: Permission denied
find: /var/lib/./sss/pipes/private: Permission denied
find: /var/lib/./sss/db: Permission denied
find: /var/lib/./iptraf: Permission denied
/var/lib/./zope/bin/zopeservice.py
/var/lib/./zope/bin/zopeservice.pyo
/var/lib/./zope/bin/zopeservice.pyc
find: /var/lib/./imap: Permission denied
find: /var/lib/./mailman/archives/private: Permission denied
find: /var/lib/./dhcpv6: Permission denied



The 2>&1 redirect STDERR to STDOUT.
This will record any errors to the logfile along with STDOUT.