What is the correct way to run multiple async-shell-command invocations serially?

For example, this will execute all at once.

  (async-shell-command "echo 1; sleep 1")
  (async-shell-command "echo 2; sleep 1")
  (async-shell-command "echo 3; sleep 1"))

But I want each command to wait for the command before it to finish first. (Of course in this case you can just chain them together with bash and &&, but you might like to have different output buffers, etc.) And I don't want to make the user wait, which is why I don't use UI-blocking shell-command function.

The only way I've figured out how to do this is with sentinels and continuation passing, which is very ugly.

  • 1
    Probably shell-command is no solution for you since you do not want to make the user wait for the commands to finish. If so, please give a note about that. Do you mean by "continuation" to wait for the currently running shell command with a sentinel reacting on finished\\|exited and start the new shell there? That is one way to go. Probably start-process is better for that. You could also use the async package and fire up all shell-command commands synchronously in a separate Emacs with its help. – Tobias Jan 15 '19 at 2:47
  • Here are links to a couple of examples of how to do this: stackoverflow.com/a/23071492/2112489 and stackoverflow.com/a/42879986/2112489 – lawlist Jan 15 '19 at 4:41

The only way I've figured out how to do this is with sentinels and continuation passing, which is very ugly.

Here it is:

 (start-process-shell-command "Shell" "*Output*" "echo 1; sleep 1")
   (_ _)
    (start-process-shell-command "Shell" "*Output*" "echo 2; sleep 1")
      (_ _)
      (start-process-shell-command "Shell" "*Output*" "echo 3; sleep 1")))))

To create the above expression programmatically, you can write a macro, e.g.,

(defmacro multiple-async-shell-commands (&rest commands)
  "Run COMMANDS in sequences, each runs asynchronously."
  (cl-labels ((aux (commands)
                   (pcase commands
                     (`(,command . ,rest)
                         ,@(if (stringp command) `("Shell" "*Output*" ,command) command))
                        (lambda (_ _)
                          ,(aux rest)))))))
    (aux commands)))

then you can use

(multiple-async-shell-commands "echo 1; sleep 1"
                               "echo 2; sleep 1"
                               "echo 3; sleep 1")

you might like to have different output buffers

The COMMANDS can also be a list of argument of start-process-shell-command, so you can set the output buffer:

(multiple-async-shell-commands ("sh" "*output-1*" "echo 1; sleep 1")
                               ("sh" "*output-2*" "echo 2; sleep 1")
                               ("sh" "*output-3*" "echo 3; sleep 1"))
| improve this answer | |
  • 1
    In (set-process-sentinel process (lambda (_proc event) ...)) you should check event for a match of "\`finished\\|exited\\|killed". Alternatively you can check (process-status process) for 'exit in the sentinel. Background: The process might also receive other signals such as stop or run. – Tobias Jan 15 '19 at 11:12
  • Example for other signals: (let* ((bash-buf (get-buffer-create "*bash*")) (proc (start-process "bash" bash-buf "bash" "-c" "sleep 5; exit -1;"))) (set-process-sentinel proc (lambda (proc event) (message "Proc: %S, Event: %S" proc event))) (stop-process proc) (sleep-for 2) (continue-process proc) (sleep-for 2) (kill-process proc)). (You can play around with the signal generating functions.) Those signals can also be sent with kill from the outside of Emacs. – Tobias Jan 15 '19 at 11:13
  • Hm, this doesn't work for me. I get garbage in my process buffers, they live in fundamental-mode, and they don't seem to track processes. – Matthew Piziak Jan 23 '19 at 20:05
  • @MatthewPiziak The first code block is straightforward, does it work for you? (It should create buffer *Output* and insert 1\n2\n3\n\nProcess Shell finished\n in the buffer) The rest is based on the first code block. – xuchunyang Jan 24 '19 at 4:59
  • @xuchunyang Ah yes, this works for plain text. I don't suppose there is a way to restore ANSI code compatibility? – Matthew Piziak Jan 24 '19 at 20:00

you can use async-defun and await from the emacs-async-await package:

;; -*- lexical-binding: t -*-
;; line above goes at the top of your buffer

(async-defun wait-for()
  (await (shell-command-to-string "echo 1; sleep 1"))
  (await (shell-command-to-string "echo 2; sleep 1"))
  (await (shell-command-to-string "echo 3; sleep 1")))


EDIT=fixed parentheses EDIT2= added lexical binding

| improve this answer | |
  • First of all I suspect that your parens don't match here. If I fix them I get Assertion failed: lexical-binding despite the fact that I have (setq lexical-binding t). – Matthew Piziak Jan 23 '19 at 19:55
  • Do mind to add ;; -*- lexical-binding: t -*- at the top of your elisp file. I do think that the assert that pops up is off, but I'm not 100% sure – Felipe Lema Jan 23 '19 at 20:41
  • @MatthewPiziak What does (setq lexical-binding t) mean? It is almost certainly an error. lexical-binding is a buffer local variable therefore setq-ing it in the initialization does not make sense. In elisp files it must be set as file variable in a comment on the first line. – Tobias Jan 24 '19 at 3:41
  • Okay, I've done that and it runs now. But why do you have shell-command-to-string instead of shell-command? There's no way to see the output. – Matthew Piziak Jan 24 '19 at 19:51
  • no particular reason. I just happen to use that function a lot and I wrote it without thinking – Felipe Lema Jan 25 '19 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.