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.-

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

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

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.

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.

You have 4 commands that are using redirection:

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:

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?

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:

(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:

(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:

cat fd1

and

cat fd2

EXAMPLE 3:

(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:

cat fd1

EXAMPLE 4:

(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:

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:

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.