LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-28-2004, 08:40 AM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
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
 
Old 11-28-2004, 09:14 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
In what language are your scripts written ?
 
Old 11-28-2004, 09:35 AM   #3
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
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
 
Old 11-28-2004, 10:04 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
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).
 
Old 11-28-2004, 11:08 AM   #5
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
thanks for that. I guess that dup2 command duplicates the stderr stream and places the result in file

Last edited by nodger; 11-28-2004 at 11:10 AM.
 
Old 11-28-2004, 11:22 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to redirect text to a file to cat at later point. says file doesn't exist. dr_zayus69 Programming 1 10-02-2005 08:10 AM
Redirecting error messages to stderr and file in Bash lowpro2k3 Programming 1 04-14-2005 06:47 PM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM
Any one know how to redirect standard error (stderr) to a JetDirect printer? RickMean Linux - General 2 09-03-2004 11:33 AM
TCSH: redirect stdout and stderr seperately ugenn Linux - General 2 06-06-2002 12:07 PM


All times are GMT -5. The time now is 12:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration