How get program name that produced an IO error redirected to a LOG in a nohup command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How get program name that produced an IO error redirected to a LOG in a nohup command?
# 1  
Old 02-01-2013
Question How get program name that produced an IO error redirected to a LOG in a nohup command?

Good afternoon,

I'm have program that executes in background another programs. The main program would be programA and the programs executed by the main one, would be program1a, program1b and program1c.

I need the programs to continue the execution no matter if the shell connection is lost, so I'm using the NOHUP command.

However, each program has its own LOG file, like follows.-

programA --> logfileA.txt
program1a --> logfile1a.txt
program1b --> logfile1b.txt
program1c --> logfile1c.txt

When an error occurs in one of the child programs, the error message is displayed in the LOG file corresponding to the main program.

Example.-

1) The programA is invoked from another routine.-

Code:
nohup nice -10 programA 2>&1 > logfileA.txt

2) Inside programA it invokes the other 3 programs.-

Code:
nohup nice -10 program1a 2>&1 > logfile1a.txt
nohup nice -10 program1b 2>&1 > logfile1b.txt
nohup nice -10 program1c 2>&1 > logfile1c.txt

When an error occurs, it is showed or displayed in the logfileA.txt, instead of being displayed in the logfile1a.txt or logfile1b.txt or logfile1c.txt

How can I know what program produced the error that it is displayed in the main program's LOG file?

Thanks in advance for your responses.

Last edited by radoulov; 04-03-2014 at 12:23 PM..
# 2  
Old 02-01-2013
Usually the log is merged for coherency, so stderr and stdout are logged contemporaneously in the same log, give or take some stdout buffering. Use redirection ">log_file 2>&1". You are making stderr become stdout and go to the outer log, and stdout goes to the three inner logs. Try that order on all your lines.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 02-01-2013
Question

Quote:
Originally Posted by DGPickett
Usually the log is merged for coherency, so stderr and stdout are logged contemporaneously in the same log, give or take some stdout buffering. Use redirection ">log_file 2>&1". You are making stderr become stdout and go to the outer log, and stdout goes to the three inner logs. Try that order on all your lines.
I attempted to use redirection ">log_file 2>&1" and it is still logging to the outer log, instead of looging to the three inner logs. Please help.
# 4  
Old 02-01-2013
Quote:
Originally Posted by enriquegm82
I attempted to use redirection ">log_file 2>&1" and it is still logging to the outer log, instead of looging to the three inner logs. Please help.
You have 4 commands that are using redirection:
Code:
nohup nice -10 programA 2>&1 > logfileA.txt
nohup nice -10 program1a 2>&1 > logfile1a.txt
nohup nice -10 program1b 2>&1 > logfile1b.txt
nohup nice -10 program1c 2>&1 > logfile1c.txt

You need to change the order of redirections on all four of them to:
Code:
nohup nice -10 programA > logfileA.txt 2>&1
nohup nice -10 program1a > logfile1a.txt 2>&1
nohup nice -10 program1b > logfile1b.txt 2>&1
nohup nice -10 program1c > logfile1c.txt 2>&1

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 02-02-2013
Question

Quote:
Originally Posted by Don Cragun
You have 4 commands that are using redirection:
Code:
nohup nice -10 programA 2>&1 > logfileA.txt
nohup nice -10 program1a 2>&1 > logfile1a.txt
nohup nice -10 program1b 2>&1 > logfile1b.txt
nohup nice -10 program1c 2>&1 > logfile1c.txt

You need to change the order of redirections on all four of them to:
Code:
nohup nice -10 programA > logfileA.txt 2>&1
nohup nice -10 program1a > logfile1a.txt 2>&1
nohup nice -10 program1b > logfile1b.txt 2>&1
nohup nice -10 program1c > logfile1c.txt 2>&1

Plz one question, why changing the order of redirection would make a difference? what is the meaning of "2>&1" ? I think that 2>&1 is telling that standard error becomes standard output and logges to the same outer log, meaning log to "logFileA.txt" instead of the 3 inner log files.

