Reputation: 182
I am creating a CLI application using ruby. This app receives a path to an executable, runs it, fetches the PID and collects stack trace samples every N milliseconds in order to profile the executable.
I use Process.spawn
like this:
pid = Process.spwan(ENV, executable)
The problem with pid
is that it's not the executable's, but it is the PID of sh -c <EXECUTABLE>
.
In order to fetch the right PID I use pidof
after Process.spawn
like this:
target_pid = `pidof -s #{executable}`
and then I use target_pid
for profiling.
Is there a cleaner way to get target_pid
using Ruby?
Upvotes: 1
Views: 133
Reputation:
If you don't need the shell then use either of these two forms of calling:
pid = Process.spwan(ENV, executable, '--arg1')
and if you have no arguments then you would need to use this format instead:
pid = Process.spwan(ENV, [executable, 'name of executable'])
If you need the shell then you need to modify your executable
variable to do something along these lines
cmd & echo "pid=$!"; fg
which means run your command in the background, obtain the pid of it which you can somehow communicate to your ruby process, then put that process in the foreground again.
Upvotes: 1