* Asynchronous shell command that leaves a background process running
@ 2010-10-20 20:51 Sean McAfee
2010-10-20 22:43 ` Andreas Politz
2010-10-21 3:41 ` Pascal J. Bourguignon
0 siblings, 2 replies; 7+ messages in thread
From: Sean McAfee @ 2010-10-20 20:51 UTC (permalink / raw)
To: help-gnu-emacs
I've written a shell script which is essentially a single exec command:
#!/bin/bash
exec real-program fixedarg1 fixedarg2 "$@"
"real-program" chugs along for several seconds, printing some status
messages in the meantime, before finally forking off a background
process and exiting. The background process manifests a window on my
desktop.
All is well when I run this command from an interactive shell, but
things fall apart when I try to run it as an asynchronous shell command
from Emacs, a la:
(shell-command "wrapper-script arg1 arg2 &")
I see the status messages appear in the *Async Shell Command* buffer,
but when the command exits, no desktop window appears.
The only fix I've found is to execute real-program in a separate process
group:
#!/bin/bash
perl -e 'if (fork() == 0) { setpgrp; exec @ARGV } wait' \
real-program fixedarg1 fixedarg2 "$@"
I guess Emacs is sending a fatal signal (probably SIGHUP) to the
asynchronous shell's process group after the main process exits.
Naturally I don't want to have to write my scripts defensively against
the possibility of running asynchronously from Emacs. So I can just
move my perl/setpgrp wrapper inside Emacs:
(shell-command "perl -e 'if (fork() == 0) { setpgrp; exec @ARGV } wait' wrapper-script arg1 arg2 &")
That's pretty ugly, though. A little Googling turned up the nohup
command, which also mostly does the job:
(shell-command "nohup wrapper-script arg1 arg2 | cat &")
The "| cat" is necessary to prevent nohup from redirecting output to
nohup.out. That's still ugly, and I also get the distracting header
"nohup: ignoring input and redirecting stderr to stdout" prepended to my
normal output.
Is there a more elegant way to address this problem?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Asynchronous shell command that leaves a background process running
2010-10-20 20:51 Asynchronous shell command that leaves a background process running Sean McAfee
@ 2010-10-20 22:43 ` Andreas Politz
2010-10-21 17:48 ` Sean McAfee
2010-10-21 3:41 ` Pascal J. Bourguignon
1 sibling, 1 reply; 7+ messages in thread
From: Andreas Politz @ 2010-10-20 22:43 UTC (permalink / raw)
To: help-gnu-emacs
Sean McAfee <eefacm@gmail.com> writes:
> I've written a shell script which is essentially a single exec command:
>
> #!/bin/bash
> exec real-program fixedarg1 fixedarg2 "$@"
>
> "real-program" chugs along for several seconds, printing some status
> messages in the meantime, before finally forking off a background
> process and exiting. The background process manifests a window on my
> desktop.
>
> All is well when I run this command from an interactive shell, but
> things fall apart when I try to run it as an asynchronous shell command
> from Emacs, a la:
>
> (shell-command "wrapper-script arg1 arg2 &")
>
> I see the status messages appear in the *Async Shell Command* buffer,
> but when the command exits, no desktop window appears.
>
> The only fix I've found is to execute real-program in a separate process
> group:
>
> #!/bin/bash
> perl -e 'if (fork() == 0) { setpgrp; exec @ARGV } wait' \
> real-program fixedarg1 fixedarg2 "$@"
>
> I guess Emacs is sending a fatal signal (probably SIGHUP) to the
> asynchronous shell's process group after the main process exits.
>
> Naturally I don't want to have to write my scripts defensively against
> the possibility of running asynchronously from Emacs. So I can just
> move my perl/setpgrp wrapper inside Emacs:
>
> (shell-command "perl -e 'if (fork() == 0) { setpgrp; exec @ARGV } wait' wrapper-script arg1 arg2 &")
>
> That's pretty ugly, though. A little Googling turned up the nohup
> command, which also mostly does the job:
>
> (shell-command "nohup wrapper-script arg1 arg2 | cat &")
>
> The "| cat" is necessary to prevent nohup from redirecting output to
> nohup.out. That's still ugly, and I also get the distracting header
> "nohup: ignoring input and redirecting stderr to stdout" prepended to my
> normal output.
>
> Is there a more elegant way to address this problem?
Try this.
(let ((process-connection-type nil)) ; Use a pipe instead of pty
(shell-command "foo bar blub &"))
I think what we are talking about are `orphaned children', whose session
leader died and who are automatically sent a SIGHUP as part of libc (not
Emacs).
-ap
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Asynchronous shell command that leaves a background process running
2010-10-20 20:51 Asynchronous shell command that leaves a background process running Sean McAfee
2010-10-20 22:43 ` Andreas Politz
@ 2010-10-21 3:41 ` Pascal J. Bourguignon
2010-10-21 19:24 ` Sean McAfee
1 sibling, 1 reply; 7+ messages in thread
From: Pascal J. Bourguignon @ 2010-10-21 3:41 UTC (permalink / raw)
To: help-gnu-emacs
Sean McAfee <eefacm@gmail.com> writes:
> I've written a shell script which is essentially a single exec command:
>
> #!/bin/bash
> exec real-program fixedarg1 fixedarg2 "$@"
>
> "real-program" chugs along for several seconds, printing some status
> messages in the meantime, before finally forking off a background
> process and exiting. The background process manifests a window on my
> desktop.
>
> All is well when I run this command from an interactive shell, but
> things fall apart when I try to run it as an asynchronous shell command
> from Emacs, a la:
>
> (shell-command "wrapper-script arg1 arg2 &")
> [...]
> Is there a more elegant way to address this problem?
You can use:
(shell-command "wrapper-script arg1 arg2 & disown")
But really, the problem is with your real-program, which should be doing
the same as disown (setting a new process group, etc), since it exits
before its children.
--
__Pascal Bourguignon__ http://www.informatimago.com/
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Asynchronous shell command that leaves a background process running
2010-10-20 22:43 ` Andreas Politz
@ 2010-10-21 17:48 ` Sean McAfee
2010-10-21 18:22 ` Andreas Politz
0 siblings, 1 reply; 7+ messages in thread
From: Sean McAfee @ 2010-10-21 17:48 UTC (permalink / raw)
To: help-gnu-emacs
Andreas Politz <politza@fh-trier.de> writes:
> Sean McAfee <eefacm@gmail.com> writes:
>> (shell-command "nohup wrapper-script arg1 arg2 | cat &")
[...]
>> Is there a more elegant way to address this problem?
> Try this.
>
> (let ((process-connection-type nil)) ; Use a pipe instead of pty
> (shell-command "foo bar blub &"))
Nice! Thanks. Shortly after posting my original article, I factored
out the new-process-group logic into a short wrapper program, so I could
just say:
(shell-command "in-a-new-process-group wrapper-script arg1 arg2 &")
That was nicer than anything I'd yet come up with, but your solution is
even better.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Asynchronous shell command that leaves a background process running
2010-10-21 17:48 ` Sean McAfee
@ 2010-10-21 18:22 ` Andreas Politz
2010-10-22 18:34 ` Sean McAfee
0 siblings, 1 reply; 7+ messages in thread
From: Andreas Politz @ 2010-10-21 18:22 UTC (permalink / raw)
To: help-gnu-emacs
Sean McAfee <eefacm@gmail.com> writes:
> Andreas Politz <politza@fh-trier.de> writes:
>> Sean McAfee <eefacm@gmail.com> writes:
>>> (shell-command "nohup wrapper-script arg1 arg2 | cat &")
> [...]
>>> Is there a more elegant way to address this problem?
>
>> Try this.
>>
>> (let ((process-connection-type nil)) ; Use a pipe instead of pty
>> (shell-command "foo bar blub &"))
>
> Nice! Thanks. Shortly after posting my original article, I factored
> out the new-process-group logic into a short wrapper program, so I could
> just say:
>
> (shell-command "in-a-new-process-group wrapper-script arg1 arg2 &")
>
> That was nicer than anything I'd yet come up with, but your solution is
> even better.
I think Pascals point is still valid: Why can't your script wait for
it's children to die ?
-ap
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Asynchronous shell command that leaves a background process running
2010-10-21 3:41 ` Pascal J. Bourguignon
@ 2010-10-21 19:24 ` Sean McAfee
0 siblings, 0 replies; 7+ messages in thread
From: Sean McAfee @ 2010-10-21 19:24 UTC (permalink / raw)
To: help-gnu-emacs
pjb@informatimago.com (Pascal J. Bourguignon) writes:
> Sean McAfee <eefacm@gmail.com> writes:
>> All is well when I run this command from an interactive shell, but
>> things fall apart when I try to run it as an asynchronous shell command
>> from Emacs, a la:
>>
>> (shell-command "wrapper-script arg1 arg2 &")
>> [...]
>> Is there a more elegant way to address this problem?
> You can use:
>
> (shell-command "wrapper-script arg1 arg2 & disown")
I tried variations on that, but couldn't find any way to run
wrapper-script asynchronously, receiving its status messages in Emacs as
it runs.
> But really, the problem is with your real-program, which should be doing
> the same as disown (setting a new process group, etc), since it exits
> before its children.
That's problematic, since real-program is a Java program.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Asynchronous shell command that leaves a background process running
2010-10-21 18:22 ` Andreas Politz
@ 2010-10-22 18:34 ` Sean McAfee
0 siblings, 0 replies; 7+ messages in thread
From: Sean McAfee @ 2010-10-22 18:34 UTC (permalink / raw)
To: help-gnu-emacs
Andreas Politz <politza@fh-trier.de> writes:
> Sean McAfee <eefacm@gmail.com> writes:
>> Andreas Politz <politza@fh-trier.de> writes:
>>> Try this.
>>>
>>> (let ((process-connection-type nil)) ; Use a pipe instead of pty
>>> (shell-command "foo bar blub &"))
>> Nice! Thanks.
> I think Pascals point is still valid: Why can't your script wait for
> it's children to die ?
Well, because one of the children is a window-manifesting program that
might persist for many hours.
More specifically, what I've been calling "real-program" is a Java
program that interacts with an intranet web server, downloads a JNLP
file, and invokes Java Web Start on that file.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2010-10-22 18:34 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-20 20:51 Asynchronous shell command that leaves a background process running Sean McAfee
2010-10-20 22:43 ` Andreas Politz
2010-10-21 17:48 ` Sean McAfee
2010-10-21 18:22 ` Andreas Politz
2010-10-22 18:34 ` Sean McAfee
2010-10-21 3:41 ` Pascal J. Bourguignon
2010-10-21 19:24 ` Sean McAfee
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.