Re: Backgrounding a stream, and sendmail?

From: Charles Sullivan (cwsulliv_at_triad.rr.com)
Date: 10/06/03

  • Next message: Mylinux: "gcc -c -g test5 -o test5 ..."
    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


  • Next message: Mylinux: "gcc -c -g test5 -o test5 ..."

    Relevant Pages

    • Re: Backgrounding a stream, and sendmail?
      ... > but that doesn't work at all, even if I just redirect ... > the output to a file instead of sendmail.) ... The shell won't restart prog_c for you, ... did was connecting stdout of prog_b to the stdin of prog_c ...
      (comp.os.linux.development.apps)
    • Re: Redirecting stdout of running program
      ... so that I can disconnect the shell and keep it running. ... I would need to redirect the stdout to a file before disowning it. ...
      (comp.os.linux.development.system)
    • Re: funneling a file to stdin from the command line
      ... line to imitate this? ... With stdout, I'll redirect so: ... of the Bourne shell. ... command line to redirect input. ...
      (comp.lang.c)
    • Re: how to redirect stderr,stdin to a file from shell
      ... Is there any way to redirect stderr,stdin to a file from shell ... If you want to redirect both error and stdout to a file then do the ...
      (comp.unix.programmer)
    • Old 4.2 user, with 6.2 newbie questions
      ... I have a shell account on my isp which runs 4.10-STABLE. ... I use my own sendmail to send mail out to various lists on ... retrieve mail on my isp via fetchmail. ...
      (freebsd-questions)