# /bin/sh exec & pipe



## izotov (Feb 3, 2010)

Hi,
I would like to start a program with logging from an sh script. I created two test scripts:

starter.sh:

```
#!/bin/sh
exec ./producer.sh | logger
```
producer.sh:

```
#!/bin/sh
while :
do
        echo "hello"
        sleep 5
done
```

My problem is that when I start starter.sh it's process remains in the system and additional processes for producer.sh and logger are created.

If I do 

```
exec ./producer.sh >> /var/log/messages
```
instead of

```
exec ./producer.sh | logger
```
then it is OK, I cannot see a process for starter.sh.

The original version should work with the pipe to logger. How could I make it work?

Thanks for any help!


----------



## SirDice (Feb 3, 2010)

From sh(1):


> *exec [command [arg ...]]*
> Unless command is omitted, the shell process is replaced with the specified program (which must be a real program, not a shell built-in command or function).  Any redirections on the exec command are marked as permanent, so that they are not undone when the exec command finishes.



Just remove the exec from your starter script.


----------



## izotov (Feb 3, 2010)

SirDice said:
			
		

> From sh(1):
> 
> 
> Just remove the exec from your starter script.



I was not clear enough: I do not want starter.sh to be in the system once it started "./producer.sh | logger".
It behaves the same from this point of view with or without exec: ps shows a process for starter.sh (and I do not want it to be there). With no exec it is the expected behaviour I guess. 
I would like to find a solution to start "./producer.sh | logger" and the starting scrip should not be in the system after that.


----------



## J65nko (Feb 3, 2010)

Are you aware of Dan Bernstein's daemontools? Read http://cr.yp.to/daemontools/faq.html to see whether it provides what you are looking for.


----------



## expl (Feb 3, 2010)

Piping requires parent process to be running to stream the I/O via pipe. In this case the parent is starter.sh.


----------



## izotov (Feb 4, 2010)

expl said:
			
		

> Piping requires parent process to be running to stream the I/O via pipe. In this case the parent is starter.sh.


So there is no way to avoid this "feature"... Thanks!


----------



## ta0kira (Feb 7, 2010)

Try `exec ./producer.sh | logger &`.
Kevin Barry


----------



## izotov (Feb 8, 2010)

ta0kira said:
			
		

> Try `exec ./producer.sh | logger &`.
> Kevin Barry


This seem to do it!  Thanks!


----------

