[SOLVED] Problem in wait statement


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] Problem in wait statement
# 1  
[SOLVED] Problem in wait statement

Iam having a script which is used to load users and dumpfile in any given schema.Iam trying to autolog the script and have added two fucntion in it.
Code:
function init_stdout_redirect {
    OUT_LOG=$1
    OUT_PIPE=$(mktemp -u)
    # Create the output pipe
    mkfifo $OUT_PIPE
    # Save stdout and stderr as 3 and 4
    exec 3>&1 4>&2
    # Tee the output pipe into the log file
    tee -a $OUT_LOG < $OUT_PIPE >&3 &
    # Save tee's pid
    TEE_PID=$!
    # Redirect the output to the pipe
    exec > $OUT_PIPE 2>&1
}
###########################################################################
# restore_stdout
#
# Restore standard output
###########################################################################
function restore_stdout {
    exec 1>&3 3>&- 2>&4 4>&-
    wait $TEE_PID
    rm -f $OUT_PIPE
}

I have added these two function in the main script.The main script is having two wait command in it.As soon i run the script after adding and calling these two function the script is getting hanged after encoutering the first wait statement of main script.

When i try to check where it is getting stuck i found the process id and saw that it get stuck on process tee -a log file name..Iam not sure why it is getting stuck as it should come out of it. Could someone please help me on it.

Last edited by Scott; 11-29-2012 at 07:47 AM.. Reason: Please use code tags
# 2  
A FIFO blocks until both ends have been opened. You can't do it one at a time; if you open the read-end first it will wait for the writer, if you open the write-end first it will wait for the reader.

You'll have to put one of the processes in the background first before you can open the other end.
# 3  
Hello Corona

Actually these are the standard fucntion which we are using in all other scripts where it is working fine. Here the difference is that the main script is having two additional wait statement. I am already running the process in background as you could see in the function.
Code:
tee -a $OUT_LOG < $OUT_PIPE >&3 &

Once all the task is done I check the process and found that process which is not terminated is the one which is teeing the log.
Code:
tee -a logfilename

So Iam not sure where it is getting stuck.Could you Adivse me if their is soemthing wrong in this function.

Thanks in Advance.

Last edited by Franklin52; 11-30-2012 at 04:40 AM.. Reason: Please use code tags for data and code samples
# 4  
That's the thing, it's not really running in the background -- yet. tee doesn't open $OUT_PIPE, redirections are the shell's job. Meaning, before the command gets put into the background, the shell has to open the fifo first. When it tries, it will deadlock, waiting for the other end to be opened.

So you need an entire background shell, not just a background process. It will open the fifo for tee, and your own shell will not need to wait for it. Once the files are opened, the extra shell's not needed and can leave, which is why I use exec instead of running tee directly -- it replaces that extra shell after it's done.

Code:
( exec tee -a $OUT_LOG < $OUT_PIPE >&3 ) &

# 5  
Hi Corona,

I have tried that too but still getting the same issue. I have attched the entire Script. If possible please have a look at it.Somehow the scrit is getting hanged on the first wait statment in main script and when i check which process is going on its showing the Tee process only.

Thanks
Vikram
# 6  
No, you have not. You did the exact same thing you did before. I suggest trying what I suggested above, for the reasons I stated above:

Code:
( exec tee -a $OUT_LOG < $OUT_PIPE >&3 ) &

# 7  
Hi Corona,

I have tried the code changes which you asked me to try but still it got stuck on 1st wait process of the main script. Iam using this fucntion in other scripts but their it is perfectly working fine. Only for this function iam facing issue.

Iam using this code only
Code:
 # Tee the output pipe into the log file
   ( exec tee -a $OUT_LOG < $OUT_PIPE >&3 ) &


I am running this code in debug mode and attaching the same.

Thanks
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #10
Difficulty: Easy
Charles Babbage was a British mathematician and inventor, known as the 'Father of the Computer'. He designed a mechanical computer called the Analytical Engine which was an early forerunner of the modern computer.
True or False?

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Plink wait problem

