Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
PID returned by spawn differs from Process.pid of the child process

I want to spawn a potentially long running process and save its PID so I can kill it later if needed. The problem is that the PID I get from spawn is not the same I get from Process.pid (in the child process). The result of Process.pid matches what I see running ps in the command line.

Here is the code for parent and child

parent.rb

puts "Hi parent"
pid = spawn "ruby child.rb 10 &"
puts pid
Process.detach(pid)
sleep 3
puts "Bye parent"

child.rb

puts "Start pid #{Process.pid}"
sleep ARGV.first.to_i
puts "End pid #{Process.pid}"

Output

Hi parent
1886789
Start pid 1886791
Bye parent
End pid 1886791
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Looks like the background job operator (&) is causing the intermediate process 1886789. When I remove the background job operator, I get the following output:

Hi parent
93185
Start pid 93185
Bye parent
End pid 93185
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!