help help help help help help

help help help help help help

Post by Bucco Thierr » Thu, 17 Sep 1998 04:00:00



Hello,

    I'm a french linux developer and i've a problem.

    this line doesn't work :

    execlp ("ps","ps","1>/home/err.txt","2>/home/msg.txt",0);

    in fact, stdout and stderr are not redirected. WHY ?????

    help me please.

    Thanks a lot.

    THIERRY - FRANCE

 
 
 

help help help help help help

Post by User » Sun, 20 Sep 1998 04:00:00


Quote:>    this line doesn't work :
>    execlp ("ps","ps","1>/home/err.txt","2>/home/msg.txt",0);

Because "1>/home/err.txt" and "2>..."
are not arguments to a program. They are used by the shell which creates
the program to identify what to
do with the output.

You can do the following instead:
system("ps 1>/home/err.txt 2>/home/msg.txt" );

Unless you must use exec in which case I am unable to help !

 
 
 

help help help help help help

Post by John McKow » Sun, 20 Sep 1998 04:00:00


execlp(...) does not interpret the arguments that you pass it. I would guess
that you are doing this after a fork(), but perhaps not. Anyway, a small,
untested code fragment would look something like this:

pid pid=fork(); /* create child */
if (0 == pid) { /* we're in the child process }
    freopen("/home/err.txt","w",stdout); /* 1>/home/err.txt */
    freopen("/home/msg.txt,"w",stderr); /* 2>/home/msg.txt */
    execlp("ps","ps",0);
    }

Note - untested but close if not exactly correct (I hope <grin>).

 
 
 

help help help help help help

Post by John McKow » Sun, 20 Sep 1998 04:00:00


OOPS - slight typo

freopen("/home/err.txt",&stdout); /* 1>/home/err.txt */
frepoen("/home/msg.txt",&stderr); /* 2>/home/msg.txt */

forgot the & in the original post

 
 
 

help help help help help help

Post by Steve » Tue, 22 Sep 1998 04:00:00



Quote:>>    this line doesn't work :
>>    execlp ("ps","ps","1>/home/err.txt","2>/home/msg.txt",0);

>Because "1>/home/err.txt" and "2>..."  are not arguments to
>a program. They are used by the shell which creates the program to
>identify what to do with the output.

>You can do the following instead:
>system("ps 1>/home/err.txt 2>/home/msg.txt" );

>Unless you must use exec in which case I am unable to help !

execlp("/bin/sh","/bin/sh","-c","ps >/home/err.txt 2>/home/msg.txt",0);

--
Steve Coile                 R e d   H a t             Systems Administration