Operating System - HP-UX
1828577 Members
2578 Online
109982 Solutions
New Discussion

redirecting output/error of a background job started from a script file

 
SOLVED
Go to solution
Mangal Pandey
Occasional Contributor

redirecting output/error of a background job started from a script file

I'm trying to invoke one shell script (say "script2")in background from another shell script (say "script1" running on foreground). Script1 is sending all the messages to a logfile (say "log1"). now i want to redirect all the messages generated from script2 to a different log file, but it is always sending its messages to "log1" only.

here is the script1:

--script1 starts----
-----------------
---doing its own processing-----
---doing its own processing-----

ksh script2 >& log2 &

---script1 stops------





7 REPLIES 7
Oviwan
Honored Contributor
Solution

Re: redirecting output/error of a background job started from a script file

Hey

redirect it like that:

script2 > logfile 2>&1

and put it in the backgroup

Regards
Mangal Pandey
Occasional Contributor

Re: redirecting output/error of a background job started from a script file

This is not working and logs are still going to parents log file.
Oviwan
Honored Contributor

Re: redirecting output/error of a background job started from a script file

this should redirect STDOUT and STDERR to the logfile...

what if you execute it in a subshell:

$(script2 > logfile2 2>&1) &

Dennis Handly
Acclaimed Contributor

Re: redirecting output/error of a background job started from a script file

>Oviwan: redirect it like that:
script2 > logfile 2>&1

>This is not working and logs are still going to parents log file.

I can't see how it does that unless script2 is tricky. What's in that file? Do you see log1 there? Unless there is a redirect on every line, the only way to redirect inside the script is to do it on an exec line:
exec > log1 2>&1
Peter Nikitka
Honored Contributor

Re: redirecting output/error of a background job started from a script file

Hi,

my additional question is, if 'script1' is written in posix shell syntax or if its csh syntax. In a C-compatible shell, doing something like this
prompt: echo $SHELL
/bin/tcsh
prompt: ls /tmp /notexiting >& ~/tmp/out&

will produce output on stderr like
[1] 24483
[1] Exit 2 ls /tmp >& ~/tmp/out

but the file ~/tmp/out will contain both stdout and stderr of the above command.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Mangal Pandey
Occasional Contributor

Re: redirecting output/error of a background job started from a script file

Thank you..its working fine now.
there was a redirection in script2 itself which was causing this problem and it was hidden somwhere in the big script file.

Thanks all for your prompt reply.
Dennis Handly
Acclaimed Contributor

Re: redirecting output/error of a background job started from a script file

You haven't assigned any points yet. Please read the following:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33