?
Solved

How to run a c program (that accepts an argument) in the background and saves all printfs to a file?

Posted on 2014-08-07
9
Medium Priority
?
378 Views
Last Modified: 2014-08-20
Hi, I'm running a c program from IBM (http://www.ibm.com/developerworks/linux/library/l-inotify/index.html) on my Linux machine to monitor a folder.

The command I ran to monitor a folder is...
/root/inotify-sample/inotify_test /home/temp/folderToWatch

Open in new window

In a second console, any changes I make to "/home/temp/folderToWatch" would result in some output in the first console. (From all the printfs in the c program.)

So now I want to save all of the output to a file. I tried to execute the following command...
/root/inotify-sample/inotify_test /home/temp/folderToWatch > /root/watchedFolders/log.txt

Open in new window

But after making changes to the "folderToWatch" folder (add/modify/delete files), there is no output in both the console and "log.txt".

How to save all of the printf output from the c program into a file?

I would also like to run this program in the background, but lets solve the logging part first.

Thanks!
0
Comment
Question by:killdurst
  • 5
  • 3
8 Comments
 
LVL 1

Author Comment

by:killdurst
ID: 40248086
You can download the codes from IBM at "http://www.ibm.com/developerworks/linux/library/l-inotify/index.html#download".

The files in the tgz are...
event_queue.c
inotify_test.c
inotify_utils.c
Makefile
README
event_queue.h
inotify_utils.h

On my Linux, I executed "make -f Makefile" to make the executable "inotify_test".

The printfs would appear correctly if I were to run...

/root/inotify-sample/inotify_test /home/temp/folderToWatch

Just let me know if you require more information. If you can download and test the program on your Linux, that'd be great! Thanks!
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 1500 total points
ID: 40248706
Try redirecting output like this:

>> /path/to/outputfile 2>&1

This will add stdout and error output to the file.

To run in the background, add in front:

nohup

and at the back:

&

So in total:

nohup /root/inotify-sample/inotify_test /home/temp/folderToWatch >> /path/to/outputfile 2>&1 &
0
 
LVL 1

Author Comment

by:killdurst
ID: 40254795
Hi, I tried the following command...

nohup /root/inotify-sample/inotify_test /home/temp/folderToWatch >> /root/watchedFolders/log.txt 2>&1 &

"cat /root/watchedFolders/log.txt" produces the following...

nohup: ignoring input

When I create/modify/delete files in "folderToWatch", "log.txt" is not updated with the printfs from the c program. :(

Do let me know if there's an error or something in the command I ran... Thanks!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
killdurst earned 0 total points
ID: 40254875
From the page at stackoverflow.com/questions/5500515/on-the-fly-output-redirection-seeing-the-file-redirection-output-while-the-prog...

It seems that adding "fflush(stdout);" after my printf statements in my c program will make the logging work... I'm gonna run more tests now...
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 1500 total points
ID: 40255057
When your program is not sending output to stdout then redirection will not work.

Did you get output on screen before making the fflush change? On screen would mean stdout ...
0
 
LVL 1

Author Comment

by:killdurst
ID: 40255093
Yup, I did get output on screen before making the fflush change. That's why it's so weird that I couldn't save the output to a log file...

Not sure if it matters, but this C program is a program that will run continuously and will exit only when you kill the process.
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 1500 total points
ID: 40256328
>> Not sure if it matters, but this C program is a program that will run continuously and will exit only when you kill the process.
This is no issue, a lot of programs run like that (services / daemons etc.)

Can you try and redirect stdout and stderr separately:

>> /path/to/outputfile 2 >> /path/to/errorfile

Or send ouput to a file as well as to the screen:

2>&1 | tee /path/to/outputfile
0
 
LVL 1

Author Closing Comment

by:killdurst
ID: 40272111
Adding fflush helped solved the issue. Thanks to everyone who participated!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question