In c program, how to use Linux tee to redirect the output of itself [duplicate]How can I implement 'tee' programmatically in C?How do I prompt for Yes/No/Cancel input in a Linux shell script?In Linux, how to prevent a background process from being stopped after closing SSH clientHow to redirect output to a file and stdoutHow can I redirect and append both stdout and stderr to a file with Bash?Improve INSERT-per-second performance of SQLite?How to symlink a file in Linux?How do I change permissions for a folder and all of its subfolders and files in one step in Linux?How to change the output color of echo in LinuxPiping command output to tee but also save exit code of commandHow do I find all files containing specific text on Linux?

Review your own paper in Mathematics

Does Doodling or Improvising on the Piano Have Any Benefits?

Giving feedback to someone without sounding prejudiced

Given this phrasing in the lease, when should I pay my rent?

What does "tick" mean in this sentence?

Identifying "long and narrow" polygons in with PostGIS

In One Punch Man, is King actually weak?

Is there a RAID 0 Equivalent for RAM?

Why didn’t Eve recognize the little cockroach as a living organism?

Animation: customize bounce interpolation

Alignment of six matrices

How to preserve electronics (computers, iPads and phones) for hundreds of years

Origin of pigs as a species

Do you waste sorcery points if you try to apply metamagic to a spell from a scroll but fail to cast it?

What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?

Confusion over Hunter with Crossbow Expert and Giant Killer

What should be the ideal length of sentences in a blog post for ease of reading?

Is there anyway, I can have two passwords for my wi-fi

Isometric embedding of a genus g surface

How do I fix the group tension caused by my character stealing and possibly killing without provocation?

How to test the sharpness of a knife?

Is there a distance limit for minecart tracks?

Echo with obfuscation

Can I run 125khz RF circuit on a breadboard?



In c program, how to use Linux tee to redirect the output of itself [duplicate]


How can I implement 'tee' programmatically in C?How do I prompt for Yes/No/Cancel input in a Linux shell script?In Linux, how to prevent a background process from being stopped after closing SSH clientHow to redirect output to a file and stdoutHow can I redirect and append both stdout and stderr to a file with Bash?Improve INSERT-per-second performance of SQLite?How to symlink a file in Linux?How do I change permissions for a folder and all of its subfolders and files in one step in Linux?How to change the output color of echo in LinuxPiping command output to tee but also save exit code of commandHow do I find all files containing specific text on Linux?













0
















This question already has an answer here:



  • How can I implement 'tee' programmatically in C?

    5 answers



I can't use the Linux command tee at the time the C program gets executed since I can't find which line of the code in a big project precisely that runs this program. I am struggling to come up with a way to call the tee command inside the C program to redirect its printf statement to a log file also. The problem is that I can't call tee without an executable like "./exe | tee some.log" since the program is already executed. I did some research on how to get the stdout of a running process and saw some answers suggesting to check out /proc//fd/1. But I don't know why "1"(stdout) file is empty, and I was expecting the file should store the printf output of the process.



Sincerely appreciate for any help!










share|improve this question













marked as duplicate by jww, Community Mar 8 at 15:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Using command-line tools within C programs is sort of missing the point of C. There's easy ways to write a logging function that outputs to one or more filehandles, like stdout and/or a file.

    – tadman
    Mar 7 at 21:54






  • 1





    ..since the program is already executed. - what? Then how do you intend to "call" tee from within the program which is already executed?

    – Eugene Sh.
    Mar 7 at 21:54











  • @EugeneSh. You can call tee using the system().

    – Ian Huang
    Mar 7 at 22:01











  • @tadman If I understand correctly, are you suggesting to for example customize a my_printf()? If so, it wouldn't work for me since then I will need to change every printf in the project, and it is just undoable...

    – Ian Huang
    Mar 7 at 22:04











  • EugeneSh is saying that if the program is already running, then making changes to it won't affect the running process; you'd need to recompile and start the process again. And if you're restarting it anyway, you can just pipe it to tee instead of modifying the source.

    – Ray
    Mar 7 at 22:10
















0
















This question already has an answer here:



  • How can I implement 'tee' programmatically in C?

    5 answers



I can't use the Linux command tee at the time the C program gets executed since I can't find which line of the code in a big project precisely that runs this program. I am struggling to come up with a way to call the tee command inside the C program to redirect its printf statement to a log file also. The problem is that I can't call tee without an executable like "./exe | tee some.log" since the program is already executed. I did some research on how to get the stdout of a running process and saw some answers suggesting to check out /proc//fd/1. But I don't know why "1"(stdout) file is empty, and I was expecting the file should store the printf output of the process.



Sincerely appreciate for any help!










share|improve this question













marked as duplicate by jww, Community Mar 8 at 15:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Using command-line tools within C programs is sort of missing the point of C. There's easy ways to write a logging function that outputs to one or more filehandles, like stdout and/or a file.

    – tadman
    Mar 7 at 21:54






  • 1





    ..since the program is already executed. - what? Then how do you intend to "call" tee from within the program which is already executed?

    – Eugene Sh.
    Mar 7 at 21:54











  • @EugeneSh. You can call tee using the system().

    – Ian Huang
    Mar 7 at 22:01











  • @tadman If I understand correctly, are you suggesting to for example customize a my_printf()? If so, it wouldn't work for me since then I will need to change every printf in the project, and it is just undoable...

    – Ian Huang
    Mar 7 at 22:04











  • EugeneSh is saying that if the program is already running, then making changes to it won't affect the running process; you'd need to recompile and start the process again. And if you're restarting it anyway, you can just pipe it to tee instead of modifying the source.

    – Ray
    Mar 7 at 22:10














0












0








0









This question already has an answer here:



  • How can I implement 'tee' programmatically in C?

    5 answers



I can't use the Linux command tee at the time the C program gets executed since I can't find which line of the code in a big project precisely that runs this program. I am struggling to come up with a way to call the tee command inside the C program to redirect its printf statement to a log file also. The problem is that I can't call tee without an executable like "./exe | tee some.log" since the program is already executed. I did some research on how to get the stdout of a running process and saw some answers suggesting to check out /proc//fd/1. But I don't know why "1"(stdout) file is empty, and I was expecting the file should store the printf output of the process.



Sincerely appreciate for any help!










share|improve this question















This question already has an answer here:



  • How can I implement 'tee' programmatically in C?

    5 answers



I can't use the Linux command tee at the time the C program gets executed since I can't find which line of the code in a big project precisely that runs this program. I am struggling to come up with a way to call the tee command inside the C program to redirect its printf statement to a log file also. The problem is that I can't call tee without an executable like "./exe | tee some.log" since the program is already executed. I did some research on how to get the stdout of a running process and saw some answers suggesting to check out /proc//fd/1. But I don't know why "1"(stdout) file is empty, and I was expecting the file should store the printf output of the process.



Sincerely appreciate for any help!





This question already has an answer here:



  • How can I implement 'tee' programmatically in C?

    5 answers







c linux tee






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 21:51









Ian HuangIan Huang

32




32




marked as duplicate by jww, Community Mar 8 at 15:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by jww, Community Mar 8 at 15:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Using command-line tools within C programs is sort of missing the point of C. There's easy ways to write a logging function that outputs to one or more filehandles, like stdout and/or a file.

    – tadman
    Mar 7 at 21:54






  • 1





    ..since the program is already executed. - what? Then how do you intend to "call" tee from within the program which is already executed?

    – Eugene Sh.
    Mar 7 at 21:54











  • @EugeneSh. You can call tee using the system().

    – Ian Huang
    Mar 7 at 22:01











  • @tadman If I understand correctly, are you suggesting to for example customize a my_printf()? If so, it wouldn't work for me since then I will need to change every printf in the project, and it is just undoable...

    – Ian Huang
    Mar 7 at 22:04











  • EugeneSh is saying that if the program is already running, then making changes to it won't affect the running process; you'd need to recompile and start the process again. And if you're restarting it anyway, you can just pipe it to tee instead of modifying the source.

    – Ray
    Mar 7 at 22:10


















  • Using command-line tools within C programs is sort of missing the point of C. There's easy ways to write a logging function that outputs to one or more filehandles, like stdout and/or a file.

    – tadman
    Mar 7 at 21:54






  • 1





    ..since the program is already executed. - what? Then how do you intend to "call" tee from within the program which is already executed?

    – Eugene Sh.
    Mar 7 at 21:54











  • @EugeneSh. You can call tee using the system().

    – Ian Huang
    Mar 7 at 22:01











  • @tadman If I understand correctly, are you suggesting to for example customize a my_printf()? If so, it wouldn't work for me since then I will need to change every printf in the project, and it is just undoable...

    – Ian Huang
    Mar 7 at 22:04











  • EugeneSh is saying that if the program is already running, then making changes to it won't affect the running process; you'd need to recompile and start the process again. And if you're restarting it anyway, you can just pipe it to tee instead of modifying the source.

    – Ray
    Mar 7 at 22:10

















