View Full Version : Best way to pop a shell to display output?

06-02-2015, 03:38 AM
I have a C program that produces output, representing its status.
Up until now, I used to run it from the command line, so, every output that was written to the standard output (or standard error) just showed up on the shell from which I run it from.
From example:

$ ./prog
time_stamp: Program execution starts.
time_stamp: System is up.
time_stamp: Waiting for requests...

But now, prog is going to be initiated by some other process (who won't be running through the shell either), and not manually by me from the shell.
So, every output it might produce will not be visible.
Now, I want prog to pop a shell whenever it is initiated, in order to have its output displayed on it.

The best way I could think of doing that is:
1. fork() a new process (call it 'child') from prog
2. let child choose one of the shells from /etc/shells
3. mount the chosen shell to child using exec()
4. redirect standard output and standard error from prog to child

This all seems like a lot of work, and I just want to be sure this is the best way to do it, and that I'm not missing out on some better, faster, magical way to shoot output to a shell.
Can anyone offer a better alternative or offer some suggestions/advices?

06-02-2015, 01:21 PM
OK, I guess what I meant is "terminal" not "shell"...
I just want the output to be printed to a terminal window.

06-05-2015, 10:47 AM
The easiest choice is to save errors into a file. There is also the possibility to redirect stderr into a file with:

freopen("error.txt", "a", stderr);

I'm not sure that your solution works correctly, because I'm not sure that a shell is opened like a new window when you do exec...

06-05-2015, 11:48 AM
Thanks for the help, zedd, but as I suspected, there was a much easier solution.
I just wrote a new program, call it launcher, and the only thing it does is:


int main(int argc, char* argv[]) {

execlp("gnome-terminal","gnome-terminal","--command","path_to_prog", NULL);
return -1;

That's it... :D

Now instead of running prog, I'm running launcher...

06-05-2015, 03:10 PM
Interesting solution :)

If you want that some code is executed after execlp you probably can use directly the system call system:
system ("gnome-terminal --command path_to_prog");

06-05-2015, 06:24 PM
Now instead of running prog, I'm running launcher...



If you can change the source for calling application, why not just forward the details to the "prog" application?