segfault
segfault

Reputation: 169

Get PID of system call in Ruby

So basically what I'm trying to do is have a system call in ruby and find out the PID of that new process. I am trying to execute another program through my system call, however the program being executed will never finish so I need to eventually kill it using the pid.

I have been unable to figure out how to do this. So far I have looked at system() which returns true/false if the program was executed successfully, which isn't useful. I have tried to fork it, but that also doesn't work because it returns the PID of the fork not the system call.

Any help is greatly appreciated.

Sorry if this question has been asked before I have been unable to find it.

Upvotes: 2

Views: 3712

Answers (2)

Christophe Biocca
Christophe Biocca

Reputation: 3468

If you use Process.spawn, you will spawn a process, and get its PID, without having to wait for it to finish.

See the docs for more info, and for resource limits, which might be an alternative way to achieve your goal.

Upvotes: 7

Amadan
Amadan

Reputation: 198496

You can't get the PID of the system call as such. You can get PID of the Ruby parent, then look up the children of that process, and find it that way. sys-proctable gem is handy for this.

However, easiest of all would be for the process itself to report its PID (by writing it in a predetermined file, for instance).

Upvotes: 2

Related Questions