Operating System - HP-UX
1748161 Members
3965 Online
108758 Solutions
New Discussion юеВ

Re: Coprocess exit code reading

 
Jeff Carlin
Frequent Advisor

Coprocess exit code reading

Does anyone know how I can read the exit code from one of my coprocess' in a ksh script? I need to know if the coprocess exited with a 0, 1 or 2. I could write into the coprocess to print out how it was going to exit and have the master script read that from the pipe, but I think reading the exit code would be cleaner and safer. Any tips?
Where wisdom is called for, force is of little use. --Of course, a hammer does wonders for relieving stress.
6 REPLIES 6
Jordan Bean
Honored Contributor

Re: Coprocess exit code reading


define a repear trap at the begninning:

export COPID COPIDERR
trap 'wait $COPID; COPIDERR=$?' CHLD

immediately after spawning the coproc, capture its PID:

coproc |&
COPID=$!

when the coprocess terminates, COPIDERR will be set to its exit status, so you can look at it whenever you want.

Jeff Carlin
Frequent Advisor

Re: Coprocess exit code reading

It didn't work, but your suggestion sent me back to the "books". I found this tidbit:

"Traps are not processed while a job is waiting for a foreground process. Thus, a trap on CHLD won't be executed until the foreground job terminates."

What does this mean?

Where wisdom is called for, force is of little use. --Of course, a hammer does wonders for relieving stress.
Jordan Bean
Honored Contributor

Re: Coprocess exit code reading

That means if a another command is running in the foreground at the time the background child terminates, the trap will not run until the foreground command finishes. This is fine.

Can you attach your script or show us the relevant segments?

Make sure that you are using single quotes:

trap 'wait $COPID; COPIDERR=$?' CHLD

trap commands are evaluated twice: once when defined and once when taken. This means that

trap "wait $COPID; COPIDERR=$?" CHLD

may set the trap to 'wait; COPIDERR=' which is meaningless.

Jeff Carlin
Frequent Advisor

Re: Coprocess exit code reading

Sure, here are my test scripts...
Where wisdom is called for, force is of little use. --Of course, a hammer does wonders for relieving stress.
Jordan Bean
Honored Contributor

Re: Coprocess exit code reading

Uh oh. I wrote my own test script and learned that the CHLD signal doesn't appear to be handled by either korn or posix, at least not in HP-UX. The open source Korn shell from AT&T handles it immediately. You will need to reap it at some point late in the script when you know the coprocess has finished.
Jeff Carlin
Frequent Advisor

Re: Coprocess exit code reading

I'm finding other things not supported in HP KSH... for instance I can't read from /dev/tcp like on other systems... Thanks for you help though! I'll just trap my exit in the child process and send a message on how it will terminate to stdout.
Where wisdom is called for, force is of little use. --Of course, a hammer does wonders for relieving stress.