Hi, I have run into a problem to which i can't seem to find any solution, posting here is my last resort. Problem: I am using plink to access my router and run a few configuration commands. When in enter configurations mode, instead of sending next command plink keeps on waiting for manual... (7 Replies)
Discussion started by: zaainabbas
7 Replies

2. Shell Programming and Scripting

[Solved] Help with shell Script ,wait for some files for some time??

Hi All, I have the requirement that ,i have to write a shell script that job has to wait for a 7 touch files created by another application for 4 hours, if i get all 7 touch files ,i have to send a mail that i jobs are completed, if if it is waiting for more than 4 hours i have to send a mail... (2 Replies)
Discussion started by: Pradeep Shetty
2 Replies

3. Shell Programming and Scripting

[Solved] If statement in bash

I have the following code in bash, however "set red frmt" is not displayed. echo "iarg_rd = $iarg_rd" iarg_rd="2" if ; then echo "Hello World" fi if ; then frmt="${gap}${!frmt_titl_yl}" elif ; then frmt="${gap}${!frmt_titl_bk}" elif ; then echo... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Shell Programming and Scripting

[Solved] While read line and if statement not working

I'm looking for some help in figuring why my little bit of code will not process any entries other then the first one in my list. while read line ;do hostname=${line//\"} a=`ssh user@$hostname uptime;echo $?` if ];then dt=`date` touch... (6 Replies)
Discussion started by: whegra
6 Replies

5. Shell Programming and Scripting

[Solved] FOR loop / IF statement returning error

The code at the bottom is a simplified example of what we have. If I use the following: && echo "echo failed" $? returns 1 When I use if ; then echo "echo failed" ; fi $? returns 0 Does anyone know what's wrong with this? Using AIX 6.1 and KSH for NUM in 1 2 3 do ... (5 Replies)
Discussion started by: jfxdavies
5 Replies

6. Shell Programming and Scripting

[Solved] 0403-057 Syntax error for if statement

I am getting the following error when I am running a script in ksh when trying to execute an if statement comparing two numerical values tstmb.sh: 1.5321e+08: 0403-057 Syntax error Below is my code snippet. #!/bin/ksh set -x TODAY=$(date +%y%m%d) for file in $(ls -rt *.log | tail... (11 Replies)
Discussion started by: kiran1112
11 Replies

7. Shell Programming and Scripting

[Solved] Strange Problem in case statement while shift

Hi, Here is my code as below: test.ksh: ======= #!/bin/ksh option="${1}" while do case $1 in -f) FILE="${2}" echo "File name is $FILE" ;; -d) DIR="${2}" echo "Dir name is $DIR" ;; -*) echo "`basename ${0}`:usage: | " (5 Replies)
Discussion started by: zaq1xsw2
5 Replies

8. Shell Programming and Scripting

plz, i wait your help, AWK problem

I have tracefile of three nodes (0 , 1 and 2 ) as follows: + 0.02 0 1 tcp 40 ------- 1 0.0 2.0 0 0 - 0.02 0 1 tcp 40 ------- 1 0.0 2.0 0 0 + 0.02 2 1 tcp 40 ------- 2 2.1 0.1 0 1 - 0.02 2 1 tcp 40 ------- 2 2.1 0.1 0 1 r 0.025032 0 1 tcp 40 ------- 1 0.0 2.0 0 0 + 0.025032 1 2 tcp 40 -------... (11 Replies)
Discussion started by: ASAADAOUI
11 Replies

9. Shell Programming and Scripting

wait problem

Hello, I have been trying to figure out why the wait isnt waiting for the sleep process to complete till now and have found out that since sleep runs as different process and not a child process the wait isnt waiting. script: cat test|while read i do echo $i sleep 30 & done wait ps... (4 Replies)
Discussion started by: wannalearn
4 Replies

10. UNIX for Dummies Questions & Answers

I/O wait Problem

When running top, I notice a bit more I/O wait time than usual. Is there a tool or piece of software out there that can me help evaluate the performance of these operations on my machine? Thanks! (5 Replies)
Discussion started by: unavb
5 Replies

Featured Tech Videos