# What is the equivalent of Ruby Process.detach?

**URL:** https://forum.crystal-lang.org/t/what-is-the-equivalent-of-ruby-process-detach/5062
**Category:** Help & Support
**Created:** [November 3, 2022, 7:30am UTC](https://forum.crystal-lang.org/t/what-is-the-equivalent-of-ruby-process-detach/5062 "2022-11-03T07:30:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)
#### Post date: [November 3, 2022, 7:30am UTC](https://forum.crystal-lang.org/t/what-is-the-equivalent-of-ruby-process-detach/5062/1 "2022-11-03T07:30:45Z")

</div>

> **[Module: Process (Ruby 3.0.2)](https://ruby-doc.org/core-3.0.2/Process.html#method-c-detach)**
>
> Module : Process - Ruby 3.0.2

Thanks.

---

<div class="post-metadata">

### Author: ![naqvis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/naqvis/32/906_2.png) [@naqvis](https://forum.crystal-lang.org/u/naqvis)
#### Post date: [November 3, 2022, 8:41am UTC](https://forum.crystal-lang.org/t/what-is-the-equivalent-of-ruby-process-detach/5062/2 "2022-11-03T08:41:01Z")

</div>

`Process.run` waits for process to complete. And if you want to achieve asynchronous execution, you can `spawn` that in fiber.

```auto
 spawn { Proces.run (........) }

```

But keep in mind, Crystal is Single Threaded by default (unless you are in preview mode), so all things will run in same thread.

HIH

---

<div class="post-metadata">

### Author: ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)
#### Post date: [November 4, 2022, 4:56pm UTC](https://forum.crystal-lang.org/t/what-is-the-equivalent-of-ruby-process-detach/5062/3 "2022-11-04T16:56:06Z")

</div>

Hi, i am migrate a ruby gem into crystal, a gem like [Foreman](https://github.com/ddollar/foreman) but things are designed to run in both foreground/background, here is the Process related code.

> <https://github.com/adamcooke/procodile/blob/master/lib/procodile/instance.rb#L150-L155>

It’s seem like same code can be write use Crystal like this:

```crystal
x = Process.new(command: "sleep 10000", shell: true)
puts x.pid

```

Process.new will return a Process object immediately, this behavior seem like same as ruby Process.spawn, as describe as following:

```auto
This method is similar to Kernel#system but it doesn't wait for the command to finish.

The parent process should use Process.wait to collect the termination status of its child or use Process.detach to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.

```

But, there is no equivalent of Process.detach(pid) in Crystal, will it cause zombie processes if we don’t use it at some case?

i still don’t know `spawn { Process.run(...) }` or `Process.new` which one should be used for this case too.

---

<div class="post-metadata">

### Author: ![naqvis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/naqvis/32/906_2.png) [@naqvis](https://forum.crystal-lang.org/u/naqvis)
#### Post date: [November 5, 2022, 9:59am UTC](https://forum.crystal-lang.org/t/what-is-the-equivalent-of-ruby-process-detach/5062/4 "2022-11-05T09:59:27Z")

</div>

Question would be, do you need to interact with process after you run that from your Crystal code? if yes then go with `Process.new` and keep hold of Process object, invoke `Process#wait` in a separate fiber. This use case is useful if you are interacting with a process via pipes and need to do some kind of communication and at end of your program closure, you need to terminate that.

And if your code doesn’t need any other interaction with process object and all you need is just start a process then you can invoke `Process.run` in a separate fiber.

Something like

```crystal
 proc = Process.new(command: "sleep 10000", shell: true)
 spawn { proc.wait}
# continue with your work
# pass around proc object to some code which need to do some further handling
# invoking either close or terminate on proc object at end of your interaction or program closure

```

OR

```crystal
spawn { Process.run(command: "sleep 10000", shell: true) }
# continue with your work

```

HIH

---

<div class="post-metadata">

### Author: ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)
#### Post date: [November 5, 2022, 12:54pm UTC](https://forum.crystal-lang.org/t/what-is-the-equivalent-of-ruby-process-detach/5062/5 "2022-11-05T12:54:51Z")

</div>

Thanks, the case is first one.

```crystal
proc = Process.new(command: "sleep 10000", shell: true)
 spawn { proc.wait}

```

One more question to clarify, after the first line run, we got the Process object proc(we can get pid from this object too), so, the spawn { proc.wait }, do the same thing as Ruby `Process.detach`, right?

As the Process.detach definition in Ruby doc.

* * *

> [@](#):
>
> Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of wait()). If the parent never collects this status, the child stays around as a zombie process. ::detach prevents this by setting up a separate Ruby thread whose sole job is to reap the status of the process pid when it terminates. Use detach only when you do not intend to explicitly wait for the child to terminate.

---

<div class="post-metadata">

### Author: ![naqvis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/naqvis/32/906_2.png) [@naqvis](https://forum.crystal-lang.org/u/naqvis)
#### Post date: [November 5, 2022, 2:00pm UTC](https://forum.crystal-lang.org/t/what-is-the-equivalent-of-ruby-process-detach/5062/6 "2022-11-05T14:00:21Z")

</div>

`Process#wait` will wait for the process to finish. But keep in mind that Crystal by default is single threaded and if your main thread terminates before process is finished, then fiber will terminate and you will have zombie process. If you need to ensure that process is terminated before your application closes, either use some channels to let main thread wait till fiber(s) are finished or send close/terminate or kill signal to processes object to ensure that get terminated.
