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

Hi, I'm running a c program from IBM ( 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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

killdurstAuthor Commented:
You can download the codes from IBM at "".

The files in the tgz are...

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!
Gerwin Jansen, EE MVETopic Advisor Commented:
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:


and at the back:


So in total:

nohup /root/inotify-sample/inotify_test /home/temp/folderToWatch >> /path/to/outputfile 2>&1 &
killdurstAuthor Commented:
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!
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

killdurstAuthor Commented:
From the page at

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...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gerwin Jansen, EE MVETopic Advisor Commented:
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 ...
killdurstAuthor Commented:
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.
Gerwin Jansen, EE MVETopic Advisor Commented:
>> 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
killdurstAuthor Commented:
Adding fflush helped solved the issue. Thanks to everyone who participated!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.