Using command-line tools within C programs is sort of missing the point of C. There's easy ways to write a logging function that outputs to one or more filehandles, like stdout and/or a file.

– tadman
Mar 7 at 21:54





Using command-line tools within C programs is sort of missing the point of C. There's easy ways to write a logging function that outputs to one or more filehandles, like stdout and/or a file.

– tadman
Mar 7 at 21:54




1




1





..since the program is already executed. - what? Then how do you intend to "call" tee from within the program which is already executed?

– Eugene Sh.
Mar 7 at 21:54





..since the program is already executed. - what? Then how do you intend to "call" tee from within the program which is already executed?

– Eugene Sh.
Mar 7 at 21:54













@EugeneSh. You can call tee using the system().

– Ian Huang
Mar 7 at 22:01





@EugeneSh. You can call tee using the system().

– Ian Huang
Mar 7 at 22:01













@tadman If I understand correctly, are you suggesting to for example customize a my_printf()? If so, it wouldn't work for me since then I will need to change every printf in the project, and it is just undoable...

– Ian Huang
Mar 7 at 22:04





@tadman If I understand correctly, are you suggesting to for example customize a my_printf()? If so, it wouldn't work for me since then I will need to change every printf in the project, and it is just undoable...

– Ian Huang
Mar 7 at 22:04













EugeneSh is saying that if the program is already running, then making changes to it won't affect the running process; you'd need to recompile and start the process again. And if you're restarting it anyway, you can just pipe it to tee instead of modifying the source.

– Ray
Mar 7 at 22:10






EugeneSh is saying that if the program is already running, then making changes to it won't affect the running process; you'd need to recompile and start the process again. And if you're restarting it anyway, you can just pipe it to tee instead of modifying the source.

– Ray
Mar 7 at 22:10













1 Answer
1






active

oldest

votes


















2














A shell sets up a pipeline using pipe and dup2 before invoking the program. There's no reason why you couldn't do it after:



void pipeto(char** cmd) 
int fds[2];
pipe(fds);
if(fork())
// Set up stdout as the write end of the pipe
dup2(fds[1], 1);
close(fds[0]);
close(fds[1]);
else
// Double fork to not be a direct child
if(fork()) exit(0);
// Set up stdin as the read end of the pipe, and run tee
dup2(fds[0], 0);
close(fds[0]);
close(fds[1]);
execvp(cmd[0], cmd);



int main()
char* cmd[] = "tee", "myfile.txt", NULL ;
pipeto(cmd);
printf("This is a testn");



Result:



$ ./foo
This is a test

$ cat myfile.txt
This is a test


Note that since the shell is no longer aware that there's a tee being piped to, it will not wait for it. This means that output can get a bit jumbled if the program exits and the shell redraws the prompt before tee has finished writing. Output like this is purely a cosmetic problem:



user@host $ ./foo
user@host $ This is a test


It does not mean that the command is hanging, the prompt still works and you can type commands or just hit Enter to redraw.






