More elegant implementation than this?



Under the bourne shell on Solaris 8 I have a shell script that starts
a java command with stdout and stderr redirected to a logfile and the
pid saved to another file. Like this:

$java_command >> $logdir/output.log 2>&1 &
echo $! > $logdir/command.pid


I need to switch to piping stdout and stderr to another program that
writes it to the logfile and rolls the logfile when it gets too large.
I tried the simple and obvious approach of just changing the redirection:

$java_command | $log_command $logdir/output.log &
echo $! > $logdir/command.pid

or

(exec 2>&1; exec $java_command) | $log_command $logdir/output.log &
echo $! > $logdir/command.pid

However the pid saves is the one from $log_command rather than from
$java_command.

After some more experimenting, I've come up with the following.
It works, but it seems a bit clunky. Have I overlooked a more
elegant way? (at the moment I can't change to a different shell)

sh -c "echo \$$ > $logdir/command.pid; exec 2>&1; exec $java_command" \
| $log_command $logdir/output.log &


Thanks,

-Greg
--
::::::::::::: Greg Andrews ::::: gerg@xxxxxxxxx :::::::::::::
I have a map of the United States that's actual size.
-- Steven Wright
.



Relevant Pages

  • Re: More elegant implementation than this?
    ... > a java command with stdout and stderr redirected to a logfile and the ... > writes it to the logfile and rolls the logfile when it gets too large. ... (at the moment I can't change to a different shell) ...
    (comp.unix.shell)
  • Re: Backgrounding a stream, and sendmail?
    ... >> just redirect the output to a file instead of sendmail.) ... > shell command like ... when prog_b has it's stdout redirected nothing ... > is done instead of printing to stdout you could start sendmail from within ...
    (comp.os.linux.development.apps)
  • Re: Shell out without so many threads?
    ... use threads to gather stdout and stderr from ... (to kill a shell script gone infinite). ... I'm using 4 threads (main, stdout_gather, stderr_gather, timeout) ... single thread with a "next timeout" model to ...
    (comp.lang.java.programmer)
  • Re: Shell out without so many threads?
    ... use threads to gather stdout and stderr from ... (to kill a shell script gone infinite). ... I'm using 4 threads (main, stdout_gather, stderr_gather, timeout) ... single thread with a "next timeout" model to ...
    (comp.lang.java.programmer)
  • Re: test whether stdout==stderr?
    ... > By default the shell attaches stdout and stderr to the same device, ... Actually it isn't the shell that does this. ...
    (comp.os.linux.development.apps)