1846552 Members
2586 Online
110256 Solutions
New Discussion

Re: Script hanging

 
Jeff_Traigle
Honored Contributor

Script hanging

This is similar to what I saw happen in another script, but it's at least happening in a self-contained script this time instead of one I wrote being called from one that I didn't.

What I wanted to add was the ability to log the entire script run. The general syntax of the script is:

#!/sbin/sh
(
if [ -f /onsite ];
then

else

fi
) | tee -a ${LOGFILE}

All of the commands execute, the script just doesn't exit. If I take the pipe to tee out, it exits fine, even with the subshell wrapped around the commands. I tried putting an explicit exit at the end of the subshell, but it still hung.

The odd part is that another script I have that I added the "() | tee" wrapping in functions perfectly well and exits normally as it should.

Bug? feature? Something subtle I'm just being clueless about?
--
Jeff Traigle
4 REPLIES 4
James A. Donovan
Honored Contributor

Re: Script hanging

Try adding "set -vx" and see if you can spot where it's hanging.

#!/sbin/sh
set -vx
(
...
Remember, wherever you go, there you are...
Jeff_Traigle
Honored Contributor

Re: Script hanging

Argh!

Ok... I added an explicit exit at the end of all the commands in the if and the else... it exited. Fine and dandy except, out of curiosity, I took them back out and left it with the original syntax... and it appears to be exiting consistently that way too now. Hmmm. Some days...
--
Jeff Traigle
Jeff_Traigle
Honored Contributor

Re: Script hanging

Well, I tried putting the set -vx in there, but it didn't seem to do anything as far as showing the commands executing. However, I put troubleshooting echo messages to see how far it's getting when it hangs (which it is doing again consistently on another test system after it worked fine).

#!/sbin/sh
(
if [ -f /onsite ];
then

echo "Done with configs"
else

fi

echo "Out of the if statement"
) | tee -a ${LOGFILE}

It prints both messages so everything is completing within the subshell, but for some reason simply isn't exiting.
--
Jeff Traigle
Jeff_Traigle
Honored Contributor

Re: Script hanging

Found the following from a couple of years ago regarding a similar problem in the System Administration Forum:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=24111

Though it's dealing with tee hanging when the poster was trying to spawn background processes from his script, the symptom is exactly the same as what I'm seeing. Since I'm starting NFS, as elaborated below, with the init scripts, I suppose it could be open file descriptors as Steve Steel mentioned there, but I don't see why it would hang sometimes and not others (seemingly randomly) if that was the case.

#!/sbin/sh
(
if [ -f /onsite ];
then

/sbin/init.d/nfs.core start
/sbin/init.d/nfs.client start
/sbin/init.d/nfs.server start;
else

fi
) | tee -a ${LOGFILE}

I'm trying to change it so the NFS scripts redirect their output to /dev/null (since I really don't care to see those messages). Will that be sufficient to guarantee closing the pipe or am I not getting the full gist of Steve Steel's explanation?
--
Jeff Traigle