Operating System - HP-UX
1828370 Members
3020 Online
109976 Solutions
New Discussion

Background process in a while-read loop

 
Stig Eriksson
Occasional Advisor

Background process in a while-read loop

I am trying to start some background processes in a "while read ... do ... done" loop.
I have a configuration file that is parsed:

grep pattern configfile | while read prc
do
start_program $prc > /dev/null 2>&1 &
done

This does not work anymore
10 REPLIES 10
monasingh_1
Trusted Contributor

Re: Background process in a while-read loop

Shuld work fine. what shell you are in?
try redirecting your standard error and standard output to a log file instead of /dev/null. It should give some details there.

Massimo Bianchi
Honored Contributor

Re: Background process in a while-read loop

HI,
I think that your problem is that the file is parsed only once.

TO have it work, try this:

tail -f configfile| grep pattern | while read prc
do
start_program $prc > /dev/null 2>&1 &
done


THis works IF you are just adding lines to your files, for example with a
echo "prog" >> configfile.

But if you want to run a program, each time you modify a line i that file, it's more difficult.

You have to write a deamon that check the file ans parse it.

Is this that you want ?

Regards,
Massimo

Stuart Browne
Honored Contributor

Re: Background process in a while-read loop

You might also try redirecting stdin on the start-program from /dev/null. This will stop it from trying to read from stdin, i.e.

grep pattern configfile | while read prc
do
start_program $prc > /dev/null 2>&1 < /dev/null &
done

Although admittadly, a backgrounded process shouldn't be able to read from stdin as far as I'm aware..
One long-haired git at your service...
Muthukumar_5
Honored Contributor

Re: Background process in a while-read loop

Your logic on shell is correct.

set -x
grep pattern configfile | while read prc
do
start_program $prc > /dev/null 2>&1 &
pid=$!
echo "Process ID: $pid"
ps -ef | grep -v grep | grep -w $pid
done

It will give the behaviour / problem to you more.
Easy to suggest when don't know about the problem!
Stig Eriksson
Occasional Advisor

Re: Background process in a while-read loop

I will give some more info:

This has been working before, but now, with another revision of ksh (82.10.2.61) it does not work anymore.

The script works, so that a couple of processes are started, but the script does not terminate. It does not reach the line following the "done" command.

A "nohup start_program ..." does not work either.
John Palmer
Honored Contributor

Re: Background process in a while-read loop

Have you tried enclosing the while loop in braces? I always do this when reading from a file or pipe...

grep pattern configfile | {
while read prc
do
start_program $prc > /dev/null 2>&1 &
done
}

Regards,
John
Muthukumar_5
Honored Contributor

Re: Background process in a while-read loop

The problem may be on start_program function.
It is good to use exit 0 statement on processed function and main program.

exit 0 statement is needed on your shell functions and main program.

And more it's good to have your script and debug input to try on your problem.

Use set -x to start the script and do as,

start_program $prc > /tmp/testlog.log 2>&1

so that start_program informations will be there in /tmp/testlog.log to track the problem
Easy to suggest when don't know about the problem!
Dietmar Konermann
Honored Contributor

Re: Background process in a while-read loop

Try to add some stdin redirection for start_program... maybe it's 'stealing' data from stdin.

grep pattern configfile | while read prc
do
start_program $prc /dev/null 2>&1 &
done

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Massimo Bianchi
Honored Contributor

Re: Background process in a while-read loop

Hi,
if it never reach the "done", what is the content of the config file ?

Maybe there is some garbage..

Also, check the canghelog for the ksh. Since you mentioned a change in it, maybe there is a different behaviour then expected.

HTH,
Massimo
Stig Eriksson
Occasional Advisor

Re: Background process in a while-read loop

Actually i don't remember how this was resolved. Just wanted to close this thread and assign points to those who tried to help me. Thanks to you all...