MeloMCR
MeloMCR

Reputation: 412

How can I redirect to a file a system error from running an executable without a needed library?

I am trying to put into a file an error that the system gives me. I have the following script:

#!/bin/bash

logfile="output.log"

echo "Starting" > $logfile

./cpi 2>&1 >> $logfile

echo "Ending" >> $logfile

exit

And the output I get in the file output.log is the following:

Starting
Ending

But in the screen I see this:

./cpi: error while loading shared libraries: libmpich.so.3: cannot open shared object file: No such file or directory

The error is on purpose. I took the path to that lib out of my #LD_LIBRARY_PATH. The point is, if the script runs in another machine and the library isn't there, I would like to get the error that goes to the screen.

I also need the output of my own program, thus I need the 2>&1 >> $logfile after its execution.

Any thoughts?

Upvotes: 2

Views: 139

Answers (2)

chepner
chepner

Reputation: 531075

I may be misunderstanding.

If you want both the standard output and standard error from cpi to go to $logfile, swap the order of the redirections:

./cpi >> $logfile 2>&1

Upvotes: 2

perreal
perreal

Reputation: 97948

This error is reported by the loader and not the cpi binary so the redirection does not work here. You can test this by generating a cpi internal error. You might be able to log this error if you use a wrapper script to launch cpi.

Upvotes: 0

Related Questions