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

end of thread, other threads:[~2020-09-03  2:50 UTC | newest]

Thread overview: 6+ 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  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

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).