I changed the redirection in the four command lines and it is still logging the Error Message to the Outer Log (logFileA.txt). How could I make it to log the Error to the 3 inner log files?
# 6  
Old 02-02-2013
Quote:
Originally Posted by enriquegm82
Plz one question, why changing the order of redirection would make a difference? what is the meaning of "2>&1" ? I think that 2>&1 is telling that standard error becomes standard output and logges to the same outer log, meaning log to "logFileA.txt" instead of the 3 inner log files.
This discussion assumes that you are using a shell that uses basic Bourne shell redirection syntax (such as bash, ksh, or sh). The csh shell and its derivatives handle redirection differently.

The shell redirection operator 2>&1 tells the shell to redirect all ouput written to file descriptor 2 (the standard error output stream) to the file to which file descriptor 1 (the standard output stream) is directed. The order of the redirections is important. Try the following examples to see the difference.
EXAMPLE 1:
Code:
(echo abc;cp "unknown file" xyz)

This command will send the standard output from echo to your terminal and the standard error output to your terminal (unless you have performed redirections for one of these output streams that were not associated with another command).

If this isn't what happens, log out and log back in and try again to get a fresh shell execution environment with standard output and standard error output directed to your terminal.

EXAMPLE 2:
Code:
(echo abc;cp "unknown file" xyz) >fd1 2>fd2

This command will send the output from echo to the file named fd1 and the diagnostic message from cp to the file named fd2. You can verify this by running the commands:
Code:
cat fd1

and
Code:
cat fd2

EXAMPLE 3:
Code:
(echo abc;cp "unknown file" xyz) >fd1 2>&1

This command will send the output from echo to the file named fd1 and the diagnostic message from cp to the file to which file descriptor 1 is directed (which in this case is the file named fd1). Again, you can verify this by running the command:
Code:
cat fd1

EXAMPLE 4:
Code:
(echo abc;cp "unknown file" xyz) 2>&1 >fd1

This will send the diagnostic output from the cp command to the file to which file descriptor 1 is directed (your terminal) and the output from echo to the file named fd1.

Quote:
Originally Posted by enriquegm82
I changed the redirection in the four command lines and it is still logging the Error Message to the Outer Log (logFileA.txt). How could I make it to log the Error to the 3 inner log files?
If the commands:
Code:
nohup nice -10 program1a > logfile1a.txt 2>&1
nohup nice -10 program1b > logfile1b.txt 2>&1
nohup nice -10 program1c > logfile1c.txt 2>&1

send any output to logfileA.txt something is seriously wrong. This all works as I have described when tested on Mac OS X Version 10.7.5 with both bash and ksh. When I tried these using csh on OS X, it gives the diagnostic:
Code:
Ambiguous output redirect.

Of course there is one other possibility: Do program1a, program1b, and program1c internally redirect their output to logfileA.txt? If so, we have wasted a lot of time, and I assume you know how to fix your problem.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 02-04-2013
Wrench It worked!

Quote:
Originally Posted by Don Cragun
This discussion assumes that you are using a shell that uses basic Bourne shell redirection syntax (such as bash, ksh, or sh). The csh shell and its derivatives handle redirection differently.

The shell redirection operator 2>&1 tells the shell to redirect all ouput written to file descriptor 2 (the standard error output stream) to the file to which file descriptor 1 (the standard output stream) is directed. The order of the redirections is important. Try the following examples to see the difference.
EXAMPLE 1:
Code:
(echo abc;cp "unknown file" xyz)

This command will send the standard output from echo to your terminal and the standard error output to your terminal (unless you have performed redirections for one of these output streams that were not associated with another command).

If this isn't what happens, log out and log back in and try again to get a fresh shell execution environment with standard output and standard error output directed to your terminal.

EXAMPLE 2:
Code:
(echo abc;cp "unknown file" xyz) >fd1 2>fd2

This command will send the output from echo to the file named fd1 and the diagnostic message from cp to the file named fd2. You can verify this by running the commands:
Code:
cat fd1

and
Code:
cat fd2

EXAMPLE 3:
Code:
(echo abc;cp "unknown file" xyz) >fd1 2>&1

This command will send the output from echo to the file named fd1 and the diagnostic message from cp to the file to which file descriptor 1 is directed (which in this case is the file named fd1). Again, you can verify this by running the command:
Code:
cat fd1

EXAMPLE 4:
Code:
(echo abc;cp "unknown file" xyz) 2>&1 >fd1

This will send the diagnostic output from the cp command to the file to which file descriptor 1 is directed (your terminal) and the output from echo to the file named fd1.


