D3XT3R
D3XT3R

Reputation: 171

How to query for Ruby scripts currently running on Unix?

I have a few Ruby scripts: a.rb, b.rb and c.rb. These scripts are called from corresponding wrapper shell scripts: a.sh, b.sh and c.sh.

All these scripts are in a distributed environment:

`a.sh` and `a.rb` are on serverA
`b.sh` and `b.rb` are on serverB
`c.sh` and `c.rb` are on serverC

I need to write a script call.rb and its wrapper call.sh script, which should check for all the scripts currently running on the distributed environment.

I have the logic which will determine the different hosts that I have and how to communicate to these different hosts.

When any Ruby script is running, the command:

ps aux 

shows:

ruby a.rb

I have no ideas on how to query for different scripts currently running. One thing to note is that there might be other Ruby scripts running in the system too, but I need to check only for a.rb, b.rb, or c.rb.

Upvotes: 1

Views: 1285

Answers (3)

the Tin Man
the Tin Man

Reputation: 160551

If you're doing a heartbeat check, or setting up a keep-alive check, why not have the files save their PID to a file at their startup, and then delete it when they quit?

The building blocks are:

  • $$ is the current process ID for a running script.
  • Ruby will run a block named BEGIN {} at start-up, before variables are defined. You can use that to create a PID file. Typically we use something like "#{ File.basename($0) }.pid" to create the filename.
  • Ruby will run a block named END {} at shut-down, as a last task. You can use that to remove the PID file.
  • Put the PID files in a well-known place. Where that is is left as an exercise for you to figure out for your OS.

Have your watchdog scan those, grab the PIDs, scan the process list for the PID IDs, possibly correlating them to the name of the pid file.

You can figure out more icing to put on your cake.

Upvotes: 1

Patrick Oscity
Patrick Oscity

Reputation: 54674

If you want to solve this in Ruby, you could use a process monitoring tool like God. Monit, Bluepill or Eye

Upvotes: 0

Sammy S.
Sammy S.

Reputation: 1290

You can simply execute commands via SSH like this:

ssh user@host "ps -ef | grep '(ruby|[^.]+\.rb)'"

Grepping the output of ps for the script names would also work:

ps -ef | grep '(a.rb|b.rb|c.rb)'

Edit: If you don't want grep itself to show up in the process list, filter it like this:

ps -ef | grep '(a.rb|b.rb|c.rb)' | grep -v grep

Upvotes: 0

Related Questions