share|improve this answer























  • This seems like exactly what I am looking for, thank you! But please allow me some time to take a look at it, and I will come back and accept it at latest tomorrow! Anyway, thank you again for your answer!

    – Ian Huang
    Mar 7 at 22:25











  • You can avoid the premature exiting problem by having the program wait for the child before it exits...

    – Chris Dodd
    Mar 8 at 0:25











  • @ChrisDodd Yes, though that assumes having an extra child does not interfere with the program. I conservatively assumed it would and double-forked

    – that other guy
    Mar 8 at 3:17

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














A shell sets up a pipeline using pipe and dup2 before invoking the program. There's no reason why you couldn't do it after:



void pipeto(char** cmd) 
int fds[2];
pipe(fds);
if(fork())
// Set up stdout as the write end of the pipe
dup2(fds[1], 1);
close(fds[0]);
close(fds[1]);
else
// Double fork to not be a direct child
if(fork()) exit(0);
// Set up stdin as the read end of the pipe, and run tee
dup2(fds[0], 0);
close(fds[0]);
close(fds[1]);
execvp(cmd[0], cmd);



int main()
char* cmd[] = "tee", "myfile.txt", NULL ;
pipeto(cmd);
printf("This is a testn");



Result:



$ ./foo
This is a test

$ cat myfile.txt
This is a test


Note that since the shell is no longer aware that there's a tee being piped to, it will not wait for it. This means that output can get a bit jumbled if the program exits and the shell redraws the prompt before tee has finished writing. Output like this is purely a cosmetic problem:



user@host $ ./foo
user@host $ This is a test


It does not mean that the command is hanging, the prompt still works and you can type commands or just hit Enter to redraw.






share|improve this answer























  • This seems like exactly what I am looking for, thank you! But please allow me some time to take a look at it, and I will come back and accept it at latest tomorrow! Anyway, thank you again for your answer!

    – Ian Huang
    Mar 7 at 22:25











  • You can avoid the premature exiting problem by having the program wait for the child before it exits...

    – Chris Dodd
    Mar 8 at 0:25











  • @ChrisDodd Yes, though that assumes having an extra child does not interfere with the program. I conservatively assumed it would and double-forked

    – that other guy
    Mar 8 at 3:17















2














A shell sets up a pipeline using pipe and dup2 before invoking the program. There's no reason why you couldn't do it after:



void pipeto(char** cmd) 
int fds[2];
pipe(fds);
if(fork())
// Set up stdout as the write end of the pipe
dup2(fds[1], 1);
close(fds[0]);
close(fds[1]);
else
// Double fork to not be a direct child
if(fork()) exit(0);
// Set up stdin as the read end of the pipe, and run tee
dup2(fds[0], 0);
close(fds[0]);
close(fds[1]);
execvp(cmd[0], cmd);



int main()
char* cmd[] = "tee", "myfile.txt", NULL ;
pipeto(cmd);
printf("This is a testn");



Result:



$ ./foo
This is a test

$ cat myfile.txt
This is a test


Note that since the shell is no longer aware that there's a tee being piped to, it will not wait for it. This means that output can get a bit jumbled if the program exits and the shell redraws the prompt before tee has finished writing. Output like this is purely a cosmetic problem:



user@host $ ./foo
user@host $ This is a test


It does not mean that the command is hanging, the prompt still works and you can type commands or just hit Enter to redraw.






share|improve this answer























  • This seems like exactly what I am looking for, thank you! But please allow me some time to take a look at it, and I will come back and accept it at latest tomorrow! Anyway, thank you again for your answer!

    – Ian Huang
    Mar 7 at 22:25











  • You can avoid the premature exiting problem by having the program wait for the child before it exits...

    – Chris Dodd
    Mar 8 at 0:25











  • @ChrisDodd Yes, though that assumes having an extra child does not interfere with the program. I conservatively assumed it would and double-forked

    – that other guy
    Mar 8 at 3:17













2












2








2







A shell sets up a pipeline using pipe and dup2 before invoking the program. There's no reason why you couldn't do it after:



void pipeto(char** cmd) 
int fds[2];
pipe(fds);
if(fork())
// Set up stdout as the write end of the pipe
dup2(fds[1], 1);
close(fds[0]);
close(fds[1]);
else
// Double fork to not be a direct child
if(fork()) exit(0);
// Set up stdin as the read end of the pipe, and run tee
dup2(fds[0], 0);
close(fds[0]);
close(fds[1]);
execvp(cmd[0], cmd);



