LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how do i redirect stderr to a file? (https://www.linuxquestions.org/questions/programming-9/how-do-i-redirect-stderr-to-a-file-260135/)

nodger 11-28-2004 08:40 AM

how do i redirect stderr to a file?
 
This is what I want to do: I have some cgi scripts and I want to debug them by redirecting the standard error to a file.

I know this much: the cgi script is a child process of apache, and each time it runs, apache hands it several enviornment variables (HTTP_HOST, etc), do I have to change the enviornment it runs in or what? Apache can obviously redirect stdout to a socket, so Im sure I can similarly redirect stderr to a file

jlliagre 11-28-2004 09:14 AM

In what language are your scripts written ?

nodger 11-28-2004 09:35 AM

theyre written in c. I know I could just use a errorlog file but some of the libraries Im linking in use stderr so its cleaner in my opinion to just spit everything out to stderr

jlliagre 11-28-2004 10:04 AM

You can add sth like this at the beginning of your code:
Code:

#include <fcntl.h>
#include <stdio.h>
..
  char *logFile="/tmp/mycgi.err";
  int fd;
  if((fd=open(logFile,O_CREAT|O_APPEND|O_WRONLY,0644))!=-1)
  {
    dup2(fd,2);
  }
  else
  {
    perror(logFile);
  }

All stderr output should then go to the logFile.

This is not very robust though if there is a very high load on your cgi (risk of mixed output from concurrent executions).

nodger 11-28-2004 11:08 AM

thanks for that. I guess that dup2 command duplicates the stderr stream and places the result in file

jlliagre 11-28-2004 11:22 AM

The error output stream is in fact redirected, not duplicated, as the previous stderr output is closed during the call.


All times are GMT -5. The time now is 01:27 PM.