unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / 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; 11+ 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] 11+ 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; 11+ 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] 11+ messages in thread

* Re: How to detect two emacs process in Elisp code?
  2020-07-18  5:55 numbchild
@ 2020-07-18 12:25 ` Noam Postavsky
  0 siblings, 0 replies; 11+ 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] 11+ messages in thread

* Re: How to detect two emacs process in Elisp code?
  2020-07-17 14:54 How to detect two emacs process in Elisp code? stardiviner
@ 2020-09-03  2:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-03  2:45   ` [SOLVED] " numbchild
  2020-09-03 13:13 ` Eli Zaretskii
  1 sibling, 1 reply; 11+ 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] 11+ messages in thread

* [SOLVED] Re: How to detect two emacs process in Elisp code?
  2020-09-03  2:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-09-03  2:45   ` numbchild
  2020-09-03  2:47     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 11+ messages in thread
From: numbchild @ 2020-09-03  2:45 UTC (permalink / raw)
  To: Emanuel Berg, emacs-help

Thanks for quick reply. Confirmed it solved my problem. Thanks very much!

[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 Thu, Sep 3, 2020 at 10:41 AM Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> 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] 11+ messages in thread

* Re: [SOLVED] Re: How to detect two emacs process in Elisp code?
  2020-09-03  2:45   ` [SOLVED] " numbchild
@ 2020-09-03  2:47     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-03  2:50       ` numbchild
  0 siblings, 1 reply; 11+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-03  2:47 UTC (permalink / raw)
  To: help-gnu-emacs

numbchild@gmail.com wrote:

> Thanks for quick reply. Confirmed it solved my
> problem. Thanks very much!

Np :)

But what about this?

(defun how-many-emacs-2 ()
  (let*((ps-outp (shell-command-to-string "ps -C emacs -o pid="))
        (ps-lst (split-string ps-outp)) )
    (length ps-lst) ))
;; (how-many-emacs-2) ; 1

Looks better, right?

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




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

* Re: [SOLVED] Re: How to detect two emacs process in Elisp code?
  2020-09-03  2:47     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-09-03  2:50       ` numbchild
  0 siblings, 0 replies; 11+ messages in thread
From: numbchild @ 2020-09-03  2:50 UTC (permalink / raw)
  To: Emanuel Berg, emacs-help

Yes, it's simpler. Ahah

[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 Thu, Sep 3, 2020 at 10:47 AM Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> numbchild@gmail.com wrote:
>
> > Thanks for quick reply. Confirmed it solved my
> > problem. Thanks very much!
>
> Np :)
>
> But what about this?
>
> (defun how-many-emacs-2 ()
>   (let*((ps-outp (shell-command-to-string "ps -C emacs -o pid="))
>         (ps-lst (split-string ps-outp)) )
>     (length ps-lst) ))
> ;; (how-many-emacs-2) ; 1
>
> Looks better, right?
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
>


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

* Re: How to detect two emacs process in Elisp code?
  2020-07-17 14:54 How to detect two emacs process in Elisp code? 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; 11+ 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] 11+ 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; 11+ 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] 11+ 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; 11+ 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] 11+ 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; 11+ 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] 11+ messages in thread

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

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-17 14:54 How to detect two emacs process in Elisp code? stardiviner
2020-09-03  2:41 ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-03  2:45   ` [SOLVED] " numbchild
2020-09-03  2:47     ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-03  2:50       ` numbchild
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
  -- strict thread matches above, loose matches on Subject: below --
2020-07-18  5:55 numbchild
2020-07-18 12:25 ` Noam Postavsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).