Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6015947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:54:47+00:00 2026-05-23T02:54:47+00:00

In a ruby script, I start more virtual shells, each managed by a shell

  • 0

In a ruby script, I start more virtual shells, each managed by a shell manager object, like so:

@shell = PTY.spawn 'env PS1="\w>" TERM=dumb COLUMNS=63 LINES=21 sh -i'

At some later point in time, I would like to destroy this instance and also kill the associated shell process. Sadly, I can’t get anything to work properly. Here’s what I tried, in order of probability to work:

  • Nothing, that is, expecting the shell proc gets closed when the managing object gets destroyed.
  • Killing all processes running on the shell (this works) with the kill command, and then killing the shell itself with system("kill #{@shell[2]"). This has no effect.
  • Using -9 in the above. This leaves the shell process defunct.

All the shells get closed when the ruby program exits, but I want to kill them while keeping the program running. Anyone encounter something like this before?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:54:47+00:00Added an answer on May 23, 2026 at 2:54 am

    The problem is zombies. Yes, really.

    All Unix-style kernel’s leave the process around until someone waits for it. (That’s in order to keep track of the PID, the exit status, and a bit of other stuff.) They are called zombies and have a Z state in the ps(1) listing. You can’t kill them, because they are already dead. They go away when you wait for them.

    So here is how to clean up your @shell object:

    @shell[0].close
    @shell[1].close
    begin
      Process.wait @shell[2]
    rescue PTY::ChildExited
    end
    

    You may not need the rescue block depending on whether you have higher level layers catching exceptions too broadly. (Sigh, like my irb.)

    By the way, the reason your process finally vanished when the Ruby program exited is because then the zombie also became an orphan (no parent process) and either the shell or init(8) will eventually wait for all orphans.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Ruby script and would like to use a n-ary tree data
I would like to execute an OS command from my ruby script but I
I have this error when I try to start script/server: /opt/ruby1.8/lib/ruby/site_ruby/1.8/rubygems.rb:827:in `report_activate_error': RubyGem version
I have written a ruby script which opens up dlink admin page in firefox
I have a Ruby script that generates a UTF8 CSV file remotely in a
I am running ruby script/generate scaffold or ruby script/generate model and I know the
I have a Ruby script in my Rails app that I use to load
When I'm running a simple Ruby script, what's the easiest way to dump an
I want to run a standalone ruby script in which I need my RoR
Is there an easy way to tell if a ruby script is already running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.