Hello All,
I have some unique requirement.
I have written a very lengthy script which calls number of resource script to execute a particular task.
What I want is output of each command(called from main script and resource scripts) should go to a particular file and I should be able to access that file within my main script (Just want to attach it to mail.). Only output of "echo" command should appear on console.
Is there any way to do this ? I thought of
"Script"
command but it prints output to console as well.
Another way is to redirect output of each command to file but I have more than 2000 commands in total so that looks tough and something that I want to avoid.
Thank you very much.
ok I think we have to keep some few things in mind here like file should STOP updating before sending to it mail as attachment, we can put a sleep process for 1 or 2 mins after your script completion. Then we can fetch the file and snd it to mail as an attachment.
For scripts which you want to execute in Bacground or you don't want their output to be shown at screen please use nohup command for same.
#! /bin/ksh
admin=admin@some.com
some_command
some_script
...
some_command
some_script
if [ -f /dir/log ]
then
cat /dir/log | mailx -s 'logfile for script is ready' $admin
else
echo "script has no entries in /dir/log up to this point" >> /dir/log
fi
exit 0
or ...
you can just create one big function in your script and redirect the output for that function into one log file ...
#! /bin/ksh
admin=admin@some.com
myfunction(){
some_command
some_script
...
some_command
some_script
}
myfunction > /dir/log 2>&1
echo "i'm done"
if [ -f /dir/log ]
then
cat /dir/log | mailx -s 'logfile for script is ready' $admin
echo "i am done. report emailed to $admin"
else
echo "script has no entries in /dir/log up to this point"
fi
exit 0