Wednesday, August 10, 2011

How to redirect output of script to a file(Need to save log in a file and file should be mentioned in the script itself?

■ Requirement : redirect output of script to a file(Need to save log in a file and file should be mentioned in the script itself)
■ OS Environment : Linux[RHEL, Centos]
■ Application: bash
■ Implementation Steps : 

1. Create a bash script.
2. add following line :

exec > >(tee /var/log/my_logfile.txt)

Example :

$ cat /etc/init.d/crond |head -5
#! /bin/bash -x
exec > >(tee /var/log/my_cron_logfile.txt)

$ service crond restart 
$ cat /var/log/my_cron_logfile.txt
Starting crond: [ OK ]

No comments:

Post a Comment