Operating System - HP-UX
1753689 Members
5133 Online
108799 Solutions
New Discussion

Re: Asynchronous command execution in a korn shell script

 
SOLVED
Go to solution
Kris_Knigga
Trusted Contributor

Asynchronous command execution in a korn shell script

I'm writing a little script (a function that gets sourced into the current shell, actually) that calls a couple of commands asynchronously (command &).  The script works fine, but I can't figure out how to suppress the job control information that gets printed to the terminal due to the asynchronous command calls.

 

Example:

$ gethostbyalc ai dev
[1]     27621
[2]     27622
devg0d41
[2] +  Done                    gethostbyalc ai dev
[1] +  Done                    gethostbyalc ai dev
$

 

I figured out that the first lines that print the PID when the commands get backgrounded are going to stderr, so that's pretty easy to get rid of.  The "done" lines, however, are being a little more stubborn.  I figured out that if I turn off ksh's monitor mode with "set +m" before I run the function that the "done" lines go away.  However, putting "set +m" in the function doesn't work. 

 

Example turning monitoring off before calling the function (works):

$ set -x
$ set +m
+ set +m
$ gethostbyalc ai dev
+ gethostbyalc ai dev
+ OPTS=
+ APP=ai
+ LCYCLE=dev
+ + date +%Y%m%d%H%M%S
APP_FIFO=/tmp/app_list_fifo.20110826142834
+ + date +%Y%m%d%H%M%S
LCYCLE_FIFO=/tmp/lcycle_list_fifo.20110826142834
+ mkfifo /tmp/app_list_fifo.20110826142834
+ mkfifo /tmp/lcycle_list_fifo.20110826142834
+ exec
+ 3>& 2
+ exec
+ 2> /dev/null
devg0d41
+ exec
+ + rm /tmp/app_list_fifo.20110826142834
+ rm /tmp/lcycle_list_fifo.20110826142834
$

 

Example turning monitoring off inside the function (doesn't work):

$ set -x
$ gethostbyalc ai dev
+ gethostbyalc ai dev
+ set +m
+ OPTS=
+ APP=ai
+ LCYCLE=dev
+ + date +%Y%m%d%H%M%S
APP_FIFO=/tmp/app_list_fifo.20110826143118
+ + date +%Y%m%d%H%M%S
LCYCLE_FIFO=/tmp/lcycle_list_fifo.20110826143118
+ mkfifo /tmp/app_list_fifo.20110826143118
+ mkfifo /tmp/lcycle_list_fifo.20110826143118
+ exec
+ 3>& 2
+ exec
+ 2> /dev/null
devg0d41
+ exec
+ + rm /tmp/app_list_fifo.20110826143118
+ rm /tmp/lcycle_list_fifo.20110826143118
+ set -m
[2] +  Done                    gethostbyalc ai dev
[1] +  Done                    gethostbyalc ai dev
$

 

Any ideas how to kill that output?


Kris Knigga
6 REPLIES 6
Kris_Knigga
Trusted Contributor

Re: Asynchronous command execution in a korn shell script

Full function source:

gethostbyalc ()
{
        set +m
        OPTS=$3
        APP=$1
        LCYCLE=$2

        APP_FIFO="/tmp/app_list_fifo.$(date +'%Y%m%d%H%M%S')"
        LCYCLE_FIFO="/tmp/lcycle_list_fifo.$(date +'%Y%m%d%H%M%S')"

        mkfifo ${APP_FIFO}
        mkfifo ${LCYCLE_FIFO}

        exec 3>&2
        exec 2>/dev/null

        gethostbyapp $1 > ${APP_FIFO} &
        gethostbylcycle $2 > ${LCYCLE_FIFO} &

        case ${OPTS} in
                "-w")
                        comm -12 ${APP_FIFO} ${LCYCLE_FIFO} | awk 'BEGIN{ ORS=" " }{ print }'
                        echo
                        ;;

                *)
                        comm -12 ${APP_FIFO} ${LCYCLE_FIFO}
                        ;;
        esac

        wait

        exec 2>&3
        exec 3>&-

        rm ${APP_FIFO}
        rm ${LCYCLE_FIFO}
}

 


Kris Knigga
James R. Ferguson
Acclaimed Contributor

Re: Asynchronous command execution in a korn shell script


@mkdlxk wrote:

I figured out that the first lines that print the PID when the commands get backgrounded are going to stderr, so that's pretty easy to get rid of.  The "done" lines, however, are being a little more stubborn.  I figured out that if I turn off ksh's monitor mode with "set +m" before I run the function that the "done" lines go away.  However, putting "set +m" in the function doesn't work. 

 


So, what's wrong with doing the 'set +m' in the calling script, before you source the file containing the function you want to run in the background?

 

Regards!

 

...JRF...

Kris_Knigga
Trusted Contributor

Re: Asynchronous command execution in a korn shell script

That certainly would work, it's just kind of annoying to remember to have to do that.

 

Also, I can see cases where the function might be called interactively from the command line.  Again, while it would work to remember to "set +m" before use, it would get old quickly.

 

The better solution, if possible, is figuring out how to turn off monitoring inside the function.

 


Kris Knigga
Dennis Handly
Acclaimed Contributor

Re: Asynchronous command execution in a korn shell script

>The better solution is figuring out how to turn off monitoring inside the function.

 

Or convert it from a function to a separate script?

 

I think the issue is that when the function returns, it resets +m.  (I've had to add "set -x" to each individual function.)

And moving it to a separate script may fail the same way if the background processes still haven't finished before it returns.

 

You could always put a sleep there. :-)

Kris_Knigga
Trusted Contributor

Re: Asynchronous command execution in a korn shell script


@Dennis Handly wrote:

>...may fail the same way if the background processes still haven't finished before it returns.

 

You could always put a sleep there. :-)



I was hoping the wait would take care of any such issues, but apparently not.


Kris Knigga
Kris_Knigga
Trusted Contributor
Solution

Re: Asynchronous command execution in a korn shell script

Unfortunately, the best answer I could come up with was to do away with the fancy named pipes and just use temporary files.  It does away with the need to background any processes and thusly eliminates unwanted output.


Kris Knigga