Operating System - HP-UX
1833710 Members
2737 Online
110063 Solutions
New Discussion

find out where stdout and stderr have been redirected to

 
Chris Justice
Occasional Contributor

find out where stdout and stderr have been redirected to

My question is, in crontab, say I have a line as follows:

05 * * * * /home/Utility/myjob.sh > /myjob.log

In the shell script myjob.sh, how can I determine that stdout has been redirected to /myjob.log? Or is there a way to see the "whole" command line that was used to execute the script? I know $0 gives me the script name, but not the rest of the line. Thanks for any help.
8 REPLIES 8
Geoff Wild
Honored Contributor

Re: find out where stdout and stderr have been redirected to

It would go to root's email from cron...

Change your entry to:

05 * * * * /home/Utility/myjob.sh > /myjob.log 2>&1

Then all errors will go to your log file...

Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Chris Justice
Occasional Contributor

Re: find out where stdout and stderr have been redirected to

I'm not looking for sending the errors, I knew this. I want to find out the file that the output is being directed to, or be able to return the complete command line used to run the script, so I can parse it. Thanks.
Bharat Katkar
Honored Contributor

Re: find out where stdout and stderr have been redirected to

hi,
If is understood the question properly, i can say that first check the return code of the command and then the size of the file myjob.log.
If the size changes after the command has executed that means it has redirected the output to your log file (assuming command really generate some output).
Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Chris Justice
Occasional Contributor

Re: find out where stdout and stderr have been redirected to

I must not be making this clear.

Say someone does the following command:

/root/utility_script.sh > /user1/test.log

In side the script utility_script.sh, I do not know whether the output has been redirected. I want to know how to find out what stdout is assigned (directed) to when the script is executed. In this case, the script would find out that stdout has been redirected to /user1/test.log. I need to find the filename that stdout has been redirected to, or the tty if it has not been redirected. Thanks. Hope this is clearer.
Bharat Katkar
Honored Contributor

Re: find out where stdout and stderr have been redirected to

Let us work on your question first :) ----
You wrote ........
In this case,

the script would find out that stdout has been redirected to /user1/test.log.

I need to find the filename that stdout has been redirected

..................................


Now i can't differentiate the two statements above!!!

Well the answer to the second statemnt comes as: myjob.log

Regards
You need to know a lot to actually know how little you know
Geoff Wild
Honored Contributor

Re: find out where stdout and stderr have been redirected to

Ah - misunderstood - you just want to know how to tell...

In the first line of the myjob.sh script, add a timestamp:

print "### $0 executed on `/usr/bin/date` ###"

Then check the log...

Rgds...Geoff





Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Rodney Hills
Honored Contributor

Re: find out where stdout and stderr have been redirected to

The current pid will be "$$". You could run lsof and it will display current file assignments. example-

lsof -p$$ -a -d2 -d1

This will display the STDOUT and STDERR mappings for the current process.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: find out where stdout and stderr have been redirected to

If you are concerned someone redirects STDOUT when they run the script, you can redirect it where you want it to go in your script "root/utility_script.sh"

exec >/tmp/utility_script.out 2>&1

as the first line in your script, will make sure that output always goes to that file.

You can also test STDOUT to see if it is assigned to the terminal with-

if [[ -t 1 ]] ; then
echo "STDOUT is on a terminal..."
fi

HTH

-- Rod Hills
There be dragons...