unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* How to capture pid of (system process?
@ 2021-12-13 21:01 Jacob Hrbek
  2021-12-14 13:05 ` Tim Van den Langenbergh
  2021-12-14 13:25 ` Maxime Devos
  0 siblings, 2 replies; 6+ messages in thread
From: Jacob Hrbek @ 2021-12-13 21:01 UTC (permalink / raw)
  To: guile-user


[-- Attachment #1.1: Type: text/plain, Size: 820 bytes --]

I wrote this potato-make <https://github.com/spk121/potato-make> 
definition (was simplified):

#:SRC_BEGIN sheme-mode
#!/usr/bin/env sh
exec guile -s "$0" "$@"
!#

(use-modules (ice-9 futures)
  (potato make))
(initialize)

(: "watch" '()
    (~ (do ((i 1 (1+ i)))
       ((> i 6))
     (future (system
      "emacs"))
     ;;(let ((emacs_pid (getpid)))
      ;; (kill emacs_pid SIGTERM)))))

(execute)
#:SRC_END

expecting to capture emacs's pid and kill it, but the issue is that 
using `getpid` gets me the PID of potato-make and `getppid` of the shell 
-> How can i capture just the emacs's pid?

FWIW the projected end-goal is 3600 loop that checks if the 
`my-theme.el` file has been changed to re-launch emacs with the theme 
loaded used for theme development.


[-- Attachment #1.2: publickey - kreyren@rixotstudio.cz - 1677db82.asc --]
[-- Type: application/pgp-keys, Size: 713 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 249 bytes --]

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

* Re: How to capture pid of (system process?
  2021-12-13 21:01 How to capture pid of (system process? Jacob Hrbek
@ 2021-12-14 13:05 ` Tim Van den Langenbergh
  2021-12-14 13:25 ` Maxime Devos
  1 sibling, 0 replies; 6+ messages in thread
From: Tim Van den Langenbergh @ 2021-12-14 13:05 UTC (permalink / raw)
  To: Jacob Hrbek, guile-user


[-- Attachment #1.1: Type: text/plain, Size: 1484 bytes --]

On 13/12/2021 22:01, Jacob Hrbek wrote:
> I wrote this potato-make <https://github.com/spk121/potato-make> definition (was simplified):
> 
> #:SRC_BEGIN sheme-mode
> #!/usr/bin/env sh
> exec guile -s "$0" "$@"
> !#
> 
> (use-modules (ice-9 futures)
>  (potato make))
> (initialize)
> 
> (: "watch" '()
>    (~ (do ((i 1 (1+ i)))
>       ((> i 6))
>     (future (system
>      "emacs"))
>     ;;(let ((emacs_pid (getpid)))
>      ;; (kill emacs_pid SIGTERM)))))
> 
> (execute)
> #:SRC_END
> 
> expecting to capture emacs's pid and kill it, but the issue is that using `getpid` gets me the PID of potato-make and `getppid` of the shell -> How can i capture just the emacs's pid?
> 
> FWIW the projected end-goal is 3600 loop that checks if the `my-theme.el` file has been changed to re-launch emacs with the theme loaded used for theme development.
> 

Hello,

I hope this day finds you well.

While Guile does not directly provide a function to get the PID of an arbitrary process, we could use pgrep to find what we need.

#+begin_src scheme
  (use-modules (ice-9 popen)
               (ice-9 textual-ports))

  (map string->number
       (string-split (let ((port (open-input-pipe "pgrep emacs"))
                           (s (get-string-all port)))
                       (close-pipe port)
                       s)
                     #\newline))
  ;; => (1220 #f)
#+end_src

I hope this helps!

Vale,

-Tim

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

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

* Re: How to capture pid of (system process?
  2021-12-13 21:01 How to capture pid of (system process? Jacob Hrbek
  2021-12-14 13:05 ` Tim Van den Langenbergh
@ 2021-12-14 13:25 ` Maxime Devos
  2021-12-17  1:32   ` Jacob Hrbek
  1 sibling, 1 reply; 6+ messages in thread
From: Maxime Devos @ 2021-12-14 13:25 UTC (permalink / raw)
  To: Jacob Hrbek, guile-user

Jacob Hrbek schreef op ma 13-12-2021 om 21:01 [+0000]:
> I wrote this potato-make <https://github.com/spk121/potato-make> 
> definition (was simplified):
> 
> #:SRC_BEGIN sheme-mode
> #!/usr/bin/env sh
> exec guile -s "$0" "$@"
> !#
> 
> (use-modules (ice-9 futures)
>   (potato make))
> (initialize)
> 
> (: "watch" '()
>     (~ (do ((i 1 (1+ i)))
>        ((> i 6))
>      (future (system
>       "emacs"))
>      ;;(let ((emacs_pid (getpid)))
>       ;; (kill emacs_pid SIGTERM)))))
> 
> (execute)
> #:SRC_END
> 
> expecting to capture emacs's pid and kill it, but the issue is that 
> using `getpid` gets me the PID of potato-make and `getppid` of the
> shell 
> -> How can i capture just the emacs's pid?

'system' executes the command and waits for it to complete.
After it completed, the process is dead so the pid it used to have is
useless.

If you want to do things asynchronuously, you can look at open-pipe.
It should also be possible to build anything you want with the lower-
level fork, execl, dup->fdes, ... primitives (assuming things are
single-threaded).

Greetings,
Maxime




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

* Re: How to capture pid of (system process?
  2021-12-14 13:25 ` Maxime Devos
@ 2021-12-17  1:32   ` Jacob Hrbek
  2021-12-17  6:45     ` Maxime Devos
  2021-12-17 16:24     ` Timothy Sample
  0 siblings, 2 replies; 6+ messages in thread
From: Jacob Hrbek @ 2021-12-17  1:32 UTC (permalink / raw)
  To: Maxime Devos, guile-user


[-- Attachment #1.1: Type: text/plain, Size: 2084 bytes --]

 > While Guile does not directly provide a function to get the PID of an 
arbitrary process, we could use pgrep to find what we need. -- Tim

I don't want it to depend on an external commands as the implementation 
is not robust enough for my environment (this codeblock doesn't have to 
be robust, but the project is designed for aviation and i want to do 
similar implementations later for more mission critical functions).

So preferably i need a derivation that integrates with the system and is 
OS independent.

 > If you want to do things asynchronuously, you can look at open-pipe. 
It should also be possible to build anything you want with the 
lower-level fork, execl, dup->fdes, ... primitives (assuming things are 
single-threaded). -- Devos

Can you elaborate? (I am noob in guile atm)

- Jacob Hrbek

On 12/14/21 14:25, Maxime Devos wrote:
> Jacob Hrbek schreef op ma 13-12-2021 om 21:01 [+0000]:
>> I wrote this potato-make <https://github.com/spk121/potato-make>
>> definition (was simplified):
>>
>> #:SRC_BEGIN sheme-mode
>> #!/usr/bin/env sh
>> exec guile -s "$0" "$@"
>> !#
>>
>> (use-modules (ice-9 futures)
>>    (potato make))
>> (initialize)
>>
>> (: "watch" '()
>>      (~ (do ((i 1 (1+ i)))
>>         ((> i 6))
>>       (future (system
>>        "emacs"))
>>       ;;(let ((emacs_pid (getpid)))
>>        ;; (kill emacs_pid SIGTERM)))))
>>
>> (execute)
>> #:SRC_END
>>
>> expecting to capture emacs's pid and kill it, but the issue is that
>> using `getpid` gets me the PID of potato-make and `getppid` of the
>> shell
>> -> How can i capture just the emacs's pid?
> 'system' executes the command and waits for it to complete.
> After it completed, the process is dead so the pid it used to have is
> useless.
>
> If you want to do things asynchronuously, you can look at open-pipe.
> It should also be possible to build anything you want with the lower-
> level fork, execl, dup->fdes, ... primitives (assuming things are
> single-threaded).
>
> Greetings,
> Maxime
>

[-- Attachment #1.2: publickey - kreyren@rixotstudio.cz - 1677db82.asc --]
[-- Type: application/pgp-keys, Size: 713 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 249 bytes --]

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

* Re: How to capture pid of (system process?
  2021-12-17  1:32   ` Jacob Hrbek
@ 2021-12-17  6:45     ` Maxime Devos
  2021-12-17 16:24     ` Timothy Sample
  1 sibling, 0 replies; 6+ messages in thread
From: Maxime Devos @ 2021-12-17  6:45 UTC (permalink / raw)
  To: Jacob Hrbek, guile-user

Jacob Hrbek schreef op vr 17-12-2021 om 01:32 [+0000]:
> Can you elaborate? (I am a noob in guile atm)

These procedures are documented in the manual of Guile,
you could look in the source code of Shepherd and Guix
for examples. Most of these procedures are based on POSIX
functions, so you could look at the documentation of the
corresponding POSIX functions for the general concept.

Greetings,
Maxime






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

* Re: How to capture pid of (system process?
  2021-12-17  1:32   ` Jacob Hrbek
  2021-12-17  6:45     ` Maxime Devos
@ 2021-12-17 16:24     ` Timothy Sample
  1 sibling, 0 replies; 6+ messages in thread
From: Timothy Sample @ 2021-12-17 16:24 UTC (permalink / raw)
  To: Jacob Hrbek; +Cc: guile-user

Hi Jacob,

Jacob Hrbek <kreyren@rixotstudio.cz> writes:

>> If you want to do things asynchronuously, you can look at
>> open-pipe. It should also be possible to build anything you want with
>> the lower-level fork, execl, dup->fdes, ... primitives (assuming
>> things are single-threaded).
>
> Can you elaborate? (I am noob in guile atm)

If you want to dig into it, the GNU manual is not half bad:

https://www.gnu.org/software/libc/manual/html_node/Processes.html

Guile just wraps those same C functions, so that description applies
quite well to Guile.  Here’s the Guile reference for the procedures
you’d need:

https://www.gnu.org/software/guile/manual/html_node/Processes.html

Alternatively, if you are feeling adventurous – you must be if you are
using Potato Make :) – you could try and use Gash, too.  You would end
up with a very Scheme-y Make experience indeed!

Gash’s interface is laser focused on shell semantics so far, so it’s a
little wonky as a Scheme library, but here’s an example:

    (use-modules (gash environment)
                 (gash shell))

    (sh:async
     (lambda ()
       (execlp "emacs")))
    (display "waiting a bit...\n")
    (sleep 5)
    (let ((emacs-pid (get-last-job)))
      (display "sending SIGTERM to ")
      (display emacs-pid)
      (newline)
      (kill emacs-pid SIGTERM))

Gash is very much experimental software when used like this, though.
The latest release does not have ‘sh:async’, so you would have to build
it from Git.  It might not be super practical, but it would be fun!  :)


-- Tim



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

end of thread, other threads:[~2021-12-17 16:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-13 21:01 How to capture pid of (system process? Jacob Hrbek
2021-12-14 13:05 ` Tim Van den Langenbergh
2021-12-14 13:25 ` Maxime Devos
2021-12-17  1:32   ` Jacob Hrbek
2021-12-17  6:45     ` Maxime Devos
2021-12-17 16:24     ` Timothy Sample

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