all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to detect two emacs process in Elisp code?
@ 2020-07-17 14:54 stardiviner
  2020-09-03  2:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-03 13:13 ` Eli Zaretskii
  0 siblings, 2 replies; 9+ messages in thread
From: stardiviner @ 2020-07-17 14:54 UTC (permalink / raw)
  To: Emacs Help


I load desktop-save-mode session in init.el. But when using
"bug-hunter-init-file", I need to exclude this session init file.

Here is my try:

#+begin_src emacs-lisp
;;; detect external system has Emacs process running?
;;; If yes, like `bug-hunter' is running. Then don't load session.
;;; FIXME
(let ((emacs-processes
       (length (mapcar
                'string-to-number
                (seq-filter
                 (lambda (str)
                   (not (string-empty-p str)))
                 (split-string
                  ;; (shell-command-to-string "ps -C emacs -o pid=")
                  (shell-command-to-string "ps -C emacs -o pid=")
                  "\n"))))))
  (when (<= emacs-processes 1)
    (message "init-emacs-session loading")
    (require 'init-emacs-session)))
#+end_src

The upper code has a problem, the command "ps -C emacs -o pid=" can't detect
emacs program name like "emacs-28.2.3" which is which is in /usr/local/bin and
compiled from source code.

Does anyone have good solution for this? Thanks in advance.

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



^ permalink raw reply	[flat|nested] 9+ messages in thread

* How to detect two emacs process in Elisp code?
@ 2020-07-18  5:55 numbchild
  2020-07-18 12:25 ` Noam Postavsky
  0 siblings, 1 reply; 9+ messages in thread
From: numbchild @ 2020-07-18  5:55 UTC (permalink / raw)
  To: emacs-help

I load desktop-save-mode session in init.el. But when using
"bug-hunter-init-file", I need to exclude this session init file.

Here is my try:

#+begin_src emacs-lisp
;;; detect external system has Emacs process running?
;;; If yes, like `bug-hunter' is running. Then don't load session.
;;; FIXME
(let ((emacs-processes
       (length (mapcar
                'string-to-number
                (seq-filter
                 (lambda (str)
                   (not (string-empty-p str)))
                 (split-string
                  ;; (shell-command-to-string "ps -C emacs -o pid=")
                  (shell-command-to-string "ps -C emacs -o pid=")
                  "\n"))))))
  (when (<= emacs-processes 1)
    (message "init-emacs-session loading")
    (require 'init-emacs-session)))
#+end_src

The upper code has a problem, the command "ps -C emacs -o pid=" can't detect
emacs program name like "emacs-28.2.3" which is in /usr/local/bin and
compiled from source code.

Does anyone has good solution for this? Thanks in advance.

[stardiviner]           <Hack this world!>      GPG key ID: 47C32433
IRC(freeenode): stardiviner                     Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to detect two emacs process in Elisp code?
  2020-07-18  5:55 How to detect two emacs process in Elisp code? numbchild
@ 2020-07-18 12:25 ` Noam Postavsky
  2020-07-18 14:01   ` [SOLVED] " numbchild
  0 siblings, 1 reply; 9+ messages in thread
From: Noam Postavsky @ 2020-07-18 12:25 UTC (permalink / raw)
  To: numbchild@gmail.com; +Cc: emacs-help

On Sat, 18 Jul 2020 at 01:56, numbchild@gmail.com <numbchild@gmail.com> wrote:

>                   (shell-command-to-string "ps -C emacs -o pid=")
>                   "\n"))))))
>   (when (<= emacs-processes 1)
>     (message "init-emacs-session loading")
>     (require 'init-emacs-session)))
> #+end_src
>
> The upper code has a problem, the command "ps -C emacs -o pid=" can't detect
> emacs program name like "emacs-28.2.3" which is in /usr/local/bin and
> compiled from source code.

If your init does (server-start), then you can just check for
(server-running-p). Otherwise, something like

(seq-count
 (lambda (proc) (and (equal (alist-get 'user proc) user-login-name)
                (string-match "\\<emacs\\>" (alist-get 'comm proc))))
 (mapcar #'process-attributes (list-system-processes)))



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [SOLVED] Re: How to detect two emacs process in Elisp code?
  2020-07-18 12:25 ` Noam Postavsky
@ 2020-07-18 14:01   ` numbchild
  0 siblings, 0 replies; 9+ messages in thread
From: numbchild @ 2020-07-18 14:01 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: emacs-help

Really thanks, Noam. You're awesome! This solved my problem.

[stardiviner]           <Hack this world!>      GPG key ID: 47C32433
IRC(freeenode): stardiviner                     Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/


On Sat, Jul 18, 2020 at 8:25 PM Noam Postavsky <npostavs@gmail.com> wrote:

> On Sat, 18 Jul 2020 at 01:56, numbchild@gmail.com <numbchild@gmail.com>
> wrote:
>
> >                   (shell-command-to-string "ps -C emacs -o pid=")
> >                   "\n"))))))
> >   (when (<= emacs-processes 1)
> >     (message "init-emacs-session loading")
> >     (require 'init-emacs-session)))
> > #+end_src
> >
> > The upper code has a problem, the command "ps -C emacs -o pid=" can't
> detect
> > emacs program name like "emacs-28.2.3" which is in /usr/local/bin and
> > compiled from source code.
>
> If your init does (server-start), then you can just check for
> (server-running-p). Otherwise, something like
>
> (seq-count
>  (lambda (proc) (and (equal (alist-get 'user proc) user-login-name)
>                 (string-match "\\<emacs\\>" (alist-get 'comm proc))))
>  (mapcar #'process-attributes (list-system-processes)))
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to detect two emacs process in Elisp code?
  2020-07-17 14:54 stardiviner
@ 2020-09-03  2:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-03 13:13 ` Eli Zaretskii
  1 sibling, 0 replies; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-03  2:41 UTC (permalink / raw)
  To: help-gnu-emacs

stardiviner wrote:

> I load desktop-save-mode session in init.el.
> But when using "bug-hunter-init-file", I need to
> exclude this session init file.
>
> Here is my try:
>
> #+begin_src emacs-lisp
> ;;; detect external system has Emacs process running?
> ;;; If yes, like `bug-hunter' is running. Then don't load session.
> ;;; FIXME
> (let ((emacs-processes
>        (length (mapcar
>                 'string-to-number
>                 (seq-filter
>                  (lambda (str)
>                    (not (string-empty-p str)))
>                  (split-string
>                   ;; (shell-command-to-string "ps -C emacs -o pid=")
>                   (shell-command-to-string "ps -C emacs -o pid=")
>                   "\n"))))))
>   (when (<= emacs-processes 1)
>     (message "init-emacs-session loading")
>     (require 'init-emacs-session)))
> #+end_src

Try this:

(require 'seq)
(defun how-many-emacs ()
  (let ((emacs-processes
         (length (mapcar
                  #'string-to-number
                  (seq-filter
                   (lambda (str)
                     (not (string-empty-p str)) )
                   (split-string
                    (shell-command-to-string "ps -C emacs -o pid=")
                    "\n") )))))
    (message (format "Emacs process: %d" emacs-processes)) ))
;; (how-many-emacs) ; 1

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to detect two emacs process in Elisp code?
  2020-07-17 14:54 stardiviner
  2020-09-03  2:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-09-03 13:13 ` Eli Zaretskii
  2020-09-03 17:39   ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 9+ messages in thread
From: Eli Zaretskii @ 2020-09-03 13:13 UTC (permalink / raw)
  To: help-gnu-emacs

> From: stardiviner <numbchild@gmail.com>
> Date: Fri, 17 Jul 2020 22:54:50 +0800
> 
> (let ((emacs-processes
>        (length (mapcar
>                 'string-to-number
>                 (seq-filter
>                  (lambda (str)
>                    (not (string-empty-p str)))
>                  (split-string
>                   ;; (shell-command-to-string "ps -C emacs -o pid=")
>                   (shell-command-to-string "ps -C emacs -o pid=")
>                   "\n"))))))
>   (when (<= emacs-processes 1)
>     (message "init-emacs-session loading")
>     (require 'init-emacs-session)))

Why do you need to use an external command ('ps')?  What's wrong with
using list-system-processes and process-attributes instead?



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to detect two emacs process in Elisp code?
  2020-09-03 13:13 ` Eli Zaretskii
@ 2020-09-03 17:39   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-04  3:57     ` stardiviner
  0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-03 17:39 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii wrote:

>> (let ((emacs-processes
>>        (length (mapcar
>>                 'string-to-number
>>                 (seq-filter
>>                  (lambda (str)
>>                    (not (string-empty-p str)))
>>                  (split-string
>>                   ;; (shell-command-to-string "ps -C emacs -o pid=")
>>                   (shell-command-to-string "ps -C emacs -o pid=")
>>                   "\n"))))))
>>   (when (<= emacs-processes 1)
>>     (message "init-emacs-session loading")
>>     (require 'init-emacs-session)))
>
> Why do you need to use an external command ('ps')?
> What's wrong with using list-system-processes and
> process-attributes instead?

Uhm... like this:

(defun count-emacs ()
  (let ((eps 0))
    (cl-loop for pid in (list-system-processes)
             when (string= (cdar (process-attributes pid)) "emacs")
             do (cl-incf eps) )
    eps) )
;; (count-emacs) ; 1

?

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to detect two emacs process in Elisp code?
  2020-09-03 17:39   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-09-04  3:57     ` stardiviner
  2020-09-04  4:28       ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 9+ messages in thread
From: stardiviner @ 2020-09-04  3:57 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> writes:

> Eli Zaretskii wrote:
>
>>> (let ((emacs-processes
>>>        (length (mapcar
>>>                 'string-to-number
>>>                 (seq-filter
>>>                  (lambda (str)
>>>                    (not (string-empty-p str)))
>>>                  (split-string
>>>                   ;; (shell-command-to-string "ps -C emacs -o pid=")
>>>                   (shell-command-to-string "ps -C emacs -o pid=")
>>>                   "\n"))))))
>>>   (when (<= emacs-processes 1)
>>>     (message "init-emacs-session loading")
>>>     (require 'init-emacs-session)))
>>
>> Why do you need to use an external command ('ps')?
>> What's wrong with using list-system-processes and
>> process-attributes instead?
>
> Uhm... like this:
>
> (defun count-emacs ()
>   (let ((eps 0))
>     (cl-loop for pid in (list-system-processes)
>              when (string= (cdar (process-attributes pid)) "emacs")
>              do (cl-incf eps) )
>     eps) )
> ;; (count-emacs) ; 1
>
> ?

This does not work perfectly, when I have Emacs running, then start "emacs -q"
from new command. The upper `(list-system-processes)` solution get result "1"
instead of "2". Maybe somewhere is wrong?

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to detect two emacs process in Elisp code?
  2020-09-04  3:57     ` stardiviner
@ 2020-09-04  4:28       ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-04  4:28 UTC (permalink / raw)
  To: help-gnu-emacs

stardiviner wrote:

>>>> (let ((emacs-processes
>>>>        (length (mapcar
>>>>                 'string-to-number
>>>>                 (seq-filter
>>>>                  (lambda (str)
>>>>                    (not (string-empty-p str)))
>>>>                  (split-string
>>>>                   ;; (shell-command-to-string "ps -C emacs -o pid=")
>>>>                   (shell-command-to-string "ps -C emacs -o pid=")
>>>>                   "\n"))))))
>>>>   (when (<= emacs-processes 1)
>>>>     (message "init-emacs-session loading")
>>>>     (require 'init-emacs-session)))
>>>
>>> Why do you need to use an external command ('ps')?
>>> What's wrong with using list-system-processes and
>>> process-attributes instead?
>>
>> Uhm... like this:
>>
>> (defun count-emacs ()
>>   (let ((eps 0))
>>     (cl-loop for pid in (list-system-processes)
>>              when (string= (cdar (process-attributes pid)) "emacs")
>>              do (cl-incf eps) )
>>     eps) )
>> ;; (count-emacs) ; 1
>>
>> ?
>
> This does not work perfectly, when I have Emacs
> running, then start "emacs -q" from new command.
> The upper `(list-system-processes)` solution get
> result "1" instead of "2". Maybe somewhere
> is wrong?

Yes, don't use -q :)

Fix it by replacing "emacs" above with something more
intelligent...

Or use the Unix version :)

Simpler.

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-09-04  4:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-18  5:55 How to detect two emacs process in Elisp code? numbchild
2020-07-18 12:25 ` Noam Postavsky
2020-07-18 14:01   ` [SOLVED] " numbchild
  -- strict thread matches above, loose matches on Subject: below --
2020-07-17 14:54 stardiviner
2020-09-03  2:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-03 13:13 ` Eli Zaretskii
2020-09-03 17:39   ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-04  3:57     ` stardiviner
2020-09-04  4:28       ` Emanuel Berg via Users list for the GNU Emacs text editor

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.