waitpid

2003-01-15 Thread Mark Goland
Hello Perl lovers,

 I am developing a script which runs in TCSH 6.x shell. I have two questions

1.  I need to be able to set an global enviernment variable. The shell has a
build in command setenv which would enable me to do what I need. This is
what I try,

system("setenv DESTDIR /mnt");

...but this fails , by hand in works great. Source has the same problme. Any
ideas ???

2. I need to run a few external commands sequantialy. Run one  wait on it to
compleate run the others a sample command would be

system("cat cab.??|tar vxz -C ${DESTDIR}");

 In C I would usually fork and waitpid on child, I was wondering if there is
a short trick to this in Perl. ( I know IPC::OpenX returns pid and I can do
a waitpid on it ) .

Thanx in advance,
Mark


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: waitpid

2003-01-15 Thread Beau E. Cox
Hi -

> -Original Message-
> From: Mark Goland [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, January 15, 2003 7:56 AM
> To: perl
> Subject: waitpid
> 
> 
> Hello Perl lovers,
> 
>  I am developing a script which runs in TCSH 6.x shell. I have 
> two questions
> 
> 1.  I need to be able to set an global enviernment variable. The 
> shell has a
> build in command setenv which would enable me to do what I need. This is
> what I try,
> 
> system("setenv DESTDIR /mnt");
> 
> ...but this fails , by hand in works great. Source has the same 
> problme. Any
> ideas ???
> 
> 2. I need to run a few external commands sequantialy. Run one  
> wait on it to
> compleate run the others a sample command would be
> 
> system("cat cab.??|tar vxz -C ${DESTDIR}");
> 
>  In C I would usually fork and waitpid on child, I was wondering 
> if there is
> a short trick to this in Perl. ( I know IPC::OpenX returns pid 
> and I can do
> a waitpid on it ) .
> 
> Thanx in advance,
> Mark
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

1. ???
2. system ($cmd) and `$cmd` do _wait_ for the command to 
finish before returing (the difference being system retuns
the command's return code and `` returns the output). A
series of these should do what you want.

Aloha => Beau;


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: waitpid

2003-01-15 Thread Mark Goland
> 2. system ($cmd) and `$cmd` do _wait_ for the command to
> finish before returing (the difference being system retuns
> the command's return code and `` returns the output). A
> series of these should do what you want.
thats what I though, but aparently thats not the case. This maybe do
to a fact that I am starting a child which in return starts a child, hance I
create grandchild. So I dont think system waits fro grandkids to compleate.
after I extract a given cab I need to configure it. So what ends up
happening is that my configure script ends up runing before the cab is
extracted.

Mark

- Original Message -
From: "Beau E. Cox" <[EMAIL PROTECTED]>
To: "Mark Goland" <[EMAIL PROTECTED]>; "perl" <[EMAIL PROTECTED]>
Sent: Wednesday, January 15, 2003 1:34 PM
Subject: RE: waitpid


> Hi -
>
> > -Original Message-
> > From: Mark Goland [mailto:[EMAIL PROTECTED]]
> > Sent: Wednesday, January 15, 2003 7:56 AM
> > To: perl
> > Subject: waitpid
> >
> >
> > Hello Perl lovers,
> >
> >  I am developing a script which runs in TCSH 6.x shell. I have
> > two questions
> >
> > 1.  I need to be able to set an global enviernment variable. The
> > shell has a
> > build in command setenv which would enable me to do what I need. This is
> > what I try,
> >
> > system("setenv DESTDIR /mnt");
> >
> > ...but this fails , by hand in works great. Source has the same
> > problme. Any
> > ideas ???
> >
> > 2. I need to run a few external commands sequantialy. Run one
> > wait on it to
> > compleate run the others a sample command would be
> >
> > system("cat cab.??|tar vxz -C ${DESTDIR}");
> >
> >  In C I would usually fork and waitpid on child, I was wondering
> > if there is
> > a short trick to this in Perl. ( I know IPC::OpenX returns pid
> > and I can do
> > a waitpid on it ) .
> >
> > Thanx in advance,
> > Mark
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
> 1. ???
> 2. system ($cmd) and `$cmd` do _wait_ for the command to
> finish before returing (the difference being system retuns
> the command's return code and `` returns the output). A
> series of these should do what you want.
>
> Aloha => Beau;
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: waitpid

2003-01-15 Thread wiggins


On Wed, 15 Jan 2003 12:55:39 -0500, Mark Goland <[EMAIL PROTECTED]> wrote:

> Hello Perl lovers,
> 
>  I am developing a script which runs in TCSH 6.x shell. I have two questions
> 

Remember your script runs in Perl not in the shell, you run the perl interpreter in 
the shell, and I believe Perl uses whatever shell is default on the system for how to 
run its "system" calls, aka most likely /bin/sh (which could be pointed to tcsh, but 
may not).

> 1.  I need to be able to set an global enviernment variable. The shell has a
> build in command setenv which would enable me to do what I need. This is
> what I try,
> 
> system("setenv DESTDIR /mnt");
> 

This is most likely succeeding (assuming the shell accepts it all) but I would imagine 
the shell is started, the command is run, and the shell is closed. So your subsequent 
call to the shell will be invoked in a new instance of the environment, without what 
you set. But it appears there is no reason to use this, at least not in this code 
segment, see below.


> ...but this fails , by hand in works great. Source has the same problme. Any
> ideas ???
> 
> 2. I need to run a few external commands sequantialy. Run one  wait on it to
> compleate run the others a sample command would be
> 
> system("cat cab.??|tar vxz -C ${DESTDIR}");

Here your "${DESTDIR}" is being interpolated to use perl's DESTDIR variable instead of 
the shell's.  Is this what you intended (is it even set, are you running with strict 
and warnings?)  Other than that it should be working I believe.  Are you getting an 
error result? You should check for one by adding an 'or die ("Couldn't tar files: 
$!")' or some such to the system call, or check the return value ($?).

> 
>  In C I would usually fork and waitpid on child, I was wondering if there is
> a short trick to this in Perl. ( I know IPC::OpenX returns pid and I can do
> a waitpid on it ) .
> 

How do you mean "short trick" ... it seems IPC::OpenX would be the short trick, the 
normal way will work as you mentioned, to fork, and then waitpid on that child, of 
course within that fork you are probably still looking at doing a 'system'.  'system' 
just encapsulates the fork/waitpid process for you, so you don't gain anything by 
first forking. To maintain the shell's environment if this can't be handled in Perl, 
you may be able to string commands together in one command to system separated by ;'s 
like on the command line, but this I am not sure about.

HTH a little, Good luck...

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: waitpid

2003-01-15 Thread Mark Goland
> > system("cat cab.??|tar vxz -C ${DESTDIR}");
>
> Here your "${DESTDIR}" is being interpolated to use perl's DESTDIR
variable instead of the shell's.  Is this what you intended (is it even set,
are you running with strict and warnings?)  Other than that it should be
working I believe.  Are you getting an error result? You should check for
one by adding an 'or die ("Couldn't tar files: $!")' or some such to the
system call, or check the return value ($?).

 yeh I know, I didnt want to write an ungly system call, this is what I
am really using.
system("cat cab.??|tar vxz -C \$\{DESTDIR\}");

following up on ...> Remember your script runs in Perl not in the shell, you
run the perl interpreter in the shell, and I believe Perl uses whatever
shell is default on the system for how to run its "system" calls, aka most
likely /bin/sh (which could be pointed to tcsh, but may not)<...
 I will probebly go with expanding a variable set with in my script. By the
way is there a way to find which shell is default, I am developing on
FreeBSD and sh is the default shell, but I will be runing these scripts as
root, and tcsh is the default root shell, this would be of segnificance.

>To maintain the shell's environment if this can't be handled in Perl, you
may be able to string commands together in one >command to system separated
by ;'s like on the command line, but this I am not sure about.

 will look into that.

Thanx,
Mark


- Original Message -
From: <[EMAIL PROTECTED]>
To: "Mark Goland" <[EMAIL PROTECTED]>; "perl" <[EMAIL PROTECTED]>
Sent: Wednesday, January 15, 2003 2:04 PM
Subject: RE: waitpid


>
> 
> On Wed, 15 Jan 2003 12:55:39 -0500, Mark Goland <[EMAIL PROTECTED]>
wrote:
>
> > Hello Perl lovers,
> >
> >  I am developing a script which runs in TCSH 6.x shell. I have two
questions
> >
>
> Remember your script runs in Perl not in the shell, you run the perl
interpreter in the shell, and I believe Perl uses whatever shell is default
on the system for how to run its "system" calls, aka most likely /bin/sh
(which could be pointed to tcsh, but may not).
>
> > 1.  I need to be able to set an global enviernment variable. The shell
has a
> > build in command setenv which would enable me to do what I need. This is
> > what I try,
> >
> > system("setenv DESTDIR /mnt");
> >
>
> This is most likely succeeding (assuming the shell accepts it all) but I
would imagine the shell is started, the command is run, and the shell is
closed. So your subsequent call to the shell will be invoked in a new
instance of the environment, without what you set. But it appears there is
no reason to use this, at least not in this code segment, see below.
>
>
> > ...but this fails , by hand in works great. Source has the same problme.
Any
> > ideas ???
> >
> > 2. I need to run a few external commands sequantialy. Run one  wait on
it to
> > compleate run the others a sample command would be
> >
> > system("cat cab.??|tar vxz -C ${DESTDIR}");
>
> Here your "${DESTDIR}" is being interpolated to use perl's DESTDIR
variable instead of the shell's.  Is this what you intended (is it even set,
are you running with strict and warnings?)  Other than that it should be
working I believe.  Are you getting an error result? You should check for
one by adding an 'or die ("Couldn't tar files: $!")' or some such to the
system call, or check the return value ($?).
>
> >
> >  In C I would usually fork and waitpid on child, I was wondering if
there is
> > a short trick to this in Perl. ( I know IPC::OpenX returns pid and I can
do
> > a waitpid on it ) .
> >
>
> How do you mean "short trick" ... it seems IPC::OpenX would be the short
trick, the normal way will work as you mentioned, to fork, and then waitpid
on that child, of course within that fork you are probably still looking at
doing a 'system'.  'system' just encapsulates the fork/waitpid process for
you, so you don't gain anything by first forking. To maintain the shell's
environment if this can't be handled in Perl, you may be able to string
commands together in one command to system separated by ;'s like on the
command line, but this I am not sure about.
>
> HTH a little, Good luck...
>
> http://danconia.org
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: waitpid

2003-01-15 Thread Mark Goland
> 1.  Use :
> $ENV{DESTDIR} = "/mnt";
> That will set the environment variable that is passed to any
> subshells (such as system calls).  Executing a system(setenv...) is kind
> of like a noop, since the shell sets it and then exists...so it
> disappears.

This will work for script and all sub kids of the script, but not the
original shell, in which system calls get executed.

>  There really isn't a
> "good" way to deal with this.  Perhaps if the process returned it's PID
> after the fork you could look for it and then keep looking and waiting for
> it (I don't think waitpid would work for you here, since you won't get a
> signal from a process that isn't a child of yours when it exits).
>

Yeh, thats exactly what I am looking for.

- Original Message -
From: "Chander Ganesan" <[EMAIL PROTECTED]>
To: "Mark Goland" <[EMAIL PROTECTED]>
Sent: Wednesday, January 15, 2003 4:37 PM
Subject: Re: waitpid


> Hello,
>
> 1.  Use :
> $ENV{DESTDIR} = "/mnt";
> That will set the environment variable that is passed to any
> subshells (such as system calls).  Executing a system(setenv...) is kind
> of like a noop, since the shell sets it and then exists...so it
> disappears.
>
> 2.  The system command will return as soon as the command it calls
> returns.  So if you run an application which forks off and detaches from
> the parent process it will return immediately.  There really isn't a
> "good" way to deal with this.  Perhaps if the process returned it's PID
> after the fork you could look for it and then keep looking and waiting for
> it (I don't think waitpid would work for you here, since you won't get a
> signal from a process that isn't a child of yours when it exits).
>
> I suspect if you fork() and then run you'll have the same issue if the
> underlying application forks off on it's own.
>
> In general, the system command is what you are looking for...for your
> special case things might be a bit more difficult.
>
> Hope that helps.
>
> Chander
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




sys_wait_h and waitpid

2005-06-06 Thread Bryan R Harris

All,

I've written a simple daemon to launch processes, and I check for the exit
status of those processes.  According to "Programming Perl", I need to use
the following:

  use POSIX qw(:sys_wait_h);

However on OS X (no jabs or snide remarks, please, today was painful enough!
=), it says:

  ":sys_wait_h" is not exported by the POSIX module

If I remove it, it runs under OS X, but then under IRIX it takes 8+ seconds
just to do a waitpid call, which is way too long for this application.  With
it in there, it runs instantly on IRIX.

Any ideas?

- B






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: sys_wait_h and waitpid

2005-06-06 Thread Chris Devers
On Mon, 6 Jun 2005, Bryan R Harris wrote:

> Any ideas?
 
Wrap the use() statement in an if block.

if ( $^O eq 'darwin' ) {
use POSIX;
} else {
use POSIX qw(:sys_wait_h);
}

Will that work?


-- 
Chris Devers

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: sys_wait_h and waitpid

2005-06-06 Thread Wiggins d'Anconia
Chris Devers wrote:
> On Mon, 6 Jun 2005, Bryan R Harris wrote:
> 
> 
>>Any ideas?
> 
>  
> Wrap the use() statement in an if block.
> 
> if ( $^O eq 'darwin' ) {
> use POSIX;
> } else {
> use POSIX qw(:sys_wait_h);
> }
> 
> Will that work?
> 
> 

Only if you wrap that in a BEGIN block. Remember, 'use' happens at
compile not run time.

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: sys_wait_h and waitpid

2005-06-07 Thread Bryan R Harris

>>> Any ideas?
>>  
>> Wrap the use() statement in an if block.
>> 
>> if ( $^O eq 'darwin' ) {
>> use POSIX;
>> } else {
>> use POSIX qw(:sys_wait_h);
>> }
>> 
>> Will that work?
> 
> Only if you wrap that in a BEGIN block. Remember, 'use' happens at
> compile not run time.


Here's what I tried:

**
BEGIN {
if ( $^O eq 'darwin' ) { use POSIX; }
else { use POSIX qw(setsid nice :sys_wait_h); } # line 14
}
**


... but it still errors out:

% sko
":sys_wait_h" is not exported by the POSIX module
Can't continue after import errors at
/System/Library/Perl/5.8.1/darwin-thread-multi-2level/POSIX.pm line 19
BEGIN failed--compilation aborted at /Users/bh/Library/perl/sko line 14.

% perl -e 'print $^O, "\n";'
darwin

Any other ideas?

TIA.

- Bryan 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: sys_wait_h and waitpid

2005-06-07 Thread Paul Johnson
On Tue, Jun 07, 2005 at 08:23:43AM -0700, Bryan R Harris wrote:
> 
> >>> Any ideas?
> >>  
> >> Wrap the use() statement in an if block.
> >> 
> >> if ( $^O eq 'darwin' ) {
> >> use POSIX;
> >> } else {
> >> use POSIX qw(:sys_wait_h);
> >> }
> >> 
> >> Will that work?

No.

> > Only if you wrap that in a BEGIN block.

And not even then.

> > Remember, 'use' happens at
> > compile not run time.

This is true, but BEGIN happens at compile time too so you've not gained
much.

> Here's what I tried:
> 
> **
> BEGIN {
> if ( $^O eq 'darwin' ) { use POSIX; }
> else { use POSIX qw(setsid nice :sys_wait_h); } # line 14
> }
> 
> ... but it still errors out:

> Any other ideas?

"perldoc -f use" reminds us that "use Module LIST" is exactly
equivalent to

BEGIN { require Module; import Module LIST; }

so:

BEGIN
{
require POSIX;
POSIX->import($^O ne "darwin" ? qw(setsid nice :sys_wait_h) : ())
}

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: sys_wait_h and waitpid

2005-06-07 Thread Bryan R Harris

> Any ideas?
  
 Wrap the use() statement in an if block.
 
 if ( $^O eq 'darwin' ) {
 use POSIX;
 } else {
 use POSIX qw(:sys_wait_h);
 }
 
 Will that work?
> 
> No.
> 
>>> Only if you wrap that in a BEGIN block.
> 
> And not even then.
> 
>>> Remember, 'use' happens at
>>> compile not run time.
> 
> This is true, but BEGIN happens at compile time too so you've not gained
> much.
> 
>> Here's what I tried:
>> 
>> **
>> BEGIN {
>> if ( $^O eq 'darwin' ) { use POSIX; }
>> else { use POSIX qw(setsid nice :sys_wait_h); } # line 14
>> }
>> 
>> ... but it still errors out:
> 
>> Any other ideas?
> 
> "perldoc -f use" reminds us that "use Module LIST" is exactly
> equivalent to
> 
> BEGIN { require Module; import Module LIST; }
> 
> so:
> 
> BEGIN
> {
> require POSIX;
> POSIX->import($^O ne "darwin" ? qw(setsid nice :sys_wait_h) : ())
> }


That seems to work, and there's *no way* I'd have ever figured that out on
my own.

Thanks Paul!

- Bryan




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




waitpid() and exitcode >> 8 ?

2005-11-07 Thread Elie De Brauwer

Hello list,

I recently encountered a small oddity.  Suppose I have a process A:

#!/usr/bin/perl

use strict;

print "Hello \n";
sleep 1;
print "Goodbye\n";
exit 9;

Simply shows some out and gives a certain exit code. A second process, 
simply calls fork, execs the child in a process and waits for the child 
in the other process. In Perl this can look like this:


my $cmd = "/home/user/proces.pl";
my $pid = fork();
if($pid == 0){
print "Hi I'm a child\n";
exec $cmd or die "Failed to run $cmd\n";
}else{
print "Hi I'm a parent waiting for child with PID: $pid\n";
my $ret = waitpid($pid,0);
print "$pid exited with code ". ($?>>8) ."\n";
}

The oddity i located in the last line. It seemd that I had to divide $? 
by 256 (or shift over 8 positions to the right) to get the correct exit 
code. So my question is:

a) Is there an other way to wait for a child to die and get the exit code
b) Can someone explain the odd behaviour of the exit code ?

greetings
E.

--
Elie De Brauwer


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: waitpid() and exitcode >> 8 ?

2005-11-07 Thread John W. Krahn
Elie De Brauwer wrote:
> Hello list,

Hello,

> I recently encountered a small oddity.  Suppose I have a process A:
> 
> #!/usr/bin/perl
> 
> use strict;
> 
> print "Hello \n";
> sleep 1;
> print "Goodbye\n";
> exit 9;
> 
> Simply shows some out and gives a certain exit code. A second process,
> simply calls fork, execs the child in a process and waits for the child
> in the other process. In Perl this can look like this:
> 
> my $cmd = "/home/user/proces.pl";
> my $pid = fork();
> if($pid == 0){
> print "Hi I'm a child\n";
> exec $cmd or die "Failed to run $cmd\n";
> }else{
> print "Hi I'm a parent waiting for child with PID: $pid\n";
> my $ret = waitpid($pid,0);
> print "$pid exited with code ". ($?>>8) ."\n";
> }
> 
> The oddity i located in the last line. It seemd that I had to divide $?
> by 256 (or shift over 8 positions to the right) to get the correct exit
> code. So my question is:
> a) Is there an other way to wait for a child to die and get the exit code
> b) Can someone explain the odd behaviour of the exit code ?

