From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Sean McAfee Newsgroups: gmane.emacs.help Subject: Asynchronous shell command that leaves a background process running Date: Wed, 20 Oct 2010 13:51:56 -0700 Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1291897207 28007 80.91.229.12 (9 Dec 2010 12:20:07 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 9 Dec 2010 12:20:07 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 09 13:19:58 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PQfT7-00029H-9V for geh-help-gnu-emacs@m.gmane.org; Thu, 09 Dec 2010 13:19:29 +0100 Original-Received: from localhost ([127.0.0.1]:44969 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQfT6-0005pF-AB for geh-help-gnu-emacs@m.gmane.org; Thu, 09 Dec 2010 07:19:28 -0500 Original-Path: usenet.stanford.edu!postnews.google.com!news1.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.supernews.com!news.supernews.com.POSTED!not-for-mail Original-NNTP-Posting-Date: Wed, 20 Oct 2010 15:51:56 -0500 Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:2/2CGow4GNE/c3CpE9yVWRcWXy4= Original-Lines: 46 Original-X-Trace: sv3-bL5QxYHMy22QLV82EyUAuZYeOsvZlEeSuCqnou0fGkd9OC0NwAYaDvXAXEnvG1UEE68JKQU52dKQFmM!zKc9oOQCCS9zEpXyM08WUQGzQplSo4JQQecL5Q+4FaKVREGFFVWNHblAlFcV9W5jn38U4D/Ki0m9!J/s6i8sDd3wKrM5azZ8p3zKn Original-X-Complaints-To: www.supernews.com/docs/abuse.html X-DMCA-Complaints-To: www.supernews.com/docs/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2859 Original-Xref: usenet.stanford.edu gnu.emacs.help:181903 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:77022 Archived-At: 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?