I want to run some Elisp when my command executed by async-shell-command
terminates.
How can I do this? Does it have completion hooks?
I want to run some Elisp when my command executed by async-shell-command
terminates.
How can I do this? Does it have completion hooks?
You can specify the output buffer for async-shell-command
.
The shell runs as a process of the output buffer.
You can get that process with get-buffer-process
.
Define your own process sentinel for the shell and set it with set-process-sentinel
.
It is wise to run shell-command-sentinel
from your sentinel
since that is the sentinel actually set by async-shell-command
.
Note that the output buffer may not be associated with any other process when you call async-shell-command
. Otherwise an other buffer could be used as process buffer for the shell command (see the documentation of the variable async-shell-command-buffer
).
There follows an example:
(defun do-something (process signal)
(when (memq (process-status process) '(exit signal))
(message "Do something!")
(shell-command-sentinel process signal)))
(let* ((output-buffer (generate-new-buffer "*Async shell command*"))
(proc (progn
(async-shell-command "sleep 10; echo Finished" output-buffer)
(get-buffer-process output-buffer))))
(if (process-live-p proc)
(set-process-sentinel proc #'do-something)
(message "No process running.")))