2012-05-26 266 views
4

下面是一個簡單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

回答

2

我認爲問題是konsole。

(shell-command "xterm &") 

做你所期望的,在新窗口中打開xterm並返回控制權給Emacs。但是,

(shell-command "konsole &") 

立即打開和關閉konsole。 konsole開始的方式似乎導致了這個問題。我認爲KDE應用程序有自己的啓動應用程序系統,但我不確定。無論如何,我認爲這個問題不在Emacs這邊。

2

您可以使用nohupdisown這樣的:

$ your_command & disown 
$ nohup your_command 

this post上stackexchange的差異的說明。

+0

酷!看看[continuation](http:// stackoverflow。com/questions/10772821/value-of-the-dired-directory)這個問題。 – Adobe

+0

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

0

哦,我解決了它:

(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 &。默默工作!