Re: Backgrounding a stream, and sendmail?
From: Charles Sullivan (cwsulliv_at_triad.rr.com)
Date: 10/06/03
- Previous message: Josef Möllers: "Re: How to check whether 2 ttys are the same one"
- In reply to: Jens.Toerring_at_physik.fu-berlin.de: "Re: Backgrounding a stream, and sendmail?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 06 Oct 2003 08:53:29 GMT
On Sat, 04 Oct 2003 17:52:07 +0000, Jens.Toerrin wrote:
> Charles Sullivan <cwsulliv@triad.rr.com> wrote:
>> I have a process that displays info about external events to an xterm.
>> I thought to write a filter to analyze the output of this process and
>> send me emails under particular conditions, and have the whole thing run
>> in the background.
>
>> This much works fine and displays the appropriate message on the xterm:
>> $ process | myfilter
>
>> This much only works partially, because sendmail exits after (correctly)
>> sending the first email: $ process | myfilter | sendmail cwsulliv (and
>> it's not clear to me from the man page how to make it remain active.)
>
>> (I originally naively thought I could run the following: $ nohup process
>> | myfilter | sendmail cwsulliv & but that doesn't work at all, even if I
>> just redirect the output to a file instead of sendmail.)
>
>> My filter is written in C with code like this fragment:
>> ------------------
>> while (1) {
>> if ( fgets(buffer, buff_size, stdin) == NULL ) {
>> sleep(1);
>> continue;
>> }
>> if ( my_analysis(buffer, ...) == COMPLETE ) {
>> printf( <message> );
>> }
>> }
>> ------------------
>
>> What am I doing wrong? Or where can I find out how to make something
>> like this work?
>
> You're not doing anything wrong as far as I can see, but when you have a
> shell command like
>
>> prog_a | prog_b | prog_c
>
> and prog_c exits prog_b hasn't left anything left to write to anymore. The
> shell won't restart prog_c for you, all it did was connecting stdout of
> prog_b to the stdin of prog_c and then starting both of them without
> having much control over them anymore. The shell does the initial plumbing
> for you but it doesn't check if pipes break and then tries to "repair"
> them - this would be impossible because the shell does not know what you
> would like it to do. Also, when prog_b has it's stdout redirected nothing
> external can change this redirection and when prog_c is dead it would have
> to actively change what it's stdout is redirected to.
>
> I guess the simplest solution would be to have your program emulate what
> the shell is doing but under control of your program: When my_analysis()
> is done instead of printing to stdout you could start sendmail from within
> your program and pass it the buffer. The simplest way to do so would
> probably be something along the lines of (error checking omitted)
>
> FILE *sm;
>
> if ( my_analysis(buffer, ...) == COMPLETE ) {
> sm = popen( "/usr/bin/sendmail cwsulliv", "w" ); fprintf( sm,
> <massage> );
> pclose( sm );
> }
> }
> This will start a new instance of sendmail each time you have a new
> message, with popen() giving you a file pointer you use to send the
> message to sendmail.
>
> Regards, Jens
Thanks Jens. That ought to work, at least in the foreground.
Regards,
Charles
- Previous message: Josef Möllers: "Re: How to check whether 2 ttys are the same one"
- In reply to: Jens.Toerring_at_physik.fu-berlin.de: "Re: Backgrounding a stream, and sendmail?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|