Have you read the perl documentation on the functions and variables you are 
using?

perldoc perlipc
perldoc perlvar
perldoc -f fork
perldoc -f exec
perldoc -f waitpid



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: waitpid() and exitcode >> 8 ?

2005-11-07 Thread Shawn Corey

Elie De Brauwer wrote:

Hello list,

I recently encountered a small oddity.  Suppose I have a process A:

#!/usr/bin/perl

use strict;

print "Hello \n";
sleep 1;
print "Goodbye\n";
exit 9;

Simply shows some out and gives a certain exit code. A second process, 
simply calls fork, execs the child in a process and waits for the child 
in the other process. In Perl this can look like this:


my $cmd = "/home/user/proces.pl";
my $pid = fork();


die "cannot fork" unless defined( $pid );


if($pid == 0){
print "Hi I'm a child\n";
exec $cmd or die "Failed to run $cmd\n";
}else{
print "Hi I'm a parent waiting for child with PID: $pid\n";
my $ret = waitpid($pid,0);
print "$pid exited with code ". ($?>>8) ."\n";
}

The oddity i located in the last line. It seemd that I had to divide $? 
by 256 (or shift over 8 positions to the right) to get the correct exit 
code. So my question is:

a) Is there an other way to wait for a child to die and get the exit code
b) Can someone explain the odd behaviour of the exit code ?

