newBike
newBike

Reputation: 15022

How to get the pid of external process

I ran the external python script by system(run_command)

But I want to get the pid of the running python script,

So I tried to use fork and get the pid,

But the returned pid was the pid of the fork's block, not the python process.

How could I get the pid of the python process, thanks.

enter image description here

arguments=[
"-f #{File.join(@public_path, @streaming_verification.excel.to_s)}",
"-duration 30",
"-output TEST_#{@streaming_verification.id}"
]
cmd = [ "python",
@automation[:bin],
arguments.join(' ')
]
run_command = cmd.join(' ').strip() 

task_pid = fork do 
    system(run_command)
end

(Update)

I tried to use the spawn method.

The retuned pid was still not the pid of the running python process.

I got the pid 5177 , but the actually pid,I wanted, is 5179

run.sh

./main.py  -f ../tests/test_setting.xls -o testing_`date +%s` -duration 5

sample.rb

cmd = './run.sh'
pid = Process.spawn(cmd)
print pid
Process.wait(pid)

enter image description here

enter image description here

Upvotes: 1

Views: 419

Answers (1)

falsetru
falsetru

Reputation: 369444

According to Kernel#system:

Executes command… in a subshell. command… is one of following forms.

You will get pid of subshell, not the command.


How about using Process#Spwan? It returns the pid of the subprocess.

run_command = '...'
pid = Process.spawn(cmd)
Process.wait(pid)

Upvotes: 2

Related Questions