Operating System - HP-UX
1834961 Members
1820 Online
110071 Solutions
New Discussion

Re: communication with background function

 
vtihu
New Member

communication with background function


I a script I define a function which I need to call in the background and at certain places check it's status and see if it's finished (without waiting for it); if it's finished I need to check it's return value and I also need to communicate with the main script via an environment variable (lateral effect) in which I put something; is this possible?

Thanks
12 REPLIES 12
Simon Hargrave
Honored Contributor

Re: communication with background function

2 simple options I can think of: -

1) Have your background script write status information etc to a temporary file, and your "checking" script monitor the contents of this file.

2) Use a named pipes. eg "mknod inpipe p", "mknod outpipe p". Your script can then write to the outpipe file, and read from the inpipe file.

Note that the second method will "need" a program on the end of the pipes, essentially "feeding" it, otherwise it'll just hang up when it reads/writes.

Which option you use depends on what exactly you're trying to achieve.
Mark Grant
Honored Contributor

Re: communication with background function

Simons solutions (above) make a good deal of sense to me. Though to address the individual questions you have here I thought I'd offer some thoughts.

The best way of running a script and checking to see if it's finished is to trap "SIGCHLD". Your script will do something like

trap "myfunction" 18

When your child script exits, the function "myfunction" will be called.

The way to check the exit status of a command you ran in the background would be to use "wait". In the above example, you would put this in your "myfunction" function. Wait expects a job number. However, if your script only runs one other script in the background, the job number will probably be 1 . So, in "myfunction" you would have "wait %1" and the exit status of that is the exit status of the job you ran in the background.

You can not pass an environment variable to a parent. You can however get your child script to print the value as in

VARIABLE=`my-sub-script` &

However, variable will probably have a carriage return followed by some job control output appended to it. You can strip that off though.

These things combined might get you what you want.
Never preceed any demonstration with anything more predictive than "watch this"
vtihu
New Member

Re: communication with background function


Simon: I thought about this file approach too but I prefer another way of solving this, more direct;
Mark: very good the trap suggestion, at least theoretically, but first I want to say that I already tried the var=`function` & and it didn't work, here is a simple test:
"
function titi {
rez=1
while [ 1 ]; do
for i in /* ; do echo $i>>/scripts/test/titi.log;
done
if [ `wc -l /scripts/test/titi.log | cut -d" " -f1` -gt 37000 ]; then
rez=2; break;
fi
sleep 1
done
echo $rez
return $rez
}
rez=900
rez=`titi` &
while [ 1 ]; do
if [ `jobs | wc -l` -eq 0 ]; then break;
else echo Working;
fi
sleep 2
done
echo $rez
"
at the end I see the value 900 displayed, not 2, what I expect; what can be the problem?

Thanks
Mark Grant
Honored Contributor

Re: communication with background function

That "rez" bit should work if you don't enclose it in ` characters. However, the "trap" then won't work. So, I think, in order to get everything to work, you'll probably need to take your whole "titi" function and make is a separate script instead.
Never preceed any demonstration with anything more predictive than "watch this"
vtihu
New Member

Re: communication with background function


Even when changing the function into a script it doesn't work, as if the output would have been "stolen" and didn't reach the intended recipient; if there are no other suggestions I think I'll finally have to use the temp file approach which is very "robust" and works fine, with some extra coding!
Muthukumar_5
Honored Contributor

Re: communication with background function

hai,

We can get the back ground running process with some debugging statements and results redirection to a common file.

We can not use the environment variables in this situation,because client process can not change main process variables value.

We cannot check the return type of backgroundly running process. It may be end at any time.

The best way is to put the debug statements in the begin,during the operation and end of operation withit's PID and identifier like that.

We can use pipe in the script to interact with the main process to know the completion of execution.

Regards,
Muthukumar
Easy to suggest when don't know about the problem!
Mark Grant
Honored Contributor

Re: communication with background function

Sorry, I'm a bit stupid. That bit can't work properly. It will only work if the "&" is inside the ` marks. However, then the shell will wait for the script to finish.

Seems you can't persuade the parent to "capture" the information from the child if it's run in the background without using a file or named pipe. .

Never preceed any demonstration with anything more predictive than "watch this"
vtihu
New Member

Re: communication with background function


Yes, it seems ok with & inside because the assignment has to wait for the output to be produced etc., but then I don't have multithreading; I already changed the script to use temp files, but there is a price to pay which seems to be serious - the code gets a lot complicated...
Mark Grant
Honored Contributor

Re: communication with background function

Can't you combine the two approaches.

Have your function as a separate script. Run it in the background. Use the "trap" thing to determine when the function script has finished. The function called by "trap" can then use "wait" to determine the function scripts' exit status and then read a file, thoughtfully provided by the function script that contains the information you wanted.
Never preceed any demonstration with anything more predictive than "watch this"
Muthukumar_5
Honored Contributor

Re: communication with background function

Hai,

There is a discussion related to this one is at location.
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=611644

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
vtihu
New Member

Re: communication with background function


Mark, I agree, this combination seems to be the most "elegant" way to check for a child background function / script completion and it's return value; one problem is when you have many parameters and / or side effects and respond to the parent script in a more complex way than a mere return value, so you have also to use temp files
Muthukumar, I can't open that link; maybe you can check, I think it can be useful..

Thank you all

Vlad
Muthukumar_5
Honored Contributor

Re: communication with background function

Hai,

Let us go with a example shell script.

#!/usr/bin/ksh
# test.ksh
# Debug mode
set -x

# parent process ID
echo "PPID $$"
fun()
{
id=$1
echo test $id
test=1;export test
}

call()
{
fun 1 &
echo "PID $!" # child 1 process ID
fun 2 &
echo "PID $!" # child 2 process ID
# it is good to redirect child pid value to some variable and using ps command check the completion of process.
}

call
echo $test

We cannot set use the environent variables between the child and parent process. So use two files as status.log and return.log.

Use echo statements in the action of background process. Try to redirect all processing debug statements to that file. Use the separate ID for every child. Use the separate return statement and redirect that to the return.log file. Use the return.log file as /var/tmp/return _$ID.log . where ID is the identifier sent from parent to child. If it contains an entry,it is finised or analyse the return value's from that.

We can connect the background and parent process with unified files. we can control the child process actions with log files and as well as PIPE's. If we want to communicate to parent then, use checking conditions related with the log files.

The link http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=611644 is related to know the back ground process execution status analysis.

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!