int main()
char* cmd[] = "tee", "myfile.txt", NULL ;
pipeto(cmd);
printf("This is a testn");



Result:



$ ./foo
This is a test

$ cat myfile.txt
This is a test


Note that since the shell is no longer aware that there's a tee being piped to, it will not wait for it. This means that output can get a bit jumbled if the program exits and the shell redraws the prompt before tee has finished writing. Output like this is purely a cosmetic problem:



user@host $ ./foo
user@host $ This is a test


It does not mean that the command is hanging, the prompt still works and you can type commands or just hit Enter to redraw.






share|improve this answer













A shell sets up a pipeline using pipe and dup2 before invoking the program. There's no reason why you couldn't do it after:



void pipeto(char** cmd) 
int fds[2];
pipe(fds);
if(fork())
// Set up stdout as the write end of the pipe
dup2(fds[1], 1);
close(fds[0]);
close(fds[1]);
else
// Double fork to not be a direct child
if(fork()) exit(0);
// Set up stdin as the read end of the pipe, and run tee
dup2(fds[0], 0);
close(fds[0]);
close(fds[1]);
execvp(cmd[0], cmd);



int main()
char* cmd[] = "tee", "myfile.txt", NULL ;
pipeto(cmd);
printf("This is a testn");



Result:



$ ./foo
This is a test

$ cat myfile.txt
This is a test


Note that since the shell is no longer aware that there's a tee being piped to, it will not wait for it. This means that output can get a bit jumbled if the program exits and the shell redraws the prompt before tee has finished writing. Output like this is purely a cosmetic problem:



user@host $ ./foo
user@host $ This is a test


It does not mean that the command is hanging, the prompt still works and you can type commands or just hit Enter to redraw.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 22:08









that other guythat other guy

74.5k886124




74.5k886124












  • This seems like exactly what I am looking for, thank you! But please allow me some time to take a look at it, and I will come back and accept it at latest tomorrow! Anyway, thank you again for your answer!

    – Ian Huang
    Mar 7 at 22:25











  • You can avoid the premature exiting problem by having the program wait for the child before it exits...

    – Chris Dodd
    Mar 8 at 0:25











  • @ChrisDodd Yes, though that assumes having an extra child does not interfere with the program. I conservatively assumed it would and double-forked

    – that other guy
    Mar 8 at 3:17

















  • This seems like exactly what I am looking for, thank you! But please allow me some time to take a look at it, and I will come back and accept it at latest tomorrow! Anyway, thank you again for your answer!

    – Ian Huang
    Mar 7 at 22:25











  • You can avoid the premature exiting problem by having the program wait for the child before it exits...

    – Chris Dodd
    Mar 8 at 0:25











  • @ChrisDodd Yes, though that assumes having an extra child does not interfere with the program. I conservatively assumed it would and double-forked

    – that other guy
    Mar 8 at 3:17
















This seems like exactly what I am looking for, thank you! But please allow me some time to take a look at it, and I will come back and accept it at latest tomorrow! Anyway, thank you again for your answer!

– Ian Huang
Mar 7 at 22:25





This seems like exactly what I am looking for, thank you! But please allow me some time to take a look at it, and I will come back and accept it at latest tomorrow! Anyway, thank you again for your answer!

– Ian Huang
Mar 7 at 22:25













You can avoid the premature exiting problem by having the program wait for the child before it exits...

– Chris Dodd
Mar 8 at 0:25





You can avoid the premature exiting problem by having the program wait for the child before it exits...

– Chris Dodd
Mar 8 at 0:25













@ChrisDodd Yes, though that assumes having an extra child does not interfere with the program. I conservatively assumed it would and double-forked

– that other guy
Mar 8 at 3:17





@ChrisDodd Yes, though that assumes having an extra child does not interfere with the program. I conservatively assumed it would and double-forked

– that other guy
Mar 8 at 3:17





Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page