How can a Ruby class detect the process name that runs it?
Asked Answered
R

3

6

I am using delayed_jobs to run Ruby classes, how can the Ruby detect the name (command) of the process that is running it?

any thoughts?

Rosemaria answered 26/5, 2011 at 16:10 Comment(0)
P
7

You can determine the name of the file using $0. You can even set the process name using it like so $0 = 'foo'Dave Thomas's blog post. If you want to get the command line args, you want to look at ARGV.

Plast answered 26/5, 2011 at 16:45 Comment(2)
By the way, the $0 variable is aliased by $PROGRAM_NAMEKennethkennett
Nice. That's a lot less confusing a name.Plast
H
2

$0 Contains the name of the script being executed. (May be assignable so it can go wrong)

Hydroplane answered 26/5, 2011 at 17:24 Comment(1)
ruby-prof's --replace-progname switch manipulates $0 for this very purpose, so the $0 == __FILE__ magic incantation in the script being run still works.Aestivate
G
-4

If you are are using Unix system you can execute script from ruby to determine process like this:

`ps aux | grep ruby` 

and then use $? to get the process

Gimble answered 26/5, 2011 at 16:35 Comment(1)
This will show every ruby process on the system, not just the script in question.Rattlepate

© 2022 - 2024 — McMap. All rights reserved.