How to execute shell command in background?

Here's a simple defun to run a shell script:

(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)) 

If I run the program without an ampersand - it runs the script, but blocks emacs until I close the program, if I do not put the ampersand, and it gives an error:

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

Edit :

So now I use:

 (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*")) 

Edit 2 :

No, it does not work:

 (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*"))) 

keeps emacs busy - either with disown or nohup .

Here's the script I run if this can help: bk-konsoles.bash

+4
source share
3 answers

I think the problem is the console.

 (shell-command "xterm &") 

does what you expect by opening xterm in a new window and returning Emacs control. Nonetheless,

 (shell-command "konsole &") 

opens and closes the console immediately. Something seems to be related to how the console starts. I think KDE applications have their own system for running applications, but I'm not sure. In any case, I don’t think the problem here is on the Emacs side.

+2
source

You can use nohup or disown as follows:

 $ your_command & disown $ nohup your_command 

See this post in the stackexchange file for a description of the difference.

+2
source

Oh, I solved this:

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

and I also call the console with 2>&1 > /dev/null & in my bash script. It is quiet!

0
source

Source: https://habr.com/ru/post/1414514/


All Articles