greetings
E.



The lower 8 bits are flags that tell you the conditions under which the 
child stopped. It could be normal termination, halted by signal (TERM or 
HALT), and if there is a core file. (Core files contain both the program 
and data at the time of termination. If the program was working on 
sensitive data and you're clever enough, you can read this data. Leaving 
core files lying around is a security risk.) Normally, Perl programs 
just ignore these flags.



--

Just my 0.0002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: waitpid() and exitcode >> 8 ?

2005-11-07 Thread Elie De Brauwer

John W. Krahn wrote:

Elie De Brauwer wrote:


Hello list,



Hello,



I recently encountered a small oddity.  Suppose I have a process A:

#!/usr/bin/perl

use strict;

print "Hello \n";
sleep 1;
print "Goodbye\n";
exit 9;

Simply shows some out and gives a certain exit code. A second process,
simply calls fork, execs the child in a process and waits for the child
in the other process. In Perl this can look like this:

my $cmd = "/home/user/proces.pl";
my $pid = fork();
if($pid == 0){
   print "Hi I'm a child\n";
   exec $cmd or die "Failed to run $cmd\n";
}else{
   print "Hi I'm a parent waiting for child with PID: $pid\n";
   my $ret = waitpid($pid,0);
   print "$pid exited with code ". ($?>>8) ."\n";
}

The oddity i located in the last line. It seemd that I had to divide $?
by 256 (or shift over 8 positions to the right) to get the correct exit
code. So my question is:
a) Is there an other way to wait for a child to die and get the exit code
b) Can someone explain the odd behaviour of the exit code ?



Have you read the perl documentation on the functions and variables you are 
using?

perldoc perlipc

-> Read it

perldoc perlvar

-> Didn't read it

perldoc -f fork

-> Read it

perldoc -f exec

-> Read it

perldoc -f waitpid

-> Read it





And as murphy'd say, perldoc perlvar contained what i was looking for:
 $?  The status returned by the last pipe close, backtick (``) com-
 mand, successful call to wait() or waitpid(), or from the sys-
 tem() operator.  This is just the 16-bit status word returned
 by the wait() system call (or else is made up to look like it).
 Thus, the exit value of the subprocess is really ("$? >> 8"),
 and "$? & 127" gives which signal, if any, the process died
 from, and "$? & 128" reports whether there was a core dump.
 (Mnemonic: similar to sh and ksh.)

Thanks for the pointer


--
Elie De Brauwer



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>