Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do get the full command line in ruby?

Tags:

ruby

How do I get the full command line in ruby?

$ rails c
> $0 
=> "script/rails"
> ARGV
[]
> `ps -eo "%p|$|%a" | grep '^\\s*#{Process.pid}'`.strip.split("|$|")[1]
=> "/home/sam/.rvm/rubies/ruby-1.9.3-p194-perf/bin/ruby script/rails console"

Is there anything cleaner than ninja ps I can do to get the same results?

To clarify, in case there is confusion, I want the exact same output as:

`ps -eo "%p|$|%a" | grep '^\\s*#{Process.pid}'`.strip.split("|$|")[1]

ARGV is coming back blank.
$0 is missing the full path.

like image 252
Sam Saffron Avatar asked Feb 18 '13 02:02

Sam Saffron


1 Answers

I'd use:

#!/usr/bin/env ruby

puts "Process ID: #{ $$ }"
puts `ps axw`.split("\n").select{ |ps| ps[ /\A#{ $$ }/ ] }

Running that inside a script outputs:

18222 s000  S+     0:00.25 /Users/foo/.rbenv/versions/1.9.3-p385/bin/ruby /Users/foo/.rbenv/versions/1.9.3-p385/bin/rdebug /Users/foo/Desktop/test.rb
like image 159
the Tin Man Avatar answered Oct 13 '22 15:10

the Tin Man