linuxstdoutstderrnohup

How do I ask nohup not to redirect stderr to stdout if it is detached from terminal?


This question looks naive but I can't find the answer after half an hour of intensive searching. Basically I do not want stderr messages to sneak into my result file.

$nohup ./a.out | gzip -c > result.txt.gz 2>log.txt &
nohup: ignoring input and redirecting stderr to stdout
$zcat result.txt.gz
this is stderr output.
this is stdout output.

The a.out file was compiled from the following test.c by "cc test.c",

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
    fprintf(stderr, "this is stderr output.\n");
    fprintf(stdout, "this is stdout output.\n");
    return 0;
}

But I do not want stderr message to be in the result file result.txt.gz. It should be in log.txt but that file is empty. Thank you!


Solution

  • That error redirection actually applies to the gzip command, not /.a.out. What you're looking for is ./a.out 2> log.txt | gzip -c > result.txt.gz.