If the commands:
Code:
nohup nice -10 program1a > logfile1a.txt 2>&1
nohup nice -10 program1b > logfile1b.txt 2>&1
nohup nice -10 program1c > logfile1c.txt 2>&1

send any output to logfileA.txt something is seriously wrong. This all works as I have described when tested on Mac OS X Version 10.7.5 with both bash and ksh. When I tried these using csh on OS X, it gives the diagnostic:
Code:
Ambiguous output redirect.

Of course there is one other possibility: Do program1a, program1b, and program1c internally redirect their output to logfileA.txt? If so, we have wasted a lot of time, and I assume you know how to fix your problem.
Don Cragun, thanks very much for the explanation. It was very clear and useful. It worked!

Thanks very much to Don Cragun and DGPickett.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Dot and redirected output syntax in a tar command

Hi All, Please could anyone advise what the purpose of the dot syntax in the following command means: tar -cvf ${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE} . >/${BACKUP_ROOT}/${ARCHIVE_LOG} Many thanks (2 Replies)
Discussion started by: daveu7
2 Replies

2. UNIX and Linux Applications

missing delimiters when mysql output is redirected to log file

Hi, Pls check that '|' and '+' present in Step-1 are not copied to log file in Step-3. Pls suggest how to get the exact output from Step-1 (i.e. with out losing '|' and '+') in to a log file ~Thanks Step-1: Execute command > mysql -utest -ptest -htesthost testdb -e "select * from... (3 Replies)
Discussion started by: newbielgn
3 Replies

3. UNIX for Dummies Questions & Answers

Make - two target produced by one recipe

Suppose executable X produces files A and B from nothing, Y produces C from A, Z produces D from B, and my final goal is to produce C and D. I wrote the following makefile: .PHONY: all all: C D C: A Y A D: B Z B A B: X This makefile seems to reflect all dependencies, as it should... (2 Replies)
Discussion started by: ybelenky
2 Replies

4. AIX

Nohup command

I'm trying to run a compress script in the background, but I even though the script is running in the background I get "Sending nohup output to nohup.out." and the screen just stays there. How can I make the script run in background and make my command prompt come back. I was using: nohup... (2 Replies)
Discussion started by: NycUnxer
2 Replies

5. Solaris

Error messgaes can not be redirected

# whoami 2>/dev/null whoami: not found # Why the error message not getting redirected to /dev/null ... The shell is # echo $SHELL /sbin/sh For other commands it is working # ls aaa 2>/dev/null # Is there any other way to redirect the err msg from whoami Thank you for your... (7 Replies)
Discussion started by: Anu_1
7 Replies

6. Programming

Want to get /var/log/messages redirected to a FIFO ...

Is it possible that I get all the syslog messages from /var/log/messages redirected to some FIFO as and when any new messages comes. In exact, I need to duplicate the messages to a FIFO, the moment a new message is logged; from where another process reads and does some processing without... (12 Replies)
Discussion started by: Praveen_218
12 Replies

7. UNIX for Dummies Questions & Answers

nohup command..

Hi.. Can anybody tell me, what exactly the nohup command does & when is it used? Your help is appreciated. (3 Replies)
Discussion started by: Amol21
3 Replies

8. UNIX for Dummies Questions & Answers

Ignoring files that are currently being produced

Hi! Letīs say I want copy dump-files to a location. These dump-files vary between 80 and 280MB and will be produced in about 1min or less. I have a cronjob which (not only) copies those. So how can I find out whether a file is currently produced or not? Because if my script works with these... (2 Replies)
Discussion started by: cypher82
2 Replies

9. UNIX for Dummies Questions & Answers

Nohup Command

Hello All, I am newbie to unix.I am trying to use NOHUP command in shell script it works fine with only "nohup test.sh 10 &" and "/opt/user/nohup test.sh 10 &" gives an error saying "ksh: /opt/user/nohup : not found". Can anybody please guide me to make it work. Thanks Blazix (9 Replies)
Discussion started by: blazix
9 Replies

10. Shell Programming and Scripting

tail -f a log file redirected into a new window?

Is this possible? I am attempting to display a new xterm window and tail -f the log file within that new window. I am currently working on a solaris 8 machine if that has any different meaning than the other platforms. As you can see, I am a newbie to this forum and to UNIX. Any help would be... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question