Operating System - HP-UX
1820475 Members
3051 Online
109624 Solutions
New Discussion юеВ

function used to create background task hangs on exit when |tee present

 
SOLVED
Go to solution
Randy Goss
Occasional Contributor

function used to create background task hangs on exit when |tee present

I created a ksh script "x" which calls a function "dwit". dwit creates several background tasks using:
nohup /cpf/bin/fms &

When I execute dwit in the following formats it works:
dwit
dwit > log
dwit >log 2>&1

When I use the |tee command:
dwit |tee log
dwit 2>&1 |tee log
dwit |tee log 2>&1
the dwit command hangs after executing the last command which is "exit".

I have to stop the "x" script with a ctrl-c.

I have removed the background creation from dwit and the process works fine, so the problem is related to background and |tee.


3 REPLIES 3
Steve Steel
Honored Contributor
Solution

Re: function used to create background task hangs on exit when |tee present

Hi

If tee hangs try script

script file
dwit
exit

Everything that happens goes to the file in copy.

The tee hang is probably caused by this.

Any process's children inherit the parent's open file descriptors. If the child does not close any them they stay open until the process exits and will be passed to
their children in a similar mannor.

A pipe, does not deliver an end-of-file indicator to its reading end until all processes that have opened it for writing have closed their file descriptors.

Thus tee, or any other command using the pipe in a comparable way,
will not terminate until all descendents of the process that inherited the pipe as the standard output have closed it or terminated.

During a hang, the program lsof may be used to
determine which processes have the pipe open for reading or writing.
Also checking the output from a ps command will show processes that are running.

Steve Steel


If you want truly to understand something, try to change it. (Kurt Lewin)
Peter Kloetgen
Esteemed Contributor

Re: function used to create background task hangs on exit when |tee present

Hi Randy,

if your function dwit is the last command line in your script then try to call it like this:

script_name | tee log 2>&1

this should do it for you.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Randy Goss
Occasional Contributor

Re: function used to create background task hangs on exit when |tee present

Steve, your clarification of my problem enabled me to fix it.

The tasks I start from the function "dwit" run continuously until a system reboot or I kill them manually, therefore they don't close output to the pipe.

The solution:
dwit now initiates the tasks as:

nohup task >tasklog 2>&1 &

by redirecting the child task's output to tasklog the pipe closes.

thanks.