如何在后台执行shell命令?

问题描述:

下面是一个简单defun定义运行shell脚本:如何在后台执行shell命令?

(defun bk-konsoles() 
    "Calls: bk-konsoles.bash" 
    (interactive) 
    (shell-command (concat (expand-file-name "~/its/plts/goodies/bk-konsoles.bash ") 
         (if (buffer-file-name) 
          (file-name-directory (buffer-file-name))) 
         " &") 
        nil nil)) 

如果我启动一个程序,没有符号 - 它启动脚本,但块的emacs,直到我关闭程序,如果我不把&符号提示错误:

/home/boris/its/plts/goodies/bk-konsoles.bash /home/boris/scl/geekgeek/: exited abnormally with code 1. 

编辑

所以,现在我使用的是:

(defun bk-konsoles() 
    "Calls: bk-konsoles.bash" 
    (interactive) 
    (shell-command (concat (expand-file-name "~/its/plts/goodies/bk-konsoles.bash ") 
         (if (buffer-file-name) 
          (file-name-directory (buffer-file-name))) 
         " & disown") 
       nil nil) 
    (kill-buffer "*Shell Command Output*")) 

编辑2

都能跟得上 - 不工作:

(defun bk-konsoles() 
    "Calls: bk-konsoles.bash" 
    (interactive) 
    (let ((curDir default-directory)) 
    ;; (shell-command (concat "nohup " (expand-file-name "~/its/plts/goodies/bk-konsoles.bash ") curDir) nil nil) 
    (shell-command (concat (expand-file-name "~/its/plts/goodies/bk-konsoles.bash ") 
          curDir "& disown") nil nil) 
    (kill-buffer "*Shell Command Output*"))) 

保持emacs的忙 - 要么disown,或nohup

这里是我跑,如果它可能会有所帮助的脚本:bk-konsoles.bash

+2

提示:使用'async-shell-command'代替 – kindahero

+0

@kindahero - 'async-shell-command'只是在场景后面添加&符号。如果这有效,那么他发布的内容也会起作用。 – Inaimathi

+0

你可以像这样使用'disown':'your-command&diswon'。 – Daimrod

我认为问题是konsole。

(shell-command "xterm &") 

做你所期望的,在新窗口中打开xterm并返回控制权给Emacs。但是,

(shell-command "konsole &") 

立即打开和关闭konsole。 konsole开始的方式似乎导致了这个问题。我认为KDE应用程序有自己的启动应用程序系统,但我不确定。无论如何,我认为这个问题不在Emacs这边。

您可以使用nohupdisown这样的:

$ your_command & disown 
$ nohup your_command 

this post上stackexchange的差异的说明。

+0

酷!看看[continuation](http:// *。com/questions/10772821/value-of-the-dired-directory)这个问题。 – Adobe

+0

不行:不行。可能是它不工作。 – Adobe

哦,我解决了它:

(shell-command (concat (expand-file-name "~/its/plts/goodies/bk-konsoles.bash ") 
       curDir " 2>&1 > /dev/null & disown") nil nil) 

和我在bash脚本也呼吁用的konsole 2>&1 > /dev/null &。默默工作!