Link to home
Start Free TrialLog in
Avatar of killdurst
killdurst

asked on

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 (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!
Avatar of killdurst
killdurst

ASKER

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!
SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Adding fflush helped solved the issue. Thanks to everyone who participated!