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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-28-2004, 08:40 AM
|
#1
|
|
Member
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192
Rep:
|
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
|
|
|
|
11-28-2004, 09:14 AM
|
#2
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
In what language are your scripts written ?
|
|
|
|
11-28-2004, 09:35 AM
|
#3
|
|
Member
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192
Original Poster
Rep:
|
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
|
|
|
|
11-28-2004, 10:04 AM
|
#4
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
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).
|
|
|
|
11-28-2004, 11:08 AM
|
#5
|
|
Member
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192
Original Poster
Rep:
|
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.
|
|
|
|
11-28-2004, 11:22 AM
|
#6
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
The error output stream is in fact redirected, not duplicated, as the previous stderr output is closed during the call.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:44 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|