all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Sean McAfee <eefacm@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Asynchronous shell command that leaves a background process running
Date: Wed, 20 Oct 2010 13:51:56 -0700	[thread overview]
Message-ID: <bp81v7kk2oj.fsf@usca1uw-JZWWPM1.sanmateo.corp.akamai.com> (raw)

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?


             reply	other threads:[~2010-10-20 20:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-20 20:51 Sean McAfee [this message]
2010-10-20 22:43 ` Asynchronous shell command that leaves a background process running 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bp81v7kk2oj.fsf@usca1uw-JZWWPM1.sanmateo.corp.akamai.com \
    --to=eefacm@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.