0

I'm running Emacs on Ubuntu 20.04.04 LTS with the GNOME desktop and Xorg window system. I launch a file previewer located at /usr/bin/sushi with:

(start-process "proc-name" nil "/usr/bin/sushi" FILENAME)

After the previewer application launches successfully and remains open, the the Emacs process finishes and there are no active processes running through Emacs. I.e., M-x list-processes shows nothing active.

The process associated with sushi can be seen with M-x proced with a line as follows:

lawlist 13352  5.5  0.9  11:06 00:00 /usr/bin/gjs /usr/libexec/org.gnome.NautilusPreviewer

The above-process can be killed manually using the M-x proced buffer interface.

Using Emacs, how can I programmatically test for whether this particular process is running and, and if so, kill it programmatically?

2 Answers 2

1

You can use list-system-processes and process-attributes (untested):

(let ((pid (delq nil
                 (mapcar
                  (lambda (pid)
                    (if (string-equal
                         "sushi"
                         (assoc-default
                           'comm (process-attributes pid) nil ""))
                        pid))
                  (list-system-processes)))))
  (if pid (signal-process (car pid) 'INT)))
1
  • Using the sushi example, the cons cell for the comm key is (comm . org.gnome.Nauti), and the cons cell for the args key is (args . /usr/bin/gjs /usr/libexec/org.gnome.NautilusPreviewer). Once those expected values are ascertained, either cons cell can be tested. Prior to initiating the question of this particular thread, I had to spin my wheels for quite a bit of time to figure out that the sushi application launches the ...NautilusPreviewer ..., as I had been looking for the word sushi in the active list of processes. Thank you for your help -- greatly appreciated!
    – lawlist
    Sep 4, 2022 at 20:18
0

The following approach uses a command-line utility pgrep with the -f argument to search for a process matching a specified command line, and the utility kill is used to terminate a known PID:

(let* ((command "pgrep -f \"/usr/bin/gjs /usr/libexec/org.gnome.NautilusPreviewer\"")
       (pid (replace-regexp-in-string "\n$" "" (shell-command-to-string command))))
  (if (not (equal "" pid))
    (call-process shell-file-name nil nil nil shell-command-switch
                  ;;; Yes, we could have just used:  (concat "kill " pid)
                  (mapconcat 'identity (list "kill" pid) " "))
    (message "Cannot locate PID!")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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