Bear with me, as this is kind of convoluted.
In a Perl/Tk program I am developing, I have to spawn a child process to run in the background. (It is another, separate Perl/Tk program.) Its invocation is actually done by a shell script that checks some conditions and craps out if there are any problems. There is also the possibility that the other Perl/Tk program might fail to start. All of this needs to be logged and presented to the user in a window so they can inform our support staff about the problem.
After this point, we don't really care about the output of the child program. What we would prefer to do is just pass it through to the parent's (my) STDOUT/STDERR. Dumping it to /dev/null or closing the filehandles isn't really a viable option.
My observations about the problem:
- system() with a & is insufficiently capable for this because it offers no provision for capturing STDOUT/STDERR. There is also no way to know what the child pid is to see if it died (though using backticks on the backgrounded process's invocation and parsing the shell response could concievably work for this).
- If we fork and exec, we have to figure out how to deal with the file descriptors. Is there a way to redirect (with dup2?) an opened child's STDERR to our own without closing our STDERR? That is, merge the two? I don't know if this is possible.
- Assuming further that we will have to use some sort of repeater code, given that this is a Perl/Tk program, what is the easiest way to have it "automatically" echo the child's output to our own (after we have decided we don't care to watch it anymore)? I think fileevent is the way to go. Can that mechanism be used with eg. IPC::Run? I would like to minimize the amount of surgery I have to do to my application's guts to set up the repeaters, etc.
Apologies for the excessive length, and thanks in advance for any input.
---
"I hate it when I think myself into a corner."
Matt Mitchell
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.