如何在后台执行shell命令?

时间:2012-05-26 13:11:55

标签: emacs elisp

这是一个简单的defun来运行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忙碌 - 使用disownnohup

这是我正在运行的脚本,如果有帮助的话:bk-konsoles.bash

3 个答案:

答案 0 :(得分:3)

我认为问题是konsole。

(shell-command "xterm &")

完成您的期望,在新窗口中打开xterm并将控制权返回给Emacs。然而,

(shell-command "konsole &")

立即打开并关闭konsole。关于konsole启动方式的一些事情似乎正在引发这个问题。我认为KDE应用程序有自己的系统来启动应用程序,但我不确定。无论如何,我认为问题不在于Emacs方面。

答案 1 :(得分:2)

您可以像这样使用nohupdisown

$ your_command & disown
$ nohup your_command

有关差异的说明,请参阅stackexchange上的this post

答案 2 :(得分:0)

哦,我解决了它:

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

我也在我的bash脚本中用2>&1 > /dev/null &调用konsole。默默地工作!

相关问题