Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running C programs in the Linux shell 2

Status
Not open for further replies.

WebDrake

Programmer
Sep 29, 2005
106
0
0
PL
I have a C program which basically does the following:

(1) Prints a load of questions and scanf()'s responses

(2) Runs a simulation based upon the input, and prints out numbers on the screen (1, 2, 3, ...) using printf() to let me know how it's progressing.

What I often do is suspend the job after it's started running, and send it into the background: Ctrl-z, followed by typing bg. Of course, the printf() command continues to print the numbers, but if I wish I can log out of the shell and the program will keep going (useful since it's usually on a remote machine).

If I log out, of course, I can't keep track of the program's progress. So I was wondering: is there any Linux shell command that would enable me to select a running job and say, "OK, from now on any printing to the shell goes instead to this file"?

Basically I want the effect of,

./program > outputfile

... but to be able to apply the effect of > outputfile *after* the program has started, not from the beginning.

Is this possible?

Thanks in advance! :)
 
Redirect all output to a log file and then use "tail -f logfile" when you want ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
OK---how do I redirect the output to a log file? :)
 
I was given the following solution on the Ubuntu message boards: to use the tee command, as in,

./myprogram | tee outputfile.txt

The only minor modification necessary to the C code was that fflush(stdout) commands had to be inserted in some places to make sure that certain stuff printed when it was supposed to, e.g. if it preceded a scanf() command.
 
The [tt]tee[/tt] command will keep sending output to the screen, like you don't want it to do.

To redirect output to a log file so you can check it at any time...
Code:
nohup myprogram > myprogram.log 2>&1 &
That will run it in the background with all output (stdout and stderr) going to the log file.

To check and watch the log file while it runs...
Code:
tail -f myprogram.log
That will "follow" the log as it's written so you can watch it as it goes. Just Ctrl-C to stop watching. When your program's done, you've got a complete log of what happened while it ran.

Hope this helps.
 
Au contraire, tee does exactly what I want---which is to maintain screen printout while the shell is open, but to have a file there receiving the output so that if I exit the shell, I still have something keeping track of the program's progress.

Thanks however for the interesting suggestion, I'll check it out since I'm sure it will also be useful.
 
OK, I must have misread your post. I thought you didn't want to keep getting screen output when you put it in the background.

The command I gave starts the program in the background from the start, but you can check and follow the output whenever you want. Just a different way.

If the tee alone works for you, I say keep it simple!
 
Naa, my post was badly written in that respect. It gave the impression that after going to the background I didn't want any more output printed to screen, but actually I don't care. Tracking that output even after exiting the shell is the really important bit.

Thanks very much! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top