Export variable, as a background process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Export variable, as a background process
# 1  
Old 08-24-2010
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):

Code:
echo "Start"
export NUMROWS=`td "select * from dbc.database" 2> /dev/null | 
    grep "Query completed" | sed -e 's/.*Query completed. //g' -e 's/row.*found.*//g'` & 
WAITPID=$!
echo $NUMROWS
ps -ef | grep $WAITPID
wait $WAITPID
echo $NUMROWS

What I need is the export to run, in the background, the script to wait for that to run and then return the row count (this is the start of a script which will kill the WAITPID and processes associated if it runs for longer than a specified time.

Would be easy if I didn't need the export... ideas?

I could use temp files, but would rather not.

Last edited by Cranie; 08-24-2010 at 08:00 AM..
# 2  
Old 08-24-2010
Quote:
Originally Posted by Cranie
[...]
Would be easy if I didn't need the export... ideas?
What's the problem with exporting the variable?
# 3  
Old 08-24-2010
I can't, the "td" command connects to the database and returns the rows. Initially I had:

Code:
export NUMROWS=`td "select * from dbc.database" 2> /dev/null | 
    grep "Query completed" | sed -e 's/.*Query completed. //g' -e 's/row.*found.*//g'`

That works. What I need is to run that in the background as it can take minutes or hours to run the SQL, if after, say in our test, 30minutes, this process is still running I want to kill it. So need the & to run in background. But without writing the results to a file can't see any obvious way of doing it...
# 4  
Old 08-24-2010
Slightly off topic. A shell environment variable is not really suitable to receive an open-ended report. It would be better as a file.

To reduce the size of the report have you considered using:
Code:
select count (*)

# 5  
Old 08-24-2010
@methyl this is a perf test suite of scripts and this "test" is to simulate Teradata, and network performance to the ETL box.

So currently have a script to run X predefined queries for Y minutes. Queries are re-submitted after they complete until Y minutes is up. Any running at this point stay running and have to be manually forced off. So am trying to re-code this process.

The reports are then generated from the number of queries run, run time for each, a seperate report is done from the log tables in the database. All this is then put in a report database available to managers after any major changes.

Think the solution is going to be pipe the rows to awk, generate the print statement into a temp file, (export NUMROWS=402) and then run that file to get that figure. bit more hassle but easy to implement. :-)

---------- Post updated at 08:23 AM ---------- Previous update was at 08:14 AM ----------

Crappy code but works:

Code:
/tmp/td "select * from dbc.databases" 2> /dev/null | grep "Query completed" | sed -e 's/.*Query completed. //g' -e 's/row.*found.*//g' | awk ' { print "export NUMROWS=" $0 } ' > tmp_rpt &
WAIT_PID=$!
 
while [ "`ps -ef | grep ${WAIT_PID} | grep -v grep`" != "" ]
do
    sleep 10
done
 
. ./tmp_rpt
rm ./tmp_rpt
 
echo "1: " $NUMROWS

So in my sleep 10 will have a if time = Y then kill the $WAIT_PID. Now to add this into the main script :-)
# 6  
Old 08-24-2010
What shell you're using? ksh93 and zsh support coprocesses.

This is with dtksh (/usr/dt/bin/dtksh)on Solaris:

Code:
$ unset v
$ sqlplus -s / as sysdba<<-! |&
  set feed off pages 0
  exec dbms_lock.sleep(5);
  select sysdate from dual;
!> > > >
[1]     20027
$ read -p v
$
[1] +  Done    sqlplus -s / as sysdba<<-! |& ;  set feed off pages 0;  exec dbms_lock.sleep(5);;  select sysdate from dual;;!
$ echo "$v"
24-AUG-10



---------- Post updated at 03:34 PM ---------- Previous update was at 03:32 PM ----------

Another option is to use expect as a wrapper, to set a timeout for the process.

Last edited by radoulov; 08-24-2010 at 11:01 AM..
This User Gave Thanks to radoulov For This Post:
# 7  
Old 08-24-2010
Or we can use the basic shell "jobs" command. Obviously we would need to build a counter into the loop to allow us to stop after a given amount of time.


Code:
while [ "`jobs`" ]
do
        echo "still running"
        sleep 10
done


Tip. The "ps -ef" command sequence posted assumes unique matches for the "grep". You are very likely to get false matches.
Confining the search to one pid with "ps -fp<pid>" would be safer and quicker.
Actually for anything mission critical I would never rely on "ps -ef" because it occasionally misses processes (or does not reply at all) when the kernel is busy.
This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 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

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

4. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

5. 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

6. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: aixjadoo
3 Replies

7. 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

8. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies

9. Shell Programming and Scripting

background process

Hi, In shell script when I use script1 >> filelog the echo statments of script1 gets printed in the filelog but when I try to run script in background i.e, script1 & >> filelog nothing gets printed in the filelog. Anybody knows whats going on here. thanks (3 Replies)
Discussion started by: k_oops9
3 Replies

10. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies
Login or Register to Ask a Question