How to export a variable from a child process running in background to the parent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to export a variable from a child process running in background to the parent
# 1  
Old 11-12-2008
Question How to export a variable from a child process running in background to the parent

Hi All,

I have a script which calls a child script with a parameter to be run in the background

. childscript.ksh $a &

Can any one suggest me how do i export a variable from the child script to parent script?

Note that the child script is in background
If the child script is in foreground that is not an issue.

Also can any one suggest me how to get back the exit status (of say exit 0 ) from any of the functions in the child script to the parent script, again in this case the child script is running in the background.

Thanks
# 2  
Old 11-12-2008
shell scripts are not supposed for IPC. they are just shell scripts.

you might find out the values of any of your childs variable by parsing /proc/[pid]/env in case you're on linux. also you functions may write variabes to a file to monitor the results.

you should use perl or at least c to do such things in a reliable manner
# 3  
Old 11-12-2008
Hi demwz

I am basically not much of a Unix guy... and didn't understand much of what you said. But do you mean it is not possible, and i am working on Kshell.

But if you mean it is not possible...? Why is it so with a background process..that you are not able to export the varible

Parent Script
-----------------
. childscript.ksh arg &
wait
echo $a -------------------- This echo statement is not getting displayed?
exit 0 Any simple idea
---------------

Child script
------------

echo $#
echo $1
a=10
export a
exit 0
-------------------------

Can you explain this in a bit simple manner?
# 4  
Old 11-14-2008
exported variables are inherited from parent to child and not the other way round.
Tough there is always a way shell ist not designed to do inter Process communication (IPC) this is normaly done by shared memory access or Unix domain sockets.
your example is quite simple bcs the child ends very fast.
echo $(./child.sh)
but if you want to acces data from a running child it gets complicated.
you may let the child write to a file or fifo which is read by the parrent.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Issue with tracking successful completion of Child process running in background

Hello All, I am using Linux. I have two scripts: inner_script.ksh main_wrapper_calling_inner.ksh Below is the code snippet of the main_wrapper_calling_inner.ksh: #!/bin/ksh ppids=() ---> Main array for process ids. fppids=() ---> array to capture failed process ids. pcnt=0 --->... (5 Replies)
Discussion started by: dmukherjee
5 Replies

2. Shell Programming and Scripting

Export variable to another script running in background

i have a script inside which i have generated a background job which will run another script. How do i export the variables from parent script to the child script which wil run in the background . a.sh:- export tmpdir="/usr/tmp" nohup b.sh& b.sh:- echo $tmpdir But... (1 Reply)
Discussion started by: millan
1 Replies

3. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

4. Shell Programming and Scripting

Export variable, as a background process

Have a script where I need to wait for a process to run, but from that process I want to capture the row count. Script I have so far (easier than explanation): echo "Start" export NUMROWS=`td "select * from dbc.database" 2> /dev/null | grep "Query completed" | sed -e 's/.*Query... (7 Replies)
Discussion started by: Cranie
7 Replies

5. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

6. Shell Programming and Scripting

Makefile: Parent - Child Inheritance and export

Hi, I have a number of Makefiles, including a couple of files that I include in Makefiles, a few scripts that are executed through Makefiles, and I have problems with environment variables that are not inherited to the scripts properly. Simplified scenario: rootdir/Makefile: all: ... (1 Reply)
Discussion started by: Shompis
1 Replies

7. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

8. UNIX for Advanced & Expert Users

send a new value to a variable in a running background process

Hi guys, I have a issue with a background process, I need to update the value of a variable in that process which is running at this time and it will be running for at least 2 days. Any idea? I will apreciate your help. regards. Razziel. (2 Replies)
Discussion started by: razziel
2 Replies

9. UNIX for Advanced & Expert Users

how to make a parent wait on a child shells running in background?

Hi I have a shell script A which calls another 10 shell scripts which run in background. How do i make the parent script wait for the child scripts complete, or in other words, i must be able to do a grep of parent script to find out if the child scripts are still running. My Code: ... (1 Reply)
Discussion started by: albertashish
1 Replies

10. Solaris

how to capture oracle export log while running as background process

I ran the Oracle 9i export command from a terminal to export out a big table using "exp andrew/password file=andrew.dmp log=andrew.log" From the terminal I can see that the export is running as there is some output from the oracle export job. The export job is not complete yet. When i go check... (4 Replies)
Discussion started by: hippo2020
4 Replies
Login or Register to Ask a Question