- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: communication with background function
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 10:25 PM
06-30-2004 10:25 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 10:50 PM
06-30-2004 10:50 PM
Re: communication with background function
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 11:04 PM
06-30-2004 11:04 PM
Re: communication with background function
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 11:24 PM
06-30-2004 11:24 PM
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 12:06 AM
07-01-2004 12:06 AM
Re: communication with background function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 12:32 AM
07-01-2004 12:32 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 01:25 AM
07-01-2004 01:25 AM
Re: communication with background function
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 02:01 AM
07-01-2004 02:01 AM
Re: communication with background function
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. .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 02:51 AM
07-01-2004 02:51 AM
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 02:57 AM
07-01-2004 02:57 AM
Re: communication with background function
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 04:20 PM
07-01-2004 04:20 PM
Re: communication with background function
There is a discussion related to this one is at location.
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=611644
Regards,
Muthukumar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 08:55 PM
07-01-2004 08:55 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2004 11:21 PM
07-01-2004 11:21 PM
Re: communication with background function
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.