Christian Ebert | 18 Jun 2011 15:15
X-Face
Picon
Gravatar

backticks: how to put process into background?

Hi,

Is there a way to force a process inside backticks into
background? `&` seems to have no effect.

Example: I'm trying to migrate a filter to display (X-)Faces -
used as display filter in mutt - from procmail to maildrop.
Everything works except putting the viewer in the background:

# customize X11 viewer (has to accept stdin)
VIEWER="/sw/bin/xv -geometry 100x100 -wait 3 -"

# adjust paths of required programs for speed
AWK="/usr/bin/awk"
REFORMAIL="/usr/local/bin/reformail"
OPENSSL="/usr/bin/openssl"
PRINTF="/usr/bin/printf"
UNCOMPFACE="/sw/bin/uncompface"

# check if $DISPLAY is non-empty
if ("$DISPLAY")
{
	if (/^Face:\s+(.{16,})/)
	{
		# work around broken headers
		# (re)create lines of width 79, removing all spaces
		# delete potentially appended X-Face (Claws-Mail)
		`$PRINTF '%s\n' $MATCH1 | $AWK '{ if ($1 == "X-Face:") exit; OFS = ""; $1 = $1; line = $0; do { print
substr(line, 0, 79); line = substr(line, 80); } while (length(line)); }' | $OPENSSL base64 -d | $VIEWER &`
		xfilter "$REFORMAIL -I'Face:'"
(Continue reading)

Sam Varshavchik | 18 Jun 2011 16:11
Gravatar

Re: backticks: how to put process into background?

Christian Ebert writes:

> Hi,
>
> Is there a way to force a process inside backticks into
> background? `&` seems to have no effect.

Of course it does. There's no reason why it wouldn't work, as the entire  
command is passed to the shell for execution, without any interpretation by  
maildrop.

However, maildrop creates a new process group, and the entire process group  
gets SIGHUPed when maildrop terminates, so that all processes that are  
involved in mail delivery get terminated, by default, when mail delivery is  
complete.

So, your process most certainly goes into the background, but gets sighupped  
when maildrop terminates a little bit later. Try nohuping your entire  
command.

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Courier-maildrop mailing list
(Continue reading)

Christian Ebert | 18 Jun 2011 22:30
X-Face
Picon
Gravatar

Re: backticks: how to put process into background?

> Christian Ebert writes:
>> Is there a way to force a process inside backticks into
>> background? `&` seems to have no effect.
> 
> Of course it does. There's no reason why it wouldn't work, as the
> entire command is passed to the shell for execution, without any
> interpretation by maildrop.
> 
> However, maildrop creates a new process group, and the entire process
> group gets SIGHUPed when maildrop terminates, so that all processes
> that are involved in mail delivery get terminated, by default, when
> mail delivery is complete.
> 
> So, your process most certainly goes into the background, but gets
> sighupped when maildrop terminates a little bit later. Try nohuping
> your entire command.

Good ... I just have not the slightest clue how to do that.

Thanks all the same.

c
--

-- 
\black\trash movie   _SAME  TIME  SAME  PLACE_
	       New York, in the summer of 2001

--->> http://www.blacktrash.org/underdogma/stsp.php

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
(Continue reading)

Christian Ebert | 20 Jun 2011 11:53
X-Face
Picon
Gravatar

Re: backticks: how to put process into background?

* Sam Varshavchik on Saturday, June 18, 2011 at 10:11:45 -0400
> Christian Ebert writes:
>> Is there a way to force a process inside backticks into
>> background? `&` seems to have no effect.
> 
> Of course it does. There's no reason why it wouldn't work, as the
> entire command is passed to the shell for execution, without any
> interpretation by maildrop.
> 
> However, maildrop creates a new process group, and the entire process
> group gets SIGHUPed when maildrop terminates, so that all processes
> that are involved in mail delivery get terminated, by default, when
> mail delivery is complete.
> 
> So, your process most certainly goes into the background, but gets
> sighupped when maildrop terminates a little bit later. Try nohuping
> your entire command.

Things I've tried:

`printf '%s\n' $MATCH1 | uncompface -X | nohup xv -geometry 100x100 -wait 3 - &`

`nohup (printf '%s\n' $MATCH1 | uncompface -X | xv -geometry 100x100 -wait 3 - &)`

and other combinations: they just do nothing, i.e. no x-face is
displayed.

When using the variables:

PRINTF="/usr/bin/printf"
(Continue reading)

Sam Varshavchik | 20 Jun 2011 13:07
Gravatar

Re: backticks: how to put process into background?

Christian Ebert writes:

> * Sam Varshavchik on Saturday, June 18, 2011 at 10:11:45 -0400
> > Christian Ebert writes:
> >> Is there a way to force a process inside backticks into
> >> background? `&` seems to have no effect.
> >
> > Of course it does. There's no reason why it wouldn't work, as the
> > entire command is passed to the shell for execution, without any
> > interpretation by maildrop.
> >
> > However, maildrop creates a new process group, and the entire process
> > group gets SIGHUPed when maildrop terminates, so that all processes
> > that are involved in mail delivery get terminated, by default, when
> > mail delivery is complete.
> >
> > So, your process most certainly goes into the background, but gets
> > sighupped when maildrop terminates a little bit later. Try nohuping
> > your entire command.
>
> Things I've tried:
>
> `printf '%s\n' $MATCH1 | uncompface -X | nohup xv -geometry 100x100 -wait 3  
> - &`
>
> `nohup (printf '%s\n' $MATCH1 | uncompface -X | xv -geometry 100x100 -wait 3  
> - &)`
>
> and other combinations: they just do nothing, i.e. no x-face is
> displayed.
(Continue reading)

Christian Ebert | 20 Jun 2011 13:59
X-Face
Picon
Gravatar

Re: backticks: how to put process into background?

* Sam Varshavchik on Monday, June 20, 2011 at 07:07:33 -0400

Thank you for your detailed response. I am aware that this is
more or less off-topic, so feel free to tell me to bugger off ;-)

> The argument to nohup is a command to execute. "(…)" is not a command,
> it is a shell pipeline construct. This is parsed and interpreted by
> the shell.
> 
> [mrsam <at> octopus maildrop]$ which nohup
> /usr/bin/nohup
> 
> nohup is a standalone command. Its first argument is the command to
> execute with SIGHUP disabled. The remaining arguments are passed
> through as the arguments to the command.
> 
> nohup does not incorporate an entire shell interpreter and parser
> inside of itself. As such, shell constructs, such as (pipeline) do not
> work when you pass it to nohup.
> 
> Your backtick command should probably be:
> 
> `nohup /bin/sh -c "command &"`

Yes, I forgot to mention that I also tried:

`nohup /bin/sh -c '$PRINTF "%s\n" $MATCH1 | $UNCOMPFACE -X | $VIEWER &'`

which behaves exactly like without nohup:

(Continue reading)

Sam Varshavchik | 20 Jun 2011 14:44
Gravatar

Re: backticks: how to put process into background?

Christian Ebert writes:

> >
> > Your backtick command should probably be:
> >
> > `nohup /bin/sh -c "command &"`
>
> Yes, I forgot to mention that I also tried:
>
> `nohup /bin/sh -c '$PRINTF "%s\n" $MATCH1 | $UNCOMPFACE -X | $VIEWER &'`
>
> which behaves exactly like without nohup:
>
> `$PRINTF '%s\n' $MATCH1 | $UNCOMPFACE -X | $VIEWER &`

Probably a syntax error somewhere.

Start with something simple:

`nohup /bin/sh -c 'xterm &'`

If, now, you get an xterm window, you can keep making the command longer  
until you get what you want.

It also occured to me that in order to get anything up on the display, the  
DISPLAY environment variable must be set.

maildrop resets all environment variables by default, in spawned commands. I  
presumed that you've imported the DISPLAY environment variable in your  
maildrop recipe, before you attempt to run any X client.
(Continue reading)

Christian Ebert | 20 Jun 2011 15:14
X-Face
Picon
Gravatar

Re: backticks: how to put process into background?

* Sam Varshavchik on Monday, June 20, 2011 at 08:44:43 -0400
> Christian Ebert writes:
>> Yes, I forgot to mention that I also tried:
>> 
>> `nohup /bin/sh -c '$PRINTF "%s\n" $MATCH1 | $UNCOMPFACE -X | $VIEWER &'`
>> 
>> which behaves exactly like without nohup:
>> 
>> `$PRINTF '%s\n' $MATCH1 | $UNCOMPFACE -X | $VIEWER &`
> 
> Probably a syntax error somewhere.
> 
> Start with something simple:
> 
> `nohup /bin/sh -c 'xterm &'`

Same behaviour as:

`xterm &`

and as:

`xterm`

i.e. the process is not going into the background, maildrop waits
for it to be terminated.

> If, now, you get an xterm window, you can keep making the command
> longer until you get what you want.

(Continue reading)

Sam Varshavchik | 20 Jun 2011 15:44
Gravatar

Re: backticks: how to put process into background?

Christian Ebert writes:

> `nohup /bin/sh -c '$PRINTF "%s\n" $MATCH1 | $UNCOMPFACE -X | $VIEWER &'`
> `$PRINTF '%s\n' $MATCH1 | $UNCOMPFACE -X | $VIEWER &`
> `$PRINTF '%s\n' $MATCH1 | $UNCOMPFACE -X | $VIEWER`
>
> > It also occured to me that in order to get anything up on the display,
> > the DISPLAY environment variable must be set.
>
> The detection of "$DISPLAY" in my recipe works fine.
>
> All works fine - except the background thingy.

strace explains what's happening. maildrop's backtick operator captures the  
output of the command, and all processes spawned by the backtick operator  
write to the same pipe on standard output, so maildrop waits for the pipe to  
close, which won't happen until all processes terminate.

Adding >/dev/null to the end fixes it.

import DISPLAY

`nohup /bin/sh -c "xterm &" >/dev/null`

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
(Continue reading)

Christian Ebert | 20 Jun 2011 17:17
X-Face
Picon
Gravatar

Re: backticks: how to put process into background?

* Sam Varshavchik on Monday, June 20, 2011 at 09:44:05 -0400
> strace explains what's happening.

grrh, Mac OS X doesn't provide it.

> maildrop's backtick operator captures the output of the
> command, and all processes spawned by the backtick operator
> write to the same pipe on standard output, so maildrop waits
> for the pipe to close, which won't happen until all processes
> terminate.
> 
> Adding >/dev/null to the end fixes it.
> 
> import DISPLAY
> 
> `nohup /bin/sh -c "xterm &" >/dev/null`

Yey! perfect, thank you.

After some shell quote tweaking, my little recipe looks like
this:

# customize X11 viewer (has to accept stdin)
VIEWER="/sw/bin/xv -geometry 100x100 -wait 3 -"
# adjust paths of required programs for speed
SHELL="/bin/sh"
CAT="/bin/cat"
AWK="/usr/bin/awk"
OPENSSL="/usr/bin/openssl"
NOHUP="/usr/bin/nohup"
(Continue reading)


Gmane