Kill the child process that disconnected from the shell

I want to control many different Sinatra applications from a central application (Sinatra).

The problem I am facing, no matter how I make / start / start the call, to start it, I can not get the pid of the Sinatra server so that I can kill (: int) it?

This is due to my shell exec line, which contains several other commands, so I get the pid from the first. My command line is like

command = source ~/.profile; rbenv #{ver}; some_env=1234 ruby app.rb 

So, I get the pid of the sh process of the sourcing command.

The question is, how can I get the pid of the ruby ​​command running above?

I am currently using spawn, but I have tried most others, but I don’t think this is a problem!

 pid = Process.spawn(command) pid # => 1234 

Beginning of the ruby ​​application itself

 $ ps aux 1234 sh -c . ~/.profile; shell_script 4567 shell_script 

I want to know 4567 !?

+4
source share
2 answers

There is no easy way to get your "4567", but you should be able to make your process with the same pid as Process.spawn .

Try completing your command with exec instead of calling ruby ​​directly, i.e.:

 source ~/.profile; rbenv #{ver}; export some_env=1234; exec ruby app.rb 
0
source

you can check if the shell_script process is a child of sh -c. ~ / .profile; shell_script. You can verify this with the ps -axgf command.

if it is a parent, then u can use the pid 1234 group id (get it from ps -axgf output) to kill the child with pid 4567 with this command.

kill -9 -1234 (pin 1234 is the group identifier)

0
source

Source: https://habr.com/ru/post/1